gpt4 book ai didi

python-3.x - 如何以数组形式显示与cv2串联的多个窗口?

转载 作者:行者123 更新时间:2023-12-02 17:58:12 25 4
gpt4 key购买 nike

我正在从各种相机实时捕获帧。每个摄像机的输出在带有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, 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个框架。然后创建一个空框架,并根据调整后的宽度和高度填充它。
由于我目前没有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()

关于python-3.x - 如何以数组形式显示与cv2串联的多个窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64207457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com