source: flair-src/branches/mavlink/lib/FlairSensorActuator/src/V4LCamera.cpp@ 49

Last change on this file since 49 was 15, checked in by Bayard Gildas, 9 years ago

sources reformatted with flair-format-dir script

File size: 5.9 KB
Line 
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 <GridLayout.h>
23#include <Label.h>
24#include <cvimage.h>
25#include <FrameworkManager.h>
26#include <linux/videodev2.h>
27
28using std::string;
29using namespace flair::core;
30using namespace flair::gui;
31
32namespace flair {
33namespace sensor {
34
35V4LCamera::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
41
42 if (capture < 0)
43 Thread::Err("cvCaptureFromCAM error\n");
44
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");
49
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 }
61
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");
79}
80
81V4LCamera::~V4LCamera() {
82 SafeStop();
83 Join();
84}
85
86void 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 // 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;
107 }
108
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());
144
145 // cam pictures
146 img = cvRetrieveRawFrame(capture);
147 if (!cvGrabFrame(capture)) {
148 Printf("Could not grab a frame\n");
149 }
150 new_time = GetTime();
151
152 output->GetMutex();
153 output->img->imageData = img->imageData;
154 output->ReleaseMutex();
155
156 output->SetDataTime(cam_time);
157 ProcessUpdate(output);
158
159 cam_time = new_time;
160 }
161
162 cvReleaseCapture(&capture);
163}
164
165void V4LCamera::SetAutoGain(bool value) {
166 cvSetCaptureProperty(capture, CV_CAP_PROP_AUTOGAIN, value);
167}
168
169void V4LCamera::SetAutoExposure(bool value) {
170 Thread::Warn("not implemented in opencv\n");
171}
172
173void V4LCamera::SetGain(float value) {
174 cvSetCaptureProperty(capture, CV_CAP_PROP_GAIN, value);
175}
176
177void V4LCamera::SetExposure(float value) {
178 cvSetCaptureProperty(capture, CV_CAP_PROP_EXPOSURE, value);
179}
180
181void V4LCamera::SetBrightness(float value) {
182 cvSetCaptureProperty(capture, CV_CAP_PROP_BRIGHTNESS, value);
183}
184
185void V4LCamera::SetSaturation(float value) {
186 cvSetCaptureProperty(capture, CV_CAP_PROP_SATURATION, value);
187}
188
189void V4LCamera::SetHue(float value) {
190 cvSetCaptureProperty(capture, CV_CAP_PROP_HUE, value);
191}
192
193void V4LCamera::SetContrast(float value) {
194 cvSetCaptureProperty(capture, CV_CAP_PROP_CONTRAST, value);
195}
196
197} // end namespace sensor
198} // end namespace flair
Note: See TracBrowser for help on using the repository browser.