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