我正在尝试构建一个简短的脚本,用我的立体相机拍摄多张图像,并在我按下按钮时将它们保存到一个目录中。
但出于某种原因,即使我拍了多张照片,我也只能得到最后一张照片。它也没有显示任何错误并打印出我在代码中编写的正确字符串。但我得到的只是最后一个图像对。
我看了几个帖子,但没有一个有同样的问题。
编辑:我没有像这里的每个人建议的那样在图像名称中添加秒数,因此图像在同一分钟拍摄时被覆盖。
如果有人想使用它,这是我现在完成的代码:
import numpy as np
import cv2
import os
import datetime
# shape of wholeFrame: (240, 640, 3)
cap = cv2.VideoCapture(1)
now = 0
imgs_taken = 0
newpath_l = "Recorded_Images/left_imgs"
newpath_r = "Recorded_Images/right_imgs"
newpath = "Recorded_Images/both_imgs"
if not os.path.exists(newpath_l):
os.makedirs(newpath_l)
if not os.path.exists(newpath_r):
os.makedirs(newpath_r)
if not os.path.exists(newpath):
os.makedirs(newpath)
while 1:
cap.grab()
ret, wholeFrame = cap.retrieve()
if ret:
leftFrame = wholeFrame[:, 0:320, :]
rightFrame = wholeFrame[:, 320:640, :]
# Rectifying images here
stereoImage = np.concatenate((leftFrame, rightFrame), axis=1) …Run Code Online (Sandbox Code Playgroud)