我刚接触opencv并尝试通过OpenCV Python访问Macbook内置摄像头,但出现错误。
import cv2
frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture(0)
cap.set(3, frameWidth)
cap.set(4, frameHeight)
cap.set(10,150)
while True:
success, img = cap.read()
cv2.imshow("Result", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
Traceback (most recent call last):
File "/Users/hasanaktas/PycharmProjects/OpencvPython/project3.py", line 12, in <module>
cv2.imshow("Result", img)
cv2.error: OpenCV(4.2.0) /Users/travis/build/skvark/opencv-python/opencv/modules/highgui/src/window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'
已经尝试将VideoCapture(0)更改为VideoCapture(1)并添加以下代码,但仍然没有帮助。 顺便说一下,我正在使用PyCharm。
cap.release()
cv2.destroyAllWindows()
cap.set(10,150)
是什么意思?尽量避免使用像这样的丑陋常量,而是使用为您定义的参数名称。 - Mark Setchell