OpenCV(Python)视频子图 [英] OpenCV (Python) video subplots

查看:154
本文介绍了OpenCV(Python)视频子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以子图的形式显示两个OpenCV视频提要,但是找不到如何做.当我尝试使用plt.imshow(...), plt.show()时,该窗口甚至不会出现.当我尝试使用cv2.imshow(...)时,它显示了两个独立的数字.我真正想要的是子图:(.有帮助吗?

I am trying to show two OpenCV video feeds in the same figure as subplots, but couldn't find how to do it. When I try using plt.imshow(...), plt.show(), the window won't even appear. When I try using cv2.imshow(...), it shows two independent figures. What I actually want are subplots :(. Any help?

这是我到目前为止的代码:

Here is the code that I have so far:

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()

更新:理想情况下,输出应类似于以下内容:

UPDATE: Ideally the output should look something like that:

推荐答案

您可以简单地使用cv2.hconcat()方法水平合并2张图像,然后使用imshow进行显示,但是请记住,这些图像必须相同 size type 以便在其上应用hconcat.

You may simply use the cv2.hconcat() method to horizontally join 2 images and then display using imshow, But keep in mind that the images must be of same size and type for applying hconcat on them.

您也可以使用vconcat垂直合并图像.

You may also use vconcat to join the images vertically.

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()

这篇关于OpenCV(Python)视频子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆