由於要實時修改攝像頭解析度,發現普通攝像頭很難滿足,所以對Opencv原始碼進行修改,如下

1. 開啟原始碼/modules/highgui/include/opencv2/highgui/highgui_c.h檔案,找到CV_CAP_PROP_FRAME_WIDTH= 3的列舉結構體,在該結構體後面加上CV_CAP_PROP_FRAME_WIDTHHEIGHT = 6666

2. 開啟原始碼/modules/highgui/src/cap_dshow.cpp,找到函式:bool CvCaptureCAM_DShow::setProperty(int property_id,double value)在該函式中

switch( property_id )
    {
    case CV_CAP_PROP_FRAME_WIDTH:
        width = cvRound(value);
        handled = true;
        break;

    case CV_CAP_PROP_FRAME_HEIGHT:
        height = cvRound(value);
        handled = true;
        break;

    case CV_CAP_PROP_FRAME_WIDTHHEIGHT:
        width = floor(value/1000);
        height = value-floor(value/1000)*1000;
        VI.stopDevice(index);
        VI.setupDevice(index, width, height);
        VI.restartDevice(index);
        handled = true;
        break;

    case CV_CAP_PROP_FOURCC:
        fourcc = (int)(unsigned long)(value);
        if ( fourcc == -1 ) {
            // following cvCreateVideo usage will pop up caprturepindialog here if fourcc=-1
            // TODO - how to create a capture pin dialog
        }
        handled = true;
        break;

    case CV_CAP_PROP_FPS:
        int fps = cvRound(value);
        if (fps != VI.getFPS(index))
        {
            VI.stopDevice(index);
            VI.setIdealFramerate(index,fps);
            if (widthSet > 0 && heightSet > 0)
                VI.setupDevice(index, widthSet, heightSet);
            else
                VI.setupDevice(index);
        }
        return VI.isDeviceSetup(index);
}

新增下面程式碼,儲存,這樣子就修改成功,然後重新編譯就可以

case CV_CAP_PROP_FRAME_WIDTHHEIGHT:
        width = floor(value/1000);
        height = value-floor(value/1000)*1000;
        VI.stopDevice(index);
        VI.setupDevice(index, width, height);
        VI.restartDevice(index);
        handled = true;
        break;