以下是两个摄像头进行拼接的代码,同理可以拼接N个。
效果如下:由于两个摄像头分辨率不一样,以及懒得resize成一样的大小,显示效果左边视频上下留黑了。
import cv2
import numpy as np
##选择摄像头
videoLeftUp = cv2.VideoCapture(0)
videoRightUp = cv2.VideoCapture(1)
width = (int(videoLeftUp.get(cv2.CAP_PROP_FRAME_WIDTH)))
height = (int(videoLeftUp.get(cv2.CAP_PROP_FRAME_HEIGHT)))
while (videoLeftUp.isOpened()):
retLeftUp, frameLeftUp = videoLeftUp.read()
retRightUp, frameRightUp = videoRightUp.read()
frameLeftUp = cv2.resize(frameLeftUp, (int(width), int(height)), interpolation=cv2.INTER_CUBIC)
frameRightUp = cv2.resize(frameRightUp, (int(width), int(height)), interpolation=cv2.INTER_CUBIC)
frameUp = np.hstack((frameLeftUp, frameRightUp))
cv2.imshow('frame', frameUp)
print('111')
key = cv2.waitKey(10)
if int(key) == 113:
break
videoLeftUp.release()
videoRightUp.release()
在下面再加两个视频窗口可以采用上下拼接:
frameUp = np.hstack((frameLeftUp, frameRightUp))
frameDown = np.hstack((frameLeftDown, frameRightDown))
frame = np.vstack((frameUp, frameDown))