如何在Python中使用OpenCV以特定的顺序显示图像?

2024-06-01 09:03:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个程序,从用户那里获取一个字符串,并显示该字符串中每个字符的手语图像。此代码显示相互重叠的图像。有没有一种方法我可以显示这些图像的顺序,他们采取了一个输入?This is the output I am currently getting.This is how I want the output to look

import cv2

print("Say something !!!")
say = input()
i=1

for x in say :
    if x == " ":
        continue
    img = cv2.imread("TrainData\\" + x + "_1.jpg")
    cv2.imshow(x + str(i), img)
    i= i+1
cv2.waitKey(0)

我想根据输入从左到右显示图像。你知道吗


Tags: the方法字符串代码用户图像程序img
1条回答
网友
1楼 · 发布于 2024-06-01 09:03:21

您可以将图像并排连接起来并在单个窗口中显示,而不是尝试定位窗口:

#!/usr/local/bin/python3
import numpy as np
import cv2

# Load the 4 letters we need
h = cv2.imread('h.png',0)
e = cv2.imread('e.png',0)
l = cv2.imread('l.png',0)
o = cv2.imread('o.png',0)

# Append images side-by-side
result = np.concatenate((h,e,l,l,o),axis=1)

# Save to disk, or display as a single, wide image
cv2.imwrite('result.png',result)

enter image description here


当然,如果需要,可以制作一个垫片:

#!/usr/local/bin/python3
import numpy as np
import cv2

# Load the 4 letters we need
h = cv2.imread('h.png',0)
e = cv2.imread('e.png',0)
l = cv2.imread('l.png',0)
o = cv2.imread('o.png',0)

shim = np.ones((200,10),dtype=np.uint8)*255

# Append images side-by-side
result = np.concatenate((h,shim,e,shim,l,shim,l,shim,o),axis=1)
cv2.imwrite('result.png',result)

enter image description here

相关问题 更多 >