python-opencv calls the binocular camera to capture video and save it

Friendly reminder: The binocular camera used in this experiment has a USB port, so when calling the camera, you only need to call a camera with a device number of 0. When subsequently displaying and saving the left and right camera images, you need to segment the entire image first. (If you use two cameras, you need to call two devices with device numbers 0 and 1. At this time, you can operate both devices at the same time when the image is displayed and saved. There is no need to split the image)

#调用双目摄像头录像,按q退出录制

import cv2 as cv2
import time

cv2.namedWindow('left')
cv2.namedWindow('right')

camera = cv2.VideoCapture(0)  # 打开摄像头,摄像头的ID不同设备上可能不同

camera.set(cv2.CAP_PROP_FRAME_WIDTH, 2560)  # 设置双目的宽度(整个双目相机的图像宽度)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)  # 设置双目的高度

fourcc=cv2.VideoWriter_fourcc(*'XVID')       # fourcc编码为视频格式,avi对应编码为XVID
left=cv2.VideoWriter("D:/test/savevideo/zuo.avi",fourcc,20.0,(1280,720))    #(视频储存位置和名称,储存格式,帧率,视频大小)
right=cv2.VideoWriter("D:/test/savevideo/you.avi",fourcc,20.0,(1280,720))

while camera.isOpened():
    ret, frame = camera.read()
    print('ret:', ret)
    left_frame = frame[0:720, 0:1280]  # 裁剪坐标为[y0:y1,x0:x1]
    right_frame = frame[0:720, 1280:2560]
    if not ret:
        break

    left.write(left_frame)
    right.write(right_frame)
    cv2.imshow('left', left_frame)
    cv2.imshow('right', right_frame)

    if cv2.waitKey(1) == ord('q'):
        break

camera.release()
left.release()
right.release()
cv2.destroyAllWindows()

Take a binocular image and save the code: https://blog.csdn.net/weixin_47214888/article/details/129101427?spm=1001.2014.3001.5501

Guess you like

Origin blog.csdn.net/weixin_47214888/article/details/130225163