source: flair-src/trunk/lib/FlairSensorActuator/src/V4LCamera.cpp @ 124

Last change on this file since 124 was 124, checked in by Sanahuja Guillaume, 5 years ago

modifs jpeg

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)<0)
52      Thread::Err("cvSetCaptureProperty error\n");
53  } else if (format == cvimage::Type::Format::YUYV) {
54    if (cvSetCaptureProperty(capture, CV_CAP_PROP_FORMAT, V4L2_PIX_FMT_YUYV) <
55        0)
56      Thread::Err("cvSetCaptureProperty error\n");
57  } else {
58    Thread::Err("format not supported\n");
59  }
60
61  // station sol
62  gain = new DoubleSpinBox(GetGroupBox()->NewRow(), "gain:", 0, 1, 0.1);
63  exposure = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "exposure:", 0,
64                               1, 0.1);
65  bright =
66      new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "bright:", 0, 1, 0.1);
67  contrast = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "contrast:", 0,
68                               1, 0.1);
69  hue = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "hue:", 0, 1, 0.1);
70  sharpness = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "sharpness:",
71                                0, 1, 0.1);
72  sat = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "saturation:", 0, 1,
73                          0.1);
74  autogain = new CheckBox(GetGroupBox()->NewRow(), "autogain:");
75  autoexposure = new CheckBox(GetGroupBox()->LastRowLastCol(), "autoexposure:");
76  awb = new CheckBox(GetGroupBox()->LastRowLastCol(), "awb:");
77  fps = new Label(GetGroupBox()->NewRow(), "fps");
78}
79
80V4LCamera::~V4LCamera() {
81  SafeStop();
82  Join();
83}
84
85void V4LCamera::Run(void) {
86  Time cam_time, new_time, fpsNow, fpsPrev;
87  IplImage *img; // raw image
88  int fpsCounter = 0;
89
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()) {
98    // fps counter
99    fpsCounter++;
100    if (fpsCounter == 100) {
101      fpsNow = GetTime();
102      fps->SetText("fps: %.1f",
103                   fpsCounter / ((float)(fpsNow - fpsPrev) / 1000000000.));
104      fpsCounter = 0;
105      fpsPrev = fpsNow;
106    }
107
108    // cam properties
109    if (gain->ValueChanged() == true && autogain->Value() == false)
110      SetGain(gain->Value());
111    if (exposure->ValueChanged() == true && autoexposure->Value() == false)
112      SetExposure(exposure->Value());
113    if (bright->ValueChanged() == true)
114      SetBrightness(bright->Value());
115    if (sat->ValueChanged() == true)
116      SetSaturation(sat->Value());
117    if (contrast->ValueChanged() == true)
118      SetContrast(contrast->Value());
119    if (hue->ValueChanged() == true)
120      SetHue(hue->Value());
121    if (sharpness->ValueChanged() == true)
122      cvSetCaptureProperty(capture, CV_CAP_PROP_SHARPNESS, sharpness->Value());
123    if (autogain->ValueChanged() == true) {
124      if (autogain->Value() == true) {
125        gain->setEnabled(false);
126      } else {
127        gain->setEnabled(true);
128        SetGain(gain->Value());
129      }
130      SetAutoGain(autogain->Value());
131    }
132    if (autoexposure->ValueChanged() == true) {
133      if (autoexposure->Value() == true) {
134        exposure->setEnabled(false);
135      } else {
136        exposure->setEnabled(true);
137        SetExposure(exposure->Value());
138      }
139      SetAutoExposure(autoexposure->Value());
140    }
141    if (awb->ValueChanged() == true)
142      cvSetCaptureProperty(capture, CV_CAP_PROP_AWB, awb->Value());
143
144    // cam pictures
145    img = cvRetrieveRawFrame(capture);
146    if (!cvGrabFrame(capture)) {
147      Printf("Could not grab a frame\n");
148    }
149    new_time = GetTime();
150
151    output->GetMutex();
152    output->img->imageData = img->imageData;
153    output->ReleaseMutex();
154
155    output->SetDataTime(cam_time);
156    ProcessUpdate(output);
157
158    cam_time = new_time;
159  }
160
161  cvReleaseCapture(&capture);
162}
163
164void V4LCamera::SetAutoGain(bool value) {
165  cvSetCaptureProperty(capture, CV_CAP_PROP_AUTOGAIN, value);
166}
167
168void V4LCamera::SetAutoExposure(bool value) {
169  Thread::Warn("not implemented in opencv\n");
170}
171
172void V4LCamera::SetGain(float value) {
173  cvSetCaptureProperty(capture, CV_CAP_PROP_GAIN, value);
174}
175
176void V4LCamera::SetExposure(float value) {
177  cvSetCaptureProperty(capture, CV_CAP_PROP_EXPOSURE, value);
178}
179
180void V4LCamera::SetBrightness(float value) {
181  cvSetCaptureProperty(capture, CV_CAP_PROP_BRIGHTNESS, value);
182}
183
184void V4LCamera::SetSaturation(float value) {
185  cvSetCaptureProperty(capture, CV_CAP_PROP_SATURATION, value);
186}
187
188void V4LCamera::SetHue(float value) {
189  cvSetCaptureProperty(capture, CV_CAP_PROP_HUE, value);
190}
191
192void V4LCamera::SetContrast(float value) {
193  cvSetCaptureProperty(capture, CV_CAP_PROP_CONTRAST, value);
194}
195
196} // end namespace sensor
197} // end namespace flair
Note: See TracBrowser for help on using the repository browser.