OpenCV(Python)视频子图

我试图以子图的形式显示两个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()

更新:理想情况下,输出应如下所示:

OpenCV(Python)视频子图

解决方法:

您可以简单地使用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()
上一篇:【优化方法】牛顿法实例


下一篇:python-matplotlib子图的刻度标签字体不一致