opencv设置相机分辨率Windows vrs linux

时间:2018-08-14 23:56:14

标签: python opencv image-resizing

会发生这种变化的问题,但不会出现这种错误情况。

此代码在Linux上有效,在Windows10 OpenCV 3.4.2、64位-Python 3.6上失败-通过“ pip3 install opencv-python”安装

在Windows上失败,并显示以下错误消息。

如果我删除了'cv2.set()'作为水平和垂直大小,则可以正常工作,但分辨率不是我想要的

我的目标是将默认图像尺寸更改为更大图像。

v=3.4.2
Camera H=480, W=640
Camera H=480, W=640
[ WARN:0] videoio(MSMF): OnReadSample() is called with error status: -1072875855
[ WARN:0] videoio(MSMF): async ReadSample() call is failed with error status: -1072875855
[ WARN:1] videoio(MSMF): can't grab frame. Error: -1072875855
[ WARN:1] videoio(MSMF): can't grab frame. Error: -2147483638

这是Python代码

import cv2
print("v=%s" % cv2.__version__)
cap = cv2.VideoCapture(0, cv2.)
h = cap.get( cv2.CAP_PROP_FRAME_HEIGHT )
w = cap.get( cv2.CAP_PROP_FRAME_WIDTH )
print("Camera H=%d, W=%d" % (h,w) )
# If I remove these two lines it works but is stuck at 640x480
cap.set( cv2.CAP_PROP_FRAME_HEIGHT, 10000 )
cap.set( cv2.CAP_PROP_FRAME_WIDTH, 10000 )
h = cap.get( cv2.CAP_PROP_FRAME_HEIGHT )
w = cap.get( cv2.CAP_PROP_FRAME_WIDTH )
print("Camera H=%d, W=%d" % (h,w) )

while(True):
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    ret, frame = cap.read()
    if ret:
        cv2.imshow('frame',frame)

cap.release()
cv2.destroyAllWindows()

3 个答案:

答案 0 :(得分:3)

我可以通过切换到DSHOW后端API来解决此问题:

cap = cv2.VideoCapture(cv2.CAP_DSHOW)

答案 1 :(得分:1)

感谢brianpeiris解决方案,我只是在这里以更正式的方式进行记录

cv2.CAP_DSHOW是作为打开调用的一部分传递的标志,您可以传递许多其他标志,并且此CAP_DSHOW是Microsoft特定的。

import cv2

camera_number = 0

c = cv2.VideoCapture( camera_number + cv2.CAP_DSHOW)
# this picks the LARGEST image possible
c.set( cv2.CAP_PROP_FRAME_HEIGHT, 10000 )
c.set( cv2.CAP_PROP_FRAME_WIDTH, 10000 )

while True:
    a,f = c.read()
    if not a:
         continue
    cv2.imshow( "it-works", f )
    k=cv2.waitKey(10)
    # press q to quit.
    if k == ord('q'):
       break

答案 2 :(得分:0)

为了能够选择源索引,this comment中的答案效果很好。

videoSourceIndex = 1
cap = cv2.VideoCapture(cv2.CAP_DSHOW + videoSourceIndex)