OpenCV (Python) 视频子图

OpenCV (Python) video subplots

我想在同一张图中显示两个 OpenCV 视频源作为子图,但找不到如何操作。当我尝试使用 plt.imshow(...), plt.show() 时,window 甚至不会出现。当我尝试使用 cv2.imshow(...) 时,它显示了两个独立的数字。我真正想要的是子图:(。有什么帮助吗?

这是我目前的代码:

import numpy as np
import cv2
import matplotlib.pyplot as plt

cap = cv2.VideoCapture(0)
ret, frame = cap.read()

while(True):
    ret, frame = cap.read()
    channels = cv2.split(frame)
    frame_merge = cv2.merge(channels)

    #~ subplot(211), plt.imshow(frame)
    #~ subplot(212), plt.imshow(frame_merged)
    cv2.imshow('frame',frame)
    cv2.imshow('frame merged', frame_merge)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

cap.release()
cv2.destroyAllWindows()

更新: 理想情况下输出应该是这样的:

您可以简单地使用cv2.hconcat()方法水平连接2张图像,然后使用imshow显示,但请记住图像必须具有相同的大小type 应用 hconcat 在他们身上。

您也可以使用 vconcat 垂直连接图像。

import numpy as np
import cv2
import matplotlib.pyplot as plt

cap = cv2.VideoCapture(0)
ret, frame = cap.read()

bg = [[[0] * len(frame[0]) for _ in xrange(len(frame))] for _ in xrange(3)]

while(True):
    ret, frame = cap.read()
    # Resizing down the image to fit in the screen.
    frame = cv2.resize(frame, None, fx = 0.5, fy = 0.5, interpolation = cv2.INTER_CUBIC)

    # creating another frame.
    channels = cv2.split(frame)
    frame_merge = cv2.merge(channels)

    # horizintally concatenating the two frames.
    final_frame = cv2.hconcat((frame, frame_merge))

    # Show the concatenated frame using imshow.
    cv2.imshow('frame',final_frame)

    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

cap.release()
cv2.destroyAllWindows()