2
机器视觉+OpenCV:在Python中用单窗口实现多摄像头画面——以双目摄像头为例
source link: https://www.taholab.com/24127
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
机器视觉+OpenCV:在Python中用单窗口实现多摄像头画面——以双目摄像头为例
为了更好的查看左右摄像头的画面,以便实现舒适的立体效果,应该将画面显示在通一个窗口之中。将画面显示在同一窗口中,可以借用numpy的功能。因为画面或者视频本质上都是一堆矩阵,其实就是用函数将矩阵组合,分为水平组合和垂直组合,用到的函数为hstack和vstack。
以下为左右排列摄像头的代码:
import cv2
import numpy as np
left_camera = cv2.VideoCapture(1) #左摄像头
right_camera = cv2.VideoCapture(6) #右摄像头
while True:
ret, left_video = left_camera.read() #读取左摄像头
ret, right_video = right_camera.read() #读取右摄像头
cam_h_stack = np.hstack((left_video, right_video)) #水平组合
# 垂直组合用 cam_v_stack = np.vstack((left_video, right_video))
cv2.namedWindow("left-and-right-dual-cameras") #创建窗口
cv2.imshow("left-and-right-dual-cameras", cam_h_stack)
key = cv2.waitKey(1)
if key == ord('q'):
break
left_camera.release()
right_camera.release()
cv2.destroyAllWindows()
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK