参考的OpenCV-Python-Toturial-中文版.pdf的代码:
import numpy as np
import cv2
cap = cv2.VideoCapture(0) # 默认内置摄像头
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BAYER_BG2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF==ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
结果出现下面的错误:
[ WARN:0] videoio(MSMF): OnReadSample() is called with error status: -1072875772
[ WARN:0] videoio(MSMF): async ReadSample() call is failed with error status: -1072875772
[ WARN:1] videoio(MSMF): can't grab frame. Error: -1072875772
解决方案如下:
# import numpy as np
# import cv2
# camera_number = 0
# cap = cv2.VideoCapture( camera_number + cv2.CAP_DSHOW)
# # define the codec and create VideoWriter object
# fourcc = cv2.VideoWriter_fourcc(*'XVID')
# out = cv2.VideoWriter('output.avi',fourcc,20.0,(640,480))
# while(cap.isOpened()):
# ret, frame = cap.read()
# # write the flipped frame
# out.write(frame)
# cv2.imshow('image',frame)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
# else:
# break
# # release everything if job is finished
# cap.release()
# out.release()
# cv2.destroyAllWindows()
import numpy as np
import cv2
camera_number = 0
cap = cv2.VideoCapture( camera_number + cv2.CAP_DSHOW)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BAYER_BG2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF==ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
参考的解决方案:
https://stackoverflow.com/questions/51851198/opencv-set-camera-resolution-windows-vrs-linux
https://blog.youkuaiyun.com/root__yang/article/details/83180822