OpenCV (Python) 视频子图

Raf*_*azZ 4 python opencv matplotlib subplot

我试图在同一个图中显示两个 OpenCV 视频源作为子图,但找不到如何做到这一点。当我尝试使用时plt.imshow(...), plt.show(),窗口甚至不会出现。当我尝试使用 时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()
Run Code Online (Sandbox Code Playgroud)

更新:理想情况下,输出应该类似于:

OpenCV 视频的子图

Zda*_*daR 5

您可以简单地使用该cv2.hconcat()方法水平连接 2 个图像,然后使用 imshow 显示,但请记住,图像必须具有相同的大小类型才能应用于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()
Run Code Online (Sandbox Code Playgroud)