Search code examples
c++opencvvideo-capture

OpenCV: change resolution of VideoCapture while it is capturing


I am using OpenCV 3.1.0 on Windows 10 64-bit. I would like to be able to set the resolution of webcam while webcam still working. It's easy to set resolution after camera working. But I can't set resolution when webcam is capturing.

Here is my code:

cv::VideoCapture cap(0);
cap.set(cv::CAP_PROP_FRAME_WIDTH, 0x7FFFFFFF);          // working
cap.set(cv::CAP_PROP_FRAME_HEIGHT, 0x7FFFFFFF);         // working

while (true) {
    cv::Mat frame;
    cap >> frame;
    if (!frame.data) continue;
    cv::imshow("test", frame);
    if (cv::waitKey(1) >= 0) break;

    int newHeight = 500 + rand() % 4 * 100;
    cap.set(cv::CAP_PROP_FRAME_HEIGHT, newHeight);      // not working
}

int newHeight = 500 + rand() % 4 * 100;
cap.set(cv::CAP_PROP_FRAME_HEIGHT, newHeight);          // not working

Solution

  • The problem is I only set a random height, and webcam only supports its preset resolution. So it select a best matched preset resolution to show it.