[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++;
|
---|
[307] | 106 | if (GetTime() > (fpsPrev + 5 * (Time)1000000000)) {
|
---|
| 107 | // toute les 5 secondes
|
---|
[15] | 108 | fpsNow = GetTime();
|
---|
| 109 | fps->SetText("fps: %.1f",
|
---|
[307] | 110 | fpsCounter / ((float)(fpsNow - fpsPrev) / 1000000000.));
|
---|
[15] | 111 | fpsCounter = 0;
|
---|
| 112 | fpsPrev = fpsNow;
|
---|
[307] | 113 | }
|
---|
| 114 |
|
---|
[15] | 115 | // cam properties
|
---|
| 116 | if (gain->ValueChanged() == true && autogain->Value() == false)
|
---|
| 117 | SetGain(gain->Value());
|
---|
| 118 | if (exposure->ValueChanged() == true && autoexposure->Value() == false)
|
---|
| 119 | SetExposure(exposure->Value());
|
---|
| 120 | if (bright->ValueChanged() == true)
|
---|
| 121 | SetBrightness(bright->Value());
|
---|
| 122 | if (sat->ValueChanged() == true)
|
---|
| 123 | SetSaturation(sat->Value());
|
---|
| 124 | if (contrast->ValueChanged() == true)
|
---|
| 125 | SetContrast(contrast->Value());
|
---|
| 126 | if (hue->ValueChanged() == true)
|
---|
| 127 | SetHue(hue->Value());
|
---|
| 128 | if (sharpness->ValueChanged() == true)
|
---|
| 129 | cvSetCaptureProperty(capture, CV_CAP_PROP_SHARPNESS, sharpness->Value());
|
---|
| 130 | if (autogain->ValueChanged() == true) {
|
---|
| 131 | if (autogain->Value() == true) {
|
---|
| 132 | gain->setEnabled(false);
|
---|
| 133 | } else {
|
---|
| 134 | gain->setEnabled(true);
|
---|
| 135 | SetGain(gain->Value());
|
---|
| 136 | }
|
---|
| 137 | SetAutoGain(autogain->Value());
|
---|
| 138 | }
|
---|
| 139 | if (autoexposure->ValueChanged() == true) {
|
---|
| 140 | if (autoexposure->Value() == true) {
|
---|
| 141 | exposure->setEnabled(false);
|
---|
| 142 | } else {
|
---|
| 143 | exposure->setEnabled(true);
|
---|
| 144 | SetExposure(exposure->Value());
|
---|
| 145 | }
|
---|
| 146 | SetAutoExposure(autoexposure->Value());
|
---|
| 147 | }
|
---|
| 148 | if (awb->ValueChanged() == true)
|
---|
| 149 | cvSetCaptureProperty(capture, CV_CAP_PROP_AWB, awb->Value());
|
---|
[3] | 150 |
|
---|
[15] | 151 | // cam pictures
|
---|
| 152 | img = cvRetrieveRawFrame(capture);
|
---|
| 153 | if (!cvGrabFrame(capture)) {
|
---|
| 154 | Printf("Could not grab a frame\n");
|
---|
| 155 | }
|
---|
| 156 | new_time = GetTime();
|
---|
[165] | 157 |
|
---|
| 158 | //check for ps3eye deconnection in hds uav
|
---|
| 159 | if(new_time-cam_time>100*1000*1000) {
|
---|
| 160 | Thread::Warn("delta trop grand\n");
|
---|
| 161 | hasProblems=true;
|
---|
| 162 | }
|
---|
[3] | 163 |
|
---|
[15] | 164 | output->GetMutex();
|
---|
| 165 | output->img->imageData = img->imageData;
|
---|
| 166 | output->ReleaseMutex();
|
---|
[3] | 167 |
|
---|
[15] | 168 | output->SetDataTime(cam_time);
|
---|
| 169 | ProcessUpdate(output);
|
---|
[3] | 170 |
|
---|
[15] | 171 | cam_time = new_time;
|
---|
| 172 | }
|
---|
[3] | 173 |
|
---|
[15] | 174 | cvReleaseCapture(&capture);
|
---|
[3] | 175 | }
|
---|
| 176 |
|
---|
[165] | 177 | bool V4LCamera::HasProblems(void) {
|
---|
| 178 | return hasProblems;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[15] | 181 | void V4LCamera::SetAutoGain(bool value) {
|
---|
| 182 | cvSetCaptureProperty(capture, CV_CAP_PROP_AUTOGAIN, value);
|
---|
[3] | 183 | }
|
---|
| 184 |
|
---|
[15] | 185 | void V4LCamera::SetAutoExposure(bool value) {
|
---|
| 186 | Thread::Warn("not implemented in opencv\n");
|
---|
[3] | 187 | }
|
---|
| 188 |
|
---|
[15] | 189 | void V4LCamera::SetGain(float value) {
|
---|
| 190 | cvSetCaptureProperty(capture, CV_CAP_PROP_GAIN, value);
|
---|
[3] | 191 | }
|
---|
| 192 |
|
---|
[15] | 193 | void V4LCamera::SetExposure(float value) {
|
---|
| 194 | cvSetCaptureProperty(capture, CV_CAP_PROP_EXPOSURE, value);
|
---|
[3] | 195 | }
|
---|
| 196 |
|
---|
[15] | 197 | void V4LCamera::SetBrightness(float value) {
|
---|
| 198 | cvSetCaptureProperty(capture, CV_CAP_PROP_BRIGHTNESS, value);
|
---|
[3] | 199 | }
|
---|
| 200 |
|
---|
[15] | 201 | void V4LCamera::SetSaturation(float value) {
|
---|
| 202 | cvSetCaptureProperty(capture, CV_CAP_PROP_SATURATION, value);
|
---|
[3] | 203 | }
|
---|
| 204 |
|
---|
[15] | 205 | void V4LCamera::SetHue(float value) {
|
---|
| 206 | cvSetCaptureProperty(capture, CV_CAP_PROP_HUE, value);
|
---|
[3] | 207 | }
|
---|
| 208 |
|
---|
[15] | 209 | void V4LCamera::SetContrast(float value) {
|
---|
| 210 | cvSetCaptureProperty(capture, CV_CAP_PROP_CONTRAST, value);
|
---|
[3] | 211 | }
|
---|
| 212 |
|
---|
| 213 | } // end namespace sensor
|
---|
| 214 | } // end namespace flair
|
---|