问题描述
我在64位Ubuntu 12.04上使用OpenCV 2.4.5.我希望能够设置Logitech C310网络摄像头的输入分辨率.相机在30fps时支持高达1280x960,并且我可以在guvcview中以这种分辨率观看视频.但是OpenCV始终只能以640x480的分辨率获得视频.
I am using OpenCV 2.4.5 on Ubuntu 12.04 64-bit. I would like to be able to set the resolution of the input from my Logitech C310 webcam. The camera supports up to 1280x960 at 30fps, and I am able to view the video at this resolution in guvcview. But OpenCV always gets the video at only 640x480.
尝试在创建VideoCapture上限后立即更改cap.set(CV_CAP_PROP_FRAME_WIDTH,1280)和cap.set(CV_CAP_PROP_FRAME_HEIGHT,960)的分辨率无效;尝试在获取每一帧之前立即进行设置会导致程序立即崩溃.我也不能用这种方法降低分辨率.我也收到错误"HIGHGUI错误:V4L/V4L2:VIDIOC_S_CROP".我认为这可能是相关的,因为它在创建VideoCapture时出现一次,而在我尝试设置宽度和高度时出现一次(但是,奇怪的是,如果我只设置其中之一,则不会出现).
Trying to change the resolution with cap.set(CV_CAP_PROP_FRAME_WIDTH, 1280) and cap.set(CV_CAP_PROP_FRAME_HEIGHT, 960) immediately after the VideoCapture cap is created has no effect; trying to set them immediately before getting every frame causes the program to crash immediately. I cannot reduce the resolution with this method either. I am also getting the error "HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP". I think this may be related, because it appears once when the VideoCapture is created, and once when I try to set the width and height (but, oddly, not if I try to set only one of them).
我知道我并不是第一个遇到此问题的人,但是在对SO和互联网上的其他内容进行大量谷歌搜索和搜索之后,我还没有找到解决方案(在我已经尝试过的许多尝试中,这个StackOverflow问题的答案:提高OpenCV中的摄像机捕获分辨率).这是OpenCV中的错误吗?如果是这样,那是一个相当明显的问题.
I know I'm not the first to have this problem, but I have yet to find a solution after much Googling and scouring of SO and elsewhere on the internet (among the many things I've already tried to no avail is the answer to this StackOverflow question: Increasing camera capture resolution in OpenCV). Is this a bug in OpenCV? If so, it's a rather glaring one.
这是一个显示问题的代码示例(只是OpenCV视频显示代码的修改版本):
Here's an example of code that exhibits the problem (just a modified version of OpenCV's video display code):
#include <cv.h>
#include <highgui.h>
using namespace cv;
int main(int argc, char** argv)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
cap.set(CV_CAP_PROP_FRAME_WIDTH, 160);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 120);
Mat image;
namedWindow("Video", CV_WINDOW_AUTOSIZE);
while(1)
{
// cap.set(CV_CAP_PROP_FRAME_WIDTH, 160);
// cap.set(CV_CAP_PROP_FRAME_HEIGHT, 120);
cap >> image;
imshow("Video", image);
if(waitKey(10) == 99 ) break;
}
return
}
就这样,如上所述,我得到了两个"HIGHGUI ERROR",并且得到了640x480的输出.我知道160x120是我的相机运行v4l2-ctl --list-formats-ext
所支持的分辨率.如果取消注释while循环中的两条注释掉的行,该程序将立即崩溃.
As it is, that gets me two "HIGHGUI ERROR"s as described above and I get a 640x480 output. I know that 160x120 is a resolution that my camera supports from running v4l2-ctl --list-formats-ext
. If I uncomment the two commented-out lines in the while loop, the program crashes immediately.
这些可能相关或有可能的解决方案: http ://answers.opencv.org/question/11427/decreasing-capture-resolution-of-webcam/, http://answers.opencv.org/question/30062/error-setting-resolution-of-video-capture-device/
These might be related or have possible solutions: http://answers.opencv.org/question/11427/decreasing-capture-resolution-of-webcam/, http://answers.opencv.org/question/30062/error-setting-resolution-of-video-capture-device/
推荐答案
这是OpenCV 2.4(包括2.4.12)的 v4l 版本"(内部版本)中的错误,但该错误不是 libv4l 版本.对于OpenCV 3.1.0,v4l版本和libv4l版本都没有错误.
This is a bug in the v4l "version" (build) of OpenCV 2.4 (including 2.4.12), but the bug is not in the libv4l version. For OpenCV 3.1.0, neither the v4l nor the libv4l version has the bug.
(您的错误错误消息HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP
表示您具有v4l版本;该消息位于cap_v4l.cpp中,请参见代码,但不在cap_libv4l.cpp中.)
(Your error error message HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP
indicates that you have the v4l version; the message is in cap_v4l.cpp, see code, but not in cap_libv4l.cpp.)
要使OpenCV 2.4的v4l版本以640x480以外的固定分辨率工作的解决方法是更改DEFAULT_V4L_WIDTH
和DEFAULT_V4L_HEIGHT
的值modules/highgui/src/cap_v4l.cpp并重新构建OpenCV,对此表示敬意 answer .
A workaround to get the v4l version of OpenCV 2.4 to work at a fixed resolution other than 640x480 ischanging the values for DEFAULT_V4L_WIDTH
and DEFAULT_V4L_HEIGHT
inmodules/highgui/src/cap_v4l.cpp and re-building OpenCV, kudos to thisanswer.
如果您想构建libv4l版本,您可能需要做的就是安装libv4l-dev
并重建OpenCV;默认情况下,我已启用WITH_LIBV4L
.如果不是,则您的cmake命令应包含
If you want to build the libv4l version instead, all you likely need to do isinstall libv4l-dev
and rebuild OpenCV; WITH_LIBV4L
was enabled by default for me. If it is not, your cmake command should contain
-D WITH_LIBV4L=ON
libv4l构建的cmake输出(或version_string.tmp)包含类似内容
The cmake output (or version_string.tmp) for a libv4l build contains something like
Video I/O:
...
V4L/V4L2: Using libv4l1 (ver 0.8.6) / libv4l2 (ver 0.8.6)
(对于v4l构建,它只是V4L/V4L2: NO/YES
.)
(For a v4l build, it is just V4L/V4L2: NO/YES
.)
这篇关于OpenCV:无法设置视频捕获的分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!