OpenCV:在捕获时更改 VideoCapture 的分辨率

OpenCV: change resolution of VideoCapture while it is capturing

我在 Windows 10 64 位上使用 OpenCV 3.1.0。我希望能够在网络摄像头仍在工作时设置网络摄像头的分辨率。相机工作后很容易设置分辨率。但是当网络摄像头正在捕获时我无法设置分辨率。

这是我的代码:

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

我最好的猜测是网络摄像头不支持您尝试的 CAP_PROP_FRAME_HEIGHT 值。如果将相机连接到 Linux 盒子,则可以使用 v4l2-ctl -d 0 --list-formats-ext 列出支持的视频格式。以下是 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)
            ...

我最近没有查看Windows上是否有类似v4l2-ctl的东西,它使用UVC从相机中查询信息。最近的网络摄像头通常支持 UVC。

问题是我只设置了一个随机高度,网络摄像头只支持它的预设分辨率。所以它 select 最匹配的预设分辨率来显示它。