省流:

  1. 内含 Python Opencv 双目相机拍照代码(手动 or 自动),可自取;
  2. 如果你的 cv2.VideoCapture() 函数卡住但不报错,打开 Windows “相机”应用可以正常看到摄像头画面,且能够正常用 cv2.imshow() 打开图像,请继续阅读。

这个学期选修了方璐老师的媒体与认知课程,期末的 Final Project 涉及到双目相机的标定问题(也许期末做完 Project 我会发一些感想和体会?)

助教提供了一段基于 Python Opencv 的双目相机自动拍摄和保存的代码给我们直接用,但是在运行的时候我遇到了奇怪的现象,程序卡在 cv2.VideoCapture(0) 这里就不能运行,不报错,就是单纯的卡在这里。先放代码:

import time

import cv2
import numpy as np

AUTO = False  # 自动拍照,或手动按s键拍照
INTERVAL = 2  # 自动拍照间隔

camera_0 = cv2.VideoCapture(0) # 代码运行的时候卡在这里!!
camera_1 = cv2.VideoCapture(1)

cv2.namedWindow("left")
cv2.namedWindow("right")

counter = 0
utc = time.time()
folder = "./SaveImage/"  # 拍照文件目录, 先新建再运行,否则无法保存


def shot(pos, frame):
    global counter
    path = folder + pos + "_" + str(counter) + ".jpg"
    cv2.imwrite(path, frame)
    print("snapshot saved into: " + path)


while True:
    ret_0, frame_0 = camera_0.read()
    ret_1, frame_1 = camera_1.read()
    left_frame = frame_0
    right_frame = frame_1
    cv2.imshow("left", left_frame)
    cv2.imshow("right", right_frame)
    now = time.time()

    if AUTO and now - utc >= INTERVAL:
        shot("left", left_frame)
        shot("right", right_frame)
        counter += 1
        utc = now

    key = cv2.waitKey(1)

    # press 's' in keyboard for capturing, 'q' for exit.
    if key == ord("q"):
        break
    elif key == ord("s"):
        shot("left", left_frame)
        shot("right", right_frame)
        counter += 1

camera_0.release()
camera_1.release()
cv2.destroyWindow("left")
cv2.destroyWindow("right")

这就比较违反常理,我和助教进行了一番讨论,无果。

最后解决的办法也比较奇怪,把卡住的代码修改为:

camera_0 = cv2.VideoCapture(0, cv2.CAP_DSHOW)

就可以了。(当然每一个 cv2.VideoCapture 都要改一下的)

这里我并不知道是怎么回事,我找到的一些资料说这个参数的作用是“不指定无法使用高分辨率摄像头”,那是不是有可能是因为课程提供的摄像头非常高清导致不设置不能使用(瞎猜,如有了解万望赐教)?因为我对 opencv 这个库基本上不了解,感觉这个大作业里面涉及到 opencv 的也就是拍个照,就不会深究了。

希望能帮助到一些和我一样在这里 stuck 的同学们~

(希望我的大作业能继续推进啊啊啊啊啊啊)

2022.12.14 补充

最近准备用新装的 Fedora 37 做开发平台了,于是把之前的代码 copy 到 Linux 上试了一下,发现 Linux 好像并不需要加这个参数……就还挺无语的 orz

GitHub 加速计划 / opencv31 / opencv
216
19
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:14 天前 )
3c3a26b6 imgcodecs: bmp: relax decoding size limit to over 1GiB #27811 Close https://github.com/opencv/opencv/issues/27789 Close https://github.com/opencv/opencv/issues/23233 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 6 小时前
744d5ecd libtiff upgrade to version 4.7.1 #27806 close https://github.com/opencv/opencv/issues/27784 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 7 小时前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐