使用两个USB摄像头获得图像,进行拼接达到双目相机的效果,并可以对图像进行截图保存。
import cv2
i = 0
cap1 = cv2.VideoCapture(0)
cap2 = cv2.VideoCapture(1) #打开两个摄像头
while (1):
retl, L = cap1.read()
retr, R = cap2.read()
cv2.resize(L, (640, 480))
cv2.resize(R, (640, 480)) #整理图像尺寸
image = cv2.hconcat([L, R]) #合并两个图像
cv2.imshow("capture", image) #显示结果
if cv2.waitKey(1) & 0xFF == ord('s'): #按下“s”键对图像进行保存
cv2.imwrite(str(i) + ".jpg",image)
i = i + 1
print("save"+str(i))