Ubuntu Opencv Camera设置V4L2错误不支持曝光控制

时间:2014-05-01 23:36:18

标签: opencv ubuntu v4l2 v4l

我在Ubuntu 12.04LTS中编写了一个简单的Opencv程序,用于调整网络摄像头(Logitech C525)设置,如亮度和曝光等。

它在Windows 7中运行良好,但在Linux中它给了我这个错误

  

$ ./adjust-camera
  初稿完成了   opengl支持可用
  HIGHGUI错误:V4L2:无法获取属性曝光(9963793) - 无效的参数
  HIGHGUI ERROR:V4L:不支持V4L中的曝光控制   HIGHGUI错误:V4L:设备不支持属性曝光(15)
  HIGHGUI错误:V4L:设备不支持属性曝光(15)
  HIGHGUI错误:V4L:属性曝光(15)不受设备支持

另外,如果我启动Ubuntu并插入网络摄像头,我可以使用像Cheese这样的程序打开它。但是如果我用相机运行我的Opencv程序,在关闭Opencv之后,相机将无法再显示在Cheese上。我必须重新启动Ubuntu才能使其与其他任何东西一起使用。

这是代码adjust-camera.cpp

#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"       // Basic OpenCV structures (cv::Mat)
#include "opencv2/highgui/highgui.hpp"  // Video write

#include "string.h"

using namespace cv;
using namespace std;

void calibrateCamera();
void getCameraVals();
VideoCapture cam(0);
//variables for cam camera
int brightness_slider=125;
int contrast_slider=86;
int gain_slider=77;
int saturation_slider=34;
int exposure_slider=6; //exposure range is from 0 to -7. 

int main()
{

namedWindow("camera 1",WINDOW_AUTOSIZE);
Mat frame;
getCameraVals();
createTrackbar("Brightness", "camera 1", &brightness_slider, 255);
createTrackbar("Contrast", "camera 1", &contrast_slider, 255);
createTrackbar("Gain", "camera 1", &gain_slider, 255);
createTrackbar("Saturation", "camera 1", &saturation_slider, 255);
createTrackbar("Exposure", "camera 1", &exposure_slider, 2047);

while(true)
{
    calibrateCamera();
    cam>>frame;
    imshow("camera 1",frame);
    if(waitKey(3)>=0) break;
}
return(0);
}

void getCameraVals()
{
brightness_slider = cam.get(CV_CAP_PROP_BRIGHTNESS);
contrast_slider = cam.get(CV_CAP_PROP_CONTRAST);
gain_slider=cam.get(CV_CAP_PROP_GAIN);
saturation_slider=cam.get(CV_CAP_PROP_SATURATION);
exposure_slider=cam.get(CV_CAP_PROP_EXPOSURE);

}
void calibrateCamera()
{
cam.set(CV_CAP_PROP_BRIGHTNESS, brightness_slider);
cam.set(CV_CAP_PROP_CONTRAST, contrast_slider);
cam.set(CV_CAP_PROP_GAIN, gain_slider);
cam.set(CV_CAP_PROP_SATURATION, saturation_slider);
cam.set(CV_CAP_PROP_EXPOSURE, exposure_slider);
//cam.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
//cam.set(CV_CAP_PROP_FRAME_HEIGHT, 720);
}

这是我用来编译的脚本

![enter image description here][1]ls#!/bin/bash
echo "compiling $1"
if [[ $1 == *.c ]]
then
    gcc -ggdb `pkg-config --cflags opencv` -o `basename $1 .c` $1 `pkg-config --libs opencv`;
elif [[ $1 == *.cpp ]]
then
    g++ -ggdb `pkg-config --cflags opencv` -o `basename $1 .cpp` $1 `pkg-config --libs opencv`;
else
    echo "Please compile only .c or .cpp files"
fi
echo "Output file => ${1%.*}"

以下是相机使用Cheese或在Windows中使用我的Opencv程序显示的内容 Here is what the camera displays with Cheese or in Windows using my Opencv program

这是OpenCV在Ubuntu上显示的内容 Here is what OpenCV displays on Ubuntu

有关如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:1)

问题与您使用的范围有关。 CV属性的值是0到1之间的两倍,但是您使用的值为0到255的轨迹栏... 一种可能的解决方案是将所有内容放在相同的比例上,例如,从0到100。

#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"       // Basic OpenCV structures (cv::Mat)
#include "opencv2/highgui/highgui.hpp"  // Video write

#include "string.h"

using namespace cv;
using namespace std;

void calibrateCamera();
void getCameraVals();
VideoCapture cam(0);
//variables for cam camera
int brightness_slider=25;
int contrast_slider=86;
int gain_slider=77;
int saturation_slider=34;
int exposure_slider=6; //exposure range is from 0 to -7. 

int main()
{

namedWindow("camera 1",WINDOW_AUTOSIZE);
Mat frame;
getCameraVals();
createTrackbar("Brightness", "camera 1", &brightness_slider, 100);
createTrackbar("Contrast", "camera 1", &contrast_slider, 100);
createTrackbar("Gain", "camera 1", &gain_slider, 100);
createTrackbar("Saturation", "camera 1", &saturation_slider, 100);
createTrackbar("Exposure", "camera 1", &exposure_slider, 100);

while(true)
{
    calibrateCamera();
    cam>>frame;
    imshow("camera 1",frame);
    if(waitKey(3)>=0) break;
}
return(0);
}

void getCameraVals()
{
brightness_slider = cam.get(CV_CAP_PROP_BRIGHTNESS)*100;
contrast_slider = cam.get(CV_CAP_PROP_CONTRAST)*100;
gain_slider=cam.get(CV_CAP_PROP_GAIN)*100;
saturation_slider=cam.get(CV_CAP_PROP_SATURATION)*100;
exposure_slider=cam.get(CV_CAP_PROP_EXPOSURE)*100;

}
void calibrateCamera()
{
cam.set(CV_CAP_PROP_BRIGHTNESS, double(brightness_slider/100));
cam.set(CV_CAP_PROP_CONTRAST, double(contrast_slider/100));
cam.set(CV_CAP_PROP_GAIN, double(gain_slider/100));
cam.set(CV_CAP_PROP_SATURATION, double(saturation_slider/100));
cam.set(CV_CAP_PROP_EXPOSURE, double(exposure_slider/100));
//cam.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
//cam.set(CV_CAP_PROP_FRAME_HEIGHT, 720);
}

我希望你觉得它很有用!!!