如何解决如何以数组形式显示与cv2串联的多个窗口?
我正在从各种相机实时捕获帧。每个摄像机的输出在带有cv2.imshow的窗口中显示。我现在想要实现的是将所有窗口串联在一起形成一个网格/矩阵,例如,如果我有4个摄像机,那么这些窗口将显示为2x2串联。
下面附上我所拥有的代码。这使我可以捕获所拥有的不同相机的帧,但无法执行上述操作。
cap = []
ret = []
frame = []
final = ""
i = 0
cap.append(cv2.VideoCapture(0))
cap.append(cv2.VideoCapture(1))
cap.append(cv2.VideoCapture(2))
number_cameras = len(cap)
# I initialize values
for x in range(number_cameras):
ret.append(x)
frame.append(x)
while(True):
# I capture frame by frame from each camera
ret[i],frame[i] = cap[i].read()
if i == number_cameras-1:
final = cv2.hconcat([cv2.resize(frame[x],(400,400)) for x in range(number_cameras)])
cv2.namedWindow('frame')
cv2.moveWindow('frame',0)
# I show the concatenated outputs
cv2.imshow('frame',final)
i = 0
else:
i = i + 1
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# I release the cameras
for x in range(number_cameras):
cap[x].release()
cv2.destroyAllWindows()
解决方法
您可以通过将四个框架的大小调整为宽度和高度的一半来构造一个框架。然后创建一个空框架并根据调整后的宽度和高度填充它。
由于我目前没有4台摄像机,因此我只使用一台摄像机进行仿真。
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret,frame = cap.read()
#find half of the width
w = int(frame.shape[1] * 50 / 100)
#find half of the height
h = int(frame.shape[0] * 50 / 100)
dim = (w,h)
#resize the frame to half it size
frame =cv2.resize(frame,dim,interpolation = cv2.INTER_AREA)
#create an empty frame
output = np.zeros((h*2,w*2,3),np.uint8)
#place a frame at the top left
output[0:h,0:w] = frame
#place a frame at the top right
output[0:h,w:w * 2] = frame
#place a frame at the bottom left
output[h:h * 2,w:w * 2] = frame
#place a frame at the bottom right
output[h:h * 2,0:w] = frame
cv2.imshow("Output",output)
key = cv2.waitKey(1) & 0xFF
# if the `q` key was pressed,break from the loop
if key == ord("q"):
break
cv2.destroyAllWindows()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。