【问题标题】:How can I display multiple windows concatenated with cv2 in array form?如何以数组形式显示与 cv2 连接的多个窗口?
【发布时间】:2020-10-05 11:09:23
【问题描述】:

我正在从各种摄像机实时捕捉帧。每个摄像机的输出都显示在一个带有 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()

【问题讨论】:

    标签: python-3.x opencv cv2


    【解决方案1】:

    您可以通过将框架调整为宽度和高度的一半来构建一个由 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()
    

    【讨论】:

    • 谢谢,但这段代码对我不起作用。我想以动态的方式完成这一切。也就是说,用户将输入摄像机的数量,例如,两个、四个、六个等,然后将开始捕获帧,然后以矩阵的形式进行串联。你所做的是在同一个窗口中显示同一个相机的四次捕获,所有这些都是以静态方式进行的,这与我打算实现的相反。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多