我尝试使用 HTC Vive Pro 前面的两个摄像头,以便使用立体视觉实现 SLAM。我希望如果可以使用 Python 实现这一点,但是,我找不到打开两个摄像头的好方法(我只能使用
cv2.VideoCapture(1)
打开右侧摄像头)。该设备目前仅通过 USB 连接。
到目前为止我所拥有的是
import cv2
cv2.namedWindow("Camera 1")
cv2.namedWindow("Camera 2")
stereo = cv2.VideoCapture(1)
if stereo.isOpened():
rval, frame = stereo.read()
else:
rval = False
while rval:
rval_left, left = stereo.retrieve(0)
rval_right, right = stereo.retrieve(1)
cv2.imshow("Camera 1", left)
cv2.imshow("Camera 2", right)
key = cv2.waitKey(20)
if key == 27:
# exit on ESC
break
cv2.destroyAllWindows()
但是
stereo
并不像预期的那样立体。 cv2.VideoCapture(0)
是笔记本电脑网络摄像头,所有其他 cv2.VideoCapture(...)
均返回 None。我希望有人能帮忙。
默认情况下,相机设置为以 640x480 分辨率进行拍摄,但它确实支持 640x960 分辨率(请参阅:this 和 this)。
下面是一个简单的解决方案。
import cv2
cv2.namedWindow("Camera 1")
cv2.namedWindow("Camera 2")
stereo = cv2.VideoCapture(1)
width = stereo.get(cv2.CAP_PROP_FRAME_WIDTH)
height = stereo.get(cv2.CAP_PROP_FRAME_HEIGHT)
print(f"default size: {width} x {height}")
stereo.set(cv2.CAP_PROP_FRAME_WIDTH, width)
stereo.set(cv2.CAP_PROP_FRAME_HEIGHT, height*2)
while stereo.isOpened():
rval, frame = stereo.read()
if not rval:
break
left = frame[int(height):, :, ...]
right = frame[:int(height), :, ...]
cv2.imshow("Camera 1", left)
cv2.imshow("Camera 2", right)
key = cv2.waitKey(20)
if key == 27:
# exit on ESC
break
cv2.destroyAllWindows()