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 | hasProblems=false;
|
---|
79 | }
|
---|
80 |
|
---|
81 | V4LCamera::~V4LCamera() {
|
---|
82 | SafeStop();
|
---|
83 | Join();
|
---|
84 | }
|
---|
85 |
|
---|
86 | void V4LCamera::Run(void) {
|
---|
87 | Time cam_time, new_time, fpsNow, fpsPrev;
|
---|
88 | IplImage *img; // raw image
|
---|
89 | int fpsCounter = 0;
|
---|
90 |
|
---|
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 | //check for ps3eye deconnection in hds uav
|
---|
100 | if(cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH)<0) {
|
---|
101 | Thread::Warn("camera disconnected\n");
|
---|
102 | hasProblems=true;
|
---|
103 | }
|
---|
104 |
|
---|
105 | // fps counter
|
---|
106 | fpsCounter++;
|
---|
107 | if (fpsCounter == 100) {
|
---|
108 | fpsNow = GetTime();
|
---|
109 | fps->SetText("fps: %.1f",
|
---|
110 | fpsCounter / ((float)(fpsNow - fpsPrev) / 1000000000.));
|
---|
111 | fpsCounter = 0;
|
---|
112 | fpsPrev = fpsNow;
|
---|
113 | }
|
---|
114 |
|
---|
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());
|
---|
150 |
|
---|
151 | // cam pictures
|
---|
152 | img = cvRetrieveRawFrame(capture);
|
---|
153 | if (!cvGrabFrame(capture)) {
|
---|
154 | Printf("Could not grab a frame\n");
|
---|
155 | }
|
---|
156 | new_time = GetTime();
|
---|
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 | }
|
---|
163 |
|
---|
164 | output->GetMutex();
|
---|
165 | output->img->imageData = img->imageData;
|
---|
166 | output->ReleaseMutex();
|
---|
167 |
|
---|
168 | output->SetDataTime(cam_time);
|
---|
169 | ProcessUpdate(output);
|
---|
170 |
|
---|
171 | cam_time = new_time;
|
---|
172 | }
|
---|
173 |
|
---|
174 | cvReleaseCapture(&capture);
|
---|
175 | }
|
---|
176 |
|
---|
177 | bool V4LCamera::HasProblems(void) {
|
---|
178 | return hasProblems;
|
---|
179 | }
|
---|
180 |
|
---|
181 | void V4LCamera::SetAutoGain(bool value) {
|
---|
182 | cvSetCaptureProperty(capture, CV_CAP_PROP_AUTOGAIN, value);
|
---|
183 | }
|
---|
184 |
|
---|
185 | void V4LCamera::SetAutoExposure(bool value) {
|
---|
186 | Thread::Warn("not implemented in opencv\n");
|
---|
187 | }
|
---|
188 |
|
---|
189 | void V4LCamera::SetGain(float value) {
|
---|
190 | cvSetCaptureProperty(capture, CV_CAP_PROP_GAIN, value);
|
---|
191 | }
|
---|
192 |
|
---|
193 | void V4LCamera::SetExposure(float value) {
|
---|
194 | cvSetCaptureProperty(capture, CV_CAP_PROP_EXPOSURE, value);
|
---|
195 | }
|
---|
196 |
|
---|
197 | void V4LCamera::SetBrightness(float value) {
|
---|
198 | cvSetCaptureProperty(capture, CV_CAP_PROP_BRIGHTNESS, value);
|
---|
199 | }
|
---|
200 |
|
---|
201 | void V4LCamera::SetSaturation(float value) {
|
---|
202 | cvSetCaptureProperty(capture, CV_CAP_PROP_SATURATION, value);
|
---|
203 | }
|
---|
204 |
|
---|
205 | void V4LCamera::SetHue(float value) {
|
---|
206 | cvSetCaptureProperty(capture, CV_CAP_PROP_HUE, value);
|
---|
207 | }
|
---|
208 |
|
---|
209 | void V4LCamera::SetContrast(float value) {
|
---|
210 | cvSetCaptureProperty(capture, CV_CAP_PROP_CONTRAST, value);
|
---|
211 | }
|
---|
212 |
|
---|
213 | } // end namespace sensor
|
---|
214 | } // end namespace flair
|
---|