简体   繁体   English

使用Python中的OpenCV无法更改网络摄像头分辨率(Windows)

[英]Can't change webcam resolution with OpenCV in Python (Windows)

I'm using Microsoft LifeCam HD 3000. Default resolution is 640x480, but supports 1280x720. 我使用的是Microsoft LifeCam HD 3000.默认分辨率为640x480,但支持1280x720。

Common code for changing resolution for OpenCV does no effect: 更改OpenCV分辨率的常用代码不起作用:

video_capture = cv2.VideoCapture(0)

print video_capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 1280)
print video_capture.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 720)

print video_capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)
print video_capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)

output: 输出:

True 
True 
640.0
480.0
  • In different software like OBS studio the webcam easily configures to 1280x720. 在像OBS studio这样的不同软件中,网络摄像头可轻松配置为1280x720。
  • Also tried any supported resolution, but result is the same 还试过任何支持的分辨率,但结果是一样的
  • Changing FPS to lower before requesting high resolution also does no effect 在请求高分辨率之前将FPS更改为较低也不起作用

Please help me:) 请帮我:)

Finally I found an issue in OpenCV sources and solved it. 最后我在OpenCV源中发现了一个问题并解决了它。

MS Lifecam HD3000 works only in YUY2 format and also requires exact resolution to be requested or it simply rejects it (some cameras just choose the closest one). MS Lifecam HD3000仅以YUY2格式工作,并且还要求精确的分辨率,或者它只是拒绝它(有些相机只选择最接近的一个)。

I had to modify 'cap_dshow.cpp' to calculate resolution request in correct way corresponding to YUY2 (2 bytes per pixel): 我必须修改'cap_dshow.cpp'以正确的方式计算与YUY2相对应的解析请求(每像素2个字节):

Original code [static bool setSizeAndSubtype(...)]: 原始代码[static bool setSizeAndSubtype(...)]:

//buffer size
if (mediatype == MEDIASUBTYPE_RGB24)
{
    VD->pAmMediaType->lSampleSize = attemptWidth*attemptHeight*3;
}
else
{
    // For compressed data, the value can be zero.
    VD->pAmMediaType->lSampleSize = 0;
}

Replaced to 替换为

if (mediatype == MEDIASUBTYPE_RGB24) {
    VD->pAmMediaType->lSampleSize = attemptWidth*attemptHeight * 3;
}
else if ((mediatype == MEDIASUBTYPE_YUY2) || (mediatype == MEDIASUBTYPE_YVYU) ||
    (mediatype == MEDIASUBTYPE_UYVY)) {

    VD->pAmMediaType->lSampleSize = attemptWidth*attemptHeight * 2;
}
else {
    VD->pAmMediaType->lSampleSize = 0;
}

Probably the problem will also appear for some other formats and webcams. 对于其他一些格式和网络摄像头,问题也可能出现。 I'll open an issues on GitHub 我将在GitHub上打开一个问题

Thanks to 'OBS Studio' open source project that helped to find out the solution 感谢“OBS Studio”开源项目,该项目有助于找到解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM