[zz]OpenCV设置摄像头的分辨率

Edit on Github

原出处

OpenCV中原本是有设置视频捕捉属性的函数的,如下:

CVAPI(int)    cvSetCaptureProperty( CvCapture* capture, int property_id, double value );
//cvSetCaptureProperty(pCapture, CV_CAP_PROP_FPS, 30);
//cvSetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_WIDTH, 1024);
//cvSetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_HEIGHT, 768);

不过我用了之后没效果,就google到这篇文章了,也仅此一篇,实践过可行,源码也蛮清楚的,保存下。


Opencv是提供了一系列的摄像头设置函数的,但是没有提供相应的实现,使用的话,需要如下设置:

1 把下面几个定义添加到 highgui.h中

#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

2 把页面中的函数

static int icvSetPropertyCAM_VFW( CvCaptureCAM_VFW* capture, int property_id, double value )

添加到cvcap_vfw.cpp中,放在typedef struct CvCaptureCAM_VFW 这个结构之后。(见下文)

3 用下面的函数代替cvcap_vfw.cpp中的同名函数

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

4 编译highgui

用的时候调用如下函数即可

cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH_HEIGHT, 640480 )

补充,需要的函数:

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);
        //SendMessage(capture->capWnd,WM_CAP_DLG_VIDEODISPLAY,0,0);
        break;
    case CV_CAP_PROP_DIALOG_FORMAT:
        result = capDlgVideoFormat(capture->capWnd);
        //SendMessage(capture->capWnd,WM_CAP_DLG_VIDEOFORMAT,0,0);
        break;
    case CV_CAP_PROP_DIALOG_SOURCE:
        result = capDlgVideoSource(capture->capWnd);
        //SendMessage(capture->capWnd,WM_CAP_DLG_VIDEOSOURCE,0,0);
        break;
    case CV_CAP_PROP_DIALOG_COMPRESSION:
        result = capDlgVideoCompression(capture->capWnd);
        break;
    case CV_CAP_PROP_FRAME_WIDTH_HEIGHT:
        capGetVideoFormat(capture->capWnd,, 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,, sizeof(BITMAPINFO));
        break;
    default:
        break;
    } 

    return result;
}