Increasing Camera Capture Resolution in OpenCV

When working with webcam feeds in OpenCV, many developers encounter limits on resolution capture. A common scenario arises with webcams like the Logitech QuickCam IM, which supports several resolutions but defaults to a lower setting, restricting higher-quality image captures. If you’ve ever struggled to enhance your camera’s resolution beyond the frustrating 320x240 limitation, you’re not alone. Let’s explore a solution to this problem!

The Challenge

You might have a setup where your webcam supports resolutions like 640x480 and 1280x960. However, attempts to adjust this in your C/C++ OpenCV program can remain futile, only delivering images at the default 320x240 resolution. The common method using cvSetCaptureProperty() often appears ineffective for these adjustments. The question is: how can you break free from this limitation?

The Solution

1. Modify Your Source Code

To increase the camera resolution, you’ll need to implement a hack shared by a user in the OpenCV community. This involves modifying some header files and adding a specific function to your OpenCV setup.

Add Definitions to highgui.h

First, you need to extend the functionality of the highgui.h. Add the following definitions:

#define CV_CAP_PROP_DIALOG_DISPLAY 8
#define CV_CAP_PROP_DIALOG_FORMAT 9
#define CV_CAP_PROP_DIALOG_SOURCE 10
#define CV_CAP_PROP_DIALOG_COMPRESSION 11
#define CV_CAP_PROP_FRAME_WIDTH_HEIGHT 12

Implement the icvSetPropertyCAM_VFW Function

Next, you have to create a new function named icvSetPropertyCAM_VFW in cvcap.cpp. This is where the workings of setting the camera properties will take place:

static int icvSetPropertyCAM_VFW( CvCaptureCAM_VFW* capture, int property_id, double value )
{
    int result = -1;
    CAPSTATUS capstat;
    CAPTUREPARMS capparam;
    BITMAPINFO btmp;

    switch( property_id )
    {
        case CV_CAP_PROP_DIALOG_DISPLAY:
            result = capDlgVideoDisplay(capture->capWnd);
            break;

        case CV_CAP_PROP_DIALOG_FORMAT:
            result = capDlgVideoFormat(capture->capWnd);
            break;

        case CV_CAP_PROP_DIALOG_SOURCE:
            result = capDlgVideoSource(capture->capWnd);
            break;

        case CV_CAP_PROP_DIALOG_COMPRESSION:
            result = capDlgVideoCompression(capture->capWnd);
            break;

        case CV_CAP_PROP_FRAME_WIDTH_HEIGHT:
            capGetVideoFormat(capture->capWnd, &btmp, sizeof(BITMAPINFO));
            btmp.bmiHeader.biWidth = floor(value/1000);
            btmp.bmiHeader.biHeight = value - floor(value/1000) * 1000;
            btmp.bmiHeader.biSizeImage = btmp.bmiHeader.biHeight * btmp.bmiHeader.biWidth * btmp.bmiHeader.biPlanes * btmp.bmiHeader.biBitCount / 8;
            capSetVideoFormat(capture->capWnd, &btmp, sizeof(BITMAPINFO));
            break;

        default:
            break;
    }

    return result;
}

Update the captureCAM_VFW_vtable

In addition, make sure to update the captureCAM_VFW_vtable like so:

static CvCaptureVTable captureCAM_VFW_vtable =
{
    6,
    (CvCaptureCloseFunc)icvCloseCAM_VFW,
    (CvCaptureGrabFrameFunc)icvGrabFrameCAM_VFW,
    (CvCaptureRetrieveFrameFunc)icvRetrieveFrameCAM_VFW,
    (CvCaptureGetPropertyFunc)icvGetPropertyCAM_VFW,
    (CvCaptureSetPropertyFunc)icvSetPropertyCAM_VFW, // now properly set
    (CvCaptureGetDescriptionFunc)0
};

Rebuild the Library

After making the above adjustments, the final step is to rebuild your highgui.dll. This integration will enable your program to utilize higher resolutions such as 640x480, making your webcam image capture much more versatile.

Conclusion

Increasing the resolution of your camera capture in OpenCV can be a technical hurdle, but with the right modifications, it’s entirely achievable. By implementing the solutions described, you can unlock higher-quality imaging from your webcam, enhancing your projects that rely on this crucial functionality. Don’t let default settings hold you back—take control of your webcam’s potential!