之前一直在使用两个树莓派对应的摄像头去拼接成双目相机,最近买了一个双目小相机去玩一下,于是淘宝上买了个便宜的,二百多块钱,效果正在测试。
这个相机自带了一个拍摄程序,但是拍出来的图像左右图是在一起的,还需要自己分割,索性直接用python调用相机一起拍照保存。
会自动在当前文件夹创建left和right文件夹,按q退出,按s保存。
import cv2
import os
cap = cv2.VideoCapture(0)
# set the video frame width and height
cap.set(3,1280)
cap.set(4,480)
if not os.path.exists("left"):
os.makedirs("left")
if not os.path.exists("right"):
os.makedirs("right")
i = 1
try:
while True:
ret, frame = cap.read()
if not ret:
print("Failed to capture video")
break
# split the frame into left and right
left_frame = frame[:, :640, :]
right_frame = frame[:, 640:, :]
cv2.imshow("Left Camera", left_frame)
cv2.imshow("Right Camera", right_frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
elif key == ord('s'):
left_filename = "left" + str(i) + ".png"
right_filename = "right" + str(i) + ".png"
cv2.imwrite("left/" + left_filename, left_frame)
cv2.imwrite("right/" + right_filename, right_frame)
print("Image saved! left image:", left_filename)
print("Image saved! right image:", right_filename)
i += 1
except KeyboardInterrupt:
cv2.destroyAllWindows()
cap.release()