[3] | 1 | // %flair:license{
|
---|
[15] | 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
[3] | 4 | // %flair:license}
|
---|
| 5 | // created: 2014/07/17
|
---|
| 6 | // filename: V4LCamera.cpp
|
---|
| 7 | //
|
---|
| 8 | // author: Guillaume Sanahuja
|
---|
| 9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 10 | //
|
---|
| 11 | // version: $Id: $
|
---|
| 12 | //
|
---|
| 13 | // purpose: base class for V4l camera
|
---|
| 14 | //
|
---|
| 15 | //
|
---|
| 16 | /*********************************************************************/
|
---|
| 17 |
|
---|
| 18 | #include "V4LCamera.h"
|
---|
| 19 | #include <GroupBox.h>
|
---|
| 20 | #include <DoubleSpinBox.h>
|
---|
| 21 | #include <CheckBox.h>
|
---|
| 22 | #include <Label.h>
|
---|
| 23 | #include <cvimage.h>
|
---|
| 24 | #include <FrameworkManager.h>
|
---|
| 25 | #include <linux/videodev2.h>
|
---|
| 26 |
|
---|
| 27 | using std::string;
|
---|
| 28 | using namespace flair::core;
|
---|
| 29 | using namespace flair::gui;
|
---|
| 30 |
|
---|
[15] | 31 | namespace flair {
|
---|
| 32 | namespace sensor {
|
---|
[3] | 33 |
|
---|
[137] | 34 | V4LCamera::V4LCamera(string name,
|
---|
[15] | 35 | uint8_t camera_index, uint16_t width, uint16_t height,
|
---|
| 36 | cvimage::Type::Format format, uint8_t priority)
|
---|
[137] | 37 | : Thread(getFrameworkManager(), name, priority),
|
---|
| 38 | Camera(name, width, height, format) {
|
---|
[169] | 39 | capture = cvCaptureFromCAM(camera_index);
|
---|
[15] | 40 | if (capture < 0)
|
---|
| 41 | Thread::Err("cvCaptureFromCAM error\n");
|
---|
[3] | 42 |
|
---|
[124] | 43 | if (cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, width)<0)
|
---|
[15] | 44 | Thread::Err("cvSetCaptureProperty error\n");
|
---|
[124] | 45 | if (cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, height)<0)
|
---|
[15] | 46 | Thread::Err("cvSetCaptureProperty error\n");
|
---|
[3] | 47 |
|
---|
[15] | 48 | if (format == cvimage::Type::Format::UYVY) {
|
---|
[124] | 49 | if (cvSetCaptureProperty(capture, CV_CAP_PROP_FORMAT, V4L2_PIX_FMT_UYVY)<0)
|
---|
[15] | 50 | Thread::Err("cvSetCaptureProperty error\n");
|
---|
| 51 | } else if (format == cvimage::Type::Format::YUYV) {
|
---|
| 52 | if (cvSetCaptureProperty(capture, CV_CAP_PROP_FORMAT, V4L2_PIX_FMT_YUYV) <
|
---|
| 53 | 0)
|
---|
| 54 | Thread::Err("cvSetCaptureProperty error\n");
|
---|
| 55 | } else {
|
---|
| 56 | Thread::Err("format not supported\n");
|
---|
| 57 | }
|
---|
[3] | 58 |
|
---|
[15] | 59 | // station sol
|
---|
| 60 | gain = new DoubleSpinBox(GetGroupBox()->NewRow(), "gain:", 0, 1, 0.1);
|
---|
| 61 | exposure = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "exposure:", 0,
|
---|
| 62 | 1, 0.1);
|
---|
| 63 | bright =
|
---|
| 64 | new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "bright:", 0, 1, 0.1);
|
---|
| 65 | contrast = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "contrast:", 0,
|
---|
| 66 | 1, 0.1);
|
---|
| 67 | hue = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "hue:", 0, 1, 0.1);
|
---|
| 68 | sharpness = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "sharpness:",
|
---|
| 69 | 0, 1, 0.1);
|
---|
| 70 | sat = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "saturation:", 0, 1,
|
---|
| 71 | 0.1);
|
---|
| 72 | autogain = new CheckBox(GetGroupBox()->NewRow(), "autogain:");
|
---|
| 73 | autoexposure = new CheckBox(GetGroupBox()->LastRowLastCol(), "autoexposure:");
|
---|
| 74 | awb = new CheckBox(GetGroupBox()->LastRowLastCol(), "awb:");
|
---|
| 75 | fps = new Label(GetGroupBox()->NewRow(), "fps");
|
---|
[165] | 76 |
|
---|
| 77 | hasProblems=false;
|
---|
[3] | 78 | }
|
---|
| 79 |
|
---|
[15] | 80 | V4LCamera::~V4LCamera() {
|
---|
| 81 | SafeStop();
|
---|
| 82 | Join();
|
---|
[3] | 83 | }
|
---|
| 84 |
|
---|
[15] | 85 | void V4LCamera::Run(void) {
|
---|
| 86 | Time cam_time, new_time, fpsNow, fpsPrev;
|
---|
| 87 | IplImage *img; // raw image
|
---|
| 88 | int fpsCounter = 0;
|
---|
[3] | 89 |
|
---|
[15] | 90 | // init image old
|
---|
| 91 | if (!cvGrabFrame(capture)) {
|
---|
| 92 | Printf("Could not grab a frame\n");
|
---|
| 93 | }
|
---|
| 94 | cam_time = GetTime();
|
---|
| 95 | fpsPrev = cam_time;
|
---|
| 96 |
|
---|
| 97 | while (!ToBeStopped()) {
|
---|
[165] | 98 | //check for ps3eye deconnection in hds uav
|
---|
| 99 | if(cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH)<0) {
|
---|
| 100 | Thread::Warn("camera disconnected\n");
|
---|
| 101 | hasProblems=true;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[15] | 104 | // fps counter
|
---|
| 105 | fpsCounter++;
|
---|
| 106 | if (fpsCounter == 100) {
|
---|
| 107 | fpsNow = GetTime();
|
---|
| 108 | fps->SetText("fps: %.1f",
|
---|
[165] | 109 | fpsCounter / ((float)(fpsNow - fpsPrev) / 1000000000.));
|
---|
[15] | 110 | fpsCounter = 0;
|
---|
| 111 | fpsPrev = fpsNow;
|
---|
[3] | 112 | }
|
---|
| 113 |
|
---|
[15] | 114 | // cam properties
|
---|
| 115 | if (gain->ValueChanged() == true && autogain->Value() == false)
|
---|
| 116 | SetGain(gain->Value());
|
---|
| 117 | if (exposure->ValueChanged() == true && autoexposure->Value() == false)
|
---|
| 118 | SetExposure(exposure->Value());
|
---|
| 119 | if (bright->ValueChanged() == true)
|
---|
| 120 | SetBrightness(bright->Value());
|
---|
| 121 | if (sat->ValueChanged() == true)
|
---|
| 122 | SetSaturation(sat->Value());
|
---|
| 123 | if (contrast->ValueChanged() == true)
|
---|
| 124 | SetContrast(contrast->Value());
|
---|
| 125 | if (hue->ValueChanged() == true)
|
---|
| 126 | SetHue(hue->Value());
|
---|
| 127 | if (sharpness->ValueChanged() == true)
|
---|
| 128 | cvSetCaptureProperty(capture, CV_CAP_PROP_SHARPNESS, sharpness->Value());
|
---|
| 129 | if (autogain->ValueChanged() == true) {
|
---|
| 130 | if (autogain->Value() == true) {
|
---|
| 131 | gain->setEnabled(false);
|
---|
| 132 | } else {
|
---|
| 133 | gain->setEnabled(true);
|
---|
| 134 | SetGain(gain->Value());
|
---|
| 135 | }
|
---|
| 136 | SetAutoGain(autogain->Value());
|
---|
| 137 | }
|
---|
| 138 | if (autoexposure->ValueChanged() == true) {
|
---|
| 139 | if (autoexposure->Value() == true) {
|
---|
| 140 | exposure->setEnabled(false);
|
---|
| 141 | } else {
|
---|
| 142 | exposure->setEnabled(true);
|
---|
| 143 | SetExposure(exposure->Value());
|
---|
| 144 | }
|
---|
| 145 | SetAutoExposure(autoexposure->Value());
|
---|
| 146 | }
|
---|
| 147 | if (awb->ValueChanged() == true)
|
---|
| 148 | cvSetCaptureProperty(capture, CV_CAP_PROP_AWB, awb->Value());
|
---|
[3] | 149 |
|
---|
[15] | 150 | // cam pictures
|
---|
| 151 | img = cvRetrieveRawFrame(capture);
|
---|
| 152 | if (!cvGrabFrame(capture)) {
|
---|
| 153 | Printf("Could not grab a frame\n");
|
---|
| 154 | }
|
---|
| 155 | new_time = GetTime();
|
---|
[165] | 156 |
|
---|
| 157 | //check for ps3eye deconnection in hds uav
|
---|
| 158 | if(new_time-cam_time>100*1000*1000) {
|
---|
| 159 | Thread::Warn("delta trop grand\n");
|
---|
| 160 | hasProblems=true;
|
---|
| 161 | }
|
---|
[3] | 162 |
|
---|
[15] | 163 | output->GetMutex();
|
---|
| 164 | output->img->imageData = img->imageData;
|
---|
| 165 | output->ReleaseMutex();
|
---|
[3] | 166 |
|
---|
[15] | 167 | output->SetDataTime(cam_time);
|
---|
| 168 | ProcessUpdate(output);
|
---|
[3] | 169 |
|
---|
[15] | 170 | cam_time = new_time;
|
---|
| 171 | }
|
---|
[3] | 172 |
|
---|
[15] | 173 | cvReleaseCapture(&capture);
|
---|
[3] | 174 | }
|
---|
| 175 |
|
---|
[165] | 176 | bool V4LCamera::HasProblems(void) {
|
---|
| 177 | return hasProblems;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[15] | 180 | void V4LCamera::SetAutoGain(bool value) {
|
---|
| 181 | cvSetCaptureProperty(capture, CV_CAP_PROP_AUTOGAIN, value);
|
---|
[3] | 182 | }
|
---|
| 183 |
|
---|
[15] | 184 | void V4LCamera::SetAutoExposure(bool value) {
|
---|
| 185 | Thread::Warn("not implemented in opencv\n");
|
---|
[3] | 186 | }
|
---|
| 187 |
|
---|
[15] | 188 | void V4LCamera::SetGain(float value) {
|
---|
| 189 | cvSetCaptureProperty(capture, CV_CAP_PROP_GAIN, value);
|
---|
[3] | 190 | }
|
---|
| 191 |
|
---|
[15] | 192 | void V4LCamera::SetExposure(float value) {
|
---|
| 193 | cvSetCaptureProperty(capture, CV_CAP_PROP_EXPOSURE, value);
|
---|
[3] | 194 | }
|
---|
| 195 |
|
---|
[15] | 196 | void V4LCamera::SetBrightness(float value) {
|
---|
| 197 | cvSetCaptureProperty(capture, CV_CAP_PROP_BRIGHTNESS, value);
|
---|
[3] | 198 | }
|
---|
| 199 |
|
---|
[15] | 200 | void V4LCamera::SetSaturation(float value) {
|
---|
| 201 | cvSetCaptureProperty(capture, CV_CAP_PROP_SATURATION, value);
|
---|
[3] | 202 | }
|
---|
| 203 |
|
---|
[15] | 204 | void V4LCamera::SetHue(float value) {
|
---|
| 205 | cvSetCaptureProperty(capture, CV_CAP_PROP_HUE, value);
|
---|
[3] | 206 | }
|
---|
| 207 |
|
---|
[15] | 208 | void V4LCamera::SetContrast(float value) {
|
---|
| 209 | cvSetCaptureProperty(capture, CV_CAP_PROP_CONTRAST, value);
|
---|
[3] | 210 | }
|
---|
| 211 |
|
---|
| 212 | } // end namespace sensor
|
---|
| 213 | } // end namespace flair
|
---|