简体   繁体   中英

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

I'm using Microsoft LifeCam HD 3000. Default resolution is 640x480, but supports 1280x720.

Common code for changing resolution for OpenCV does no effect:

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.
  • Also tried any supported resolution, but result is the same
  • Changing FPS to lower before requesting high resolution also does no effect

Please help me:)

Finally I found an issue in OpenCV sources and solved it.

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).

I had to modify 'cap_dshow.cpp' to calculate resolution request in correct way corresponding to YUY2 (2 bytes per pixel):

Original code [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

Thanks to 'OBS Studio' open source project that helped to find out the solution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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