A.J
A.J

Reputation: 348

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

Upvotes: 1

Views: 4235

Answers (2)

A.J
A.J

Reputation: 348

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.

Upvotes: 1

Ulrich Stern
Ulrich Stern

Reputation: 11301

My best guess is the values for CAP_PROP_FRAME_HEIGHT you are attempting are not supported by the webcam. If you hook your camera up to a Linux box, you can use v4l2-ctl -d 0 --list-formats-ext to list the supported video formats. Here an excerpt of the output for a Microsoft LifeCam Cinema:

    Index       : 1
    Type        : Video Capture
    Pixel Format: 'MJPG' (compressed)
    Name        : Motion-JPEG
            Size: Discrete 640x480
                    Interval: Discrete 0.033s (30.000 fps)
                    Interval: Discrete 0.050s (20.000 fps)
                    Interval: Discrete 0.067s (15.000 fps)
                    Interval: Discrete 0.100s (10.000 fps)
                    Interval: Discrete 0.133s (7.500 fps)
            Size: Discrete 1280x720
                    Interval: Discrete 0.033s (30.000 fps)
                    Interval: Discrete 0.050s (20.000 fps)
                    Interval: Discrete 0.067s (15.000 fps)
                    Interval: Discrete 0.100s (10.000 fps)
                    Interval: Discrete 0.133s (7.500 fps)
            ...

I have not checked recently whether on Windows there is something similar to v4l2-ctl, which uses UVC to query the info from the camera. UVC is typically supported by recent webcams.

Upvotes: 2

Related Questions