[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 <GridLayout.h>
|
---|
| 23 | #include <Label.h>
|
---|
| 24 | #include <cvimage.h>
|
---|
| 25 | #include <FrameworkManager.h>
|
---|
| 26 | #include <linux/videodev2.h>
|
---|
| 27 |
|
---|
| 28 | using std::string;
|
---|
| 29 | using namespace flair::core;
|
---|
| 30 | using namespace flair::gui;
|
---|
| 31 |
|
---|
[15] | 32 | namespace flair {
|
---|
| 33 | namespace sensor {
|
---|
[3] | 34 |
|
---|
[15] | 35 | V4LCamera::V4LCamera(const FrameworkManager *parent, string name,
|
---|
| 36 | uint8_t camera_index, uint16_t width, uint16_t height,
|
---|
| 37 | cvimage::Type::Format format, uint8_t priority)
|
---|
| 38 | : Thread(parent, name, priority),
|
---|
| 39 | Camera(parent, name, width, height, format) {
|
---|
| 40 | capture = cvCaptureFromCAM(camera_index); // a mettre apres l'init dsp
|
---|
[3] | 41 |
|
---|
[15] | 42 | if (capture < 0)
|
---|
| 43 | Thread::Err("cvCaptureFromCAM error\n");
|
---|
[3] | 44 |
|
---|
[15] | 45 | if (cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, width) < 0)
|
---|
| 46 | Thread::Err("cvSetCaptureProperty error\n");
|
---|
| 47 | if (cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, height) < 0)
|
---|
| 48 | Thread::Err("cvSetCaptureProperty error\n");
|
---|
[3] | 49 |
|
---|
[15] | 50 | if (format == cvimage::Type::Format::UYVY) {
|
---|
| 51 | if (cvSetCaptureProperty(capture, CV_CAP_PROP_FORMAT, V4L2_PIX_FMT_UYVY) <
|
---|
| 52 | 0)
|
---|
| 53 | Thread::Err("cvSetCaptureProperty error\n");
|
---|
| 54 | } else if (format == cvimage::Type::Format::YUYV) {
|
---|
| 55 | if (cvSetCaptureProperty(capture, CV_CAP_PROP_FORMAT, V4L2_PIX_FMT_YUYV) <
|
---|
| 56 | 0)
|
---|
| 57 | Thread::Err("cvSetCaptureProperty error\n");
|
---|
| 58 | } else {
|
---|
| 59 | Thread::Err("format not supported\n");
|
---|
| 60 | }
|
---|
[3] | 61 |
|
---|
[15] | 62 | // station sol
|
---|
| 63 | gain = new DoubleSpinBox(GetGroupBox()->NewRow(), "gain:", 0, 1, 0.1);
|
---|
| 64 | exposure = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "exposure:", 0,
|
---|
| 65 | 1, 0.1);
|
---|
| 66 | bright =
|
---|
| 67 | new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "bright:", 0, 1, 0.1);
|
---|
| 68 | contrast = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "contrast:", 0,
|
---|
| 69 | 1, 0.1);
|
---|
| 70 | hue = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "hue:", 0, 1, 0.1);
|
---|
| 71 | sharpness = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "sharpness:",
|
---|
| 72 | 0, 1, 0.1);
|
---|
| 73 | sat = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "saturation:", 0, 1,
|
---|
| 74 | 0.1);
|
---|
| 75 | autogain = new CheckBox(GetGroupBox()->NewRow(), "autogain:");
|
---|
| 76 | autoexposure = new CheckBox(GetGroupBox()->LastRowLastCol(), "autoexposure:");
|
---|
| 77 | awb = new CheckBox(GetGroupBox()->LastRowLastCol(), "awb:");
|
---|
| 78 | fps = new Label(GetGroupBox()->NewRow(), "fps");
|
---|
[3] | 79 | }
|
---|
| 80 |
|
---|
[15] | 81 | V4LCamera::~V4LCamera() {
|
---|
| 82 | SafeStop();
|
---|
| 83 | Join();
|
---|
[3] | 84 | }
|
---|
| 85 |
|
---|
[15] | 86 | void V4LCamera::Run(void) {
|
---|
| 87 | Time cam_time, new_time, fpsNow, fpsPrev;
|
---|
| 88 | IplImage *img; // raw image
|
---|
| 89 | int fpsCounter = 0;
|
---|
[3] | 90 |
|
---|
[15] | 91 | // init image old
|
---|
| 92 | if (!cvGrabFrame(capture)) {
|
---|
| 93 | Printf("Could not grab a frame\n");
|
---|
| 94 | }
|
---|
| 95 | cam_time = GetTime();
|
---|
| 96 | fpsPrev = cam_time;
|
---|
| 97 |
|
---|
| 98 | while (!ToBeStopped()) {
|
---|
| 99 | // fps counter
|
---|
| 100 | fpsCounter++;
|
---|
| 101 | if (fpsCounter == 100) {
|
---|
| 102 | fpsNow = GetTime();
|
---|
| 103 | fps->SetText("fps: %.1f",
|
---|
| 104 | fpsCounter / ((float)(fpsNow - fpsPrev) / 1000000000.));
|
---|
| 105 | fpsCounter = 0;
|
---|
| 106 | fpsPrev = fpsNow;
|
---|
[3] | 107 | }
|
---|
| 108 |
|
---|
[15] | 109 | // cam properties
|
---|
| 110 | if (gain->ValueChanged() == true && autogain->Value() == false)
|
---|
| 111 | SetGain(gain->Value());
|
---|
| 112 | if (exposure->ValueChanged() == true && autoexposure->Value() == false)
|
---|
| 113 | SetExposure(exposure->Value());
|
---|
| 114 | if (bright->ValueChanged() == true)
|
---|
| 115 | SetBrightness(bright->Value());
|
---|
| 116 | if (sat->ValueChanged() == true)
|
---|
| 117 | SetSaturation(sat->Value());
|
---|
| 118 | if (contrast->ValueChanged() == true)
|
---|
| 119 | SetContrast(contrast->Value());
|
---|
| 120 | if (hue->ValueChanged() == true)
|
---|
| 121 | SetHue(hue->Value());
|
---|
| 122 | if (sharpness->ValueChanged() == true)
|
---|
| 123 | cvSetCaptureProperty(capture, CV_CAP_PROP_SHARPNESS, sharpness->Value());
|
---|
| 124 | if (autogain->ValueChanged() == true) {
|
---|
| 125 | if (autogain->Value() == true) {
|
---|
| 126 | gain->setEnabled(false);
|
---|
| 127 | } else {
|
---|
| 128 | gain->setEnabled(true);
|
---|
| 129 | SetGain(gain->Value());
|
---|
| 130 | }
|
---|
| 131 | SetAutoGain(autogain->Value());
|
---|
| 132 | }
|
---|
| 133 | if (autoexposure->ValueChanged() == true) {
|
---|
| 134 | if (autoexposure->Value() == true) {
|
---|
| 135 | exposure->setEnabled(false);
|
---|
| 136 | } else {
|
---|
| 137 | exposure->setEnabled(true);
|
---|
| 138 | SetExposure(exposure->Value());
|
---|
| 139 | }
|
---|
| 140 | SetAutoExposure(autoexposure->Value());
|
---|
| 141 | }
|
---|
| 142 | if (awb->ValueChanged() == true)
|
---|
| 143 | cvSetCaptureProperty(capture, CV_CAP_PROP_AWB, awb->Value());
|
---|
[3] | 144 |
|
---|
[15] | 145 | // cam pictures
|
---|
| 146 | img = cvRetrieveRawFrame(capture);
|
---|
| 147 | if (!cvGrabFrame(capture)) {
|
---|
| 148 | Printf("Could not grab a frame\n");
|
---|
| 149 | }
|
---|
| 150 | new_time = GetTime();
|
---|
[3] | 151 |
|
---|
[15] | 152 | output->GetMutex();
|
---|
| 153 | output->img->imageData = img->imageData;
|
---|
| 154 | output->ReleaseMutex();
|
---|
[3] | 155 |
|
---|
[15] | 156 | output->SetDataTime(cam_time);
|
---|
| 157 | ProcessUpdate(output);
|
---|
[3] | 158 |
|
---|
[15] | 159 | cam_time = new_time;
|
---|
| 160 | }
|
---|
[3] | 161 |
|
---|
[15] | 162 | cvReleaseCapture(&capture);
|
---|
[3] | 163 | }
|
---|
| 164 |
|
---|
[15] | 165 | void V4LCamera::SetAutoGain(bool value) {
|
---|
| 166 | cvSetCaptureProperty(capture, CV_CAP_PROP_AUTOGAIN, value);
|
---|
[3] | 167 | }
|
---|
| 168 |
|
---|
[15] | 169 | void V4LCamera::SetAutoExposure(bool value) {
|
---|
| 170 | Thread::Warn("not implemented in opencv\n");
|
---|
[3] | 171 | }
|
---|
| 172 |
|
---|
[15] | 173 | void V4LCamera::SetGain(float value) {
|
---|
| 174 | cvSetCaptureProperty(capture, CV_CAP_PROP_GAIN, value);
|
---|
[3] | 175 | }
|
---|
| 176 |
|
---|
[15] | 177 | void V4LCamera::SetExposure(float value) {
|
---|
| 178 | cvSetCaptureProperty(capture, CV_CAP_PROP_EXPOSURE, value);
|
---|
[3] | 179 | }
|
---|
| 180 |
|
---|
[15] | 181 | void V4LCamera::SetBrightness(float value) {
|
---|
| 182 | cvSetCaptureProperty(capture, CV_CAP_PROP_BRIGHTNESS, value);
|
---|
[3] | 183 | }
|
---|
| 184 |
|
---|
[15] | 185 | void V4LCamera::SetSaturation(float value) {
|
---|
| 186 | cvSetCaptureProperty(capture, CV_CAP_PROP_SATURATION, value);
|
---|
[3] | 187 | }
|
---|
| 188 |
|
---|
[15] | 189 | void V4LCamera::SetHue(float value) {
|
---|
| 190 | cvSetCaptureProperty(capture, CV_CAP_PROP_HUE, value);
|
---|
[3] | 191 | }
|
---|
| 192 |
|
---|
[15] | 193 | void V4LCamera::SetContrast(float value) {
|
---|
| 194 | cvSetCaptureProperty(capture, CV_CAP_PROP_CONTRAST, value);
|
---|
[3] | 195 | }
|
---|
| 196 |
|
---|
| 197 | } // end namespace sensor
|
---|
| 198 | } // end namespace flair
|
---|