gpt4 book ai didi

python - OpenCV (Python) 视频子图

转载 作者:太空宇宙 更新时间:2023-11-03 15:01:22 25 4
gpt4 key购买 nike

我试图在同一张图中显示两个 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()

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

Subplots for OpenCV videos

最佳答案

您可以简单地使用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()

关于python - OpenCV (Python) 视频子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37669622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com