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