0
votes

I wrote a simple Opencv program in Ubuntu 12.04LTS that adjusts a webcam (Logitech C525) settings like brightness and exposure and such.

It works fine in Windows 7, but in Linux it gives me this error

$ ./adjust-camera
init done
opengl support available
HIGHGUI ERROR: V4L2: Unable to get property Exposure(9963793) - Invalid argument
HIGHGUI ERROR: V4L: Exposure control in V4L is not supported
HIGHGUI ERROR: V4L: Property Exposure(15) not supported by device
HIGHGUI ERROR: V4L: Property Exposure(15) not supported by device
HIGHGUI ERROR: V4L: Property Exposure(15) not supported by device

On a side note, if I bootup Ubuntu and plug in the webcam, I can open it with a program like Cheese. But if I run my Opencv program with the camera, after I close Opencv the camera will no longer be able to show on Cheese. I have to reboot Ubuntu in order for it to work with anything else.

Here is the code 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);
}

Here is the script I use to compile

![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%.*}"

Here is what the camera displays with Cheese or in Windows using my Opencv program Here is what the camera displays with Cheese or in Windows using my Opencv program

Here is what OpenCV displays on Ubuntu Here is what OpenCV displays on Ubuntu

Any ideas on how to fix this?

1
I tried the program on my MacBook's built-in camera. The program ran, but none of the sliders did anything and I didn't get any highgui errors. It looks like the Ubuntu driver for the camera doesn't support the Exposure property and instead of failing quietly it's giving you an error message.SSteve
The webcam support of linux isn't that great and the openCV implementation of the drivers is shakey at best. If you know what you're doing, you could dive into the openCV code and recompile openCV with your fixes. If that isn't your cup of tea, the only option left is to submit a bugreport / feature request.Nallath
What a bummer!!!!!! I would love to help out with the driver stuff but have no idea what to do (well maybe a vague idea), but the issue here is time. I need this ASAP, so...ugh, this is a bust for now.ChumbiChubaGo

1 Answers

1
votes

the problem has to do with the ranges you are using. Values of the CV properties are doubles between 0 and 1 but you are using trackbars with values from 0 to 255... One possible solution si to put everything on the same scale, for example, from 0 to 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);
}

I hope you find it useful!!!