1 | // created: 2012/04/12
|
---|
2 | // filename: OpticalFlowSpeed.cpp
|
---|
3 | //
|
---|
4 | // author: Guillaume Sanahuja
|
---|
5 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
6 | //
|
---|
7 | // version: $Id: $
|
---|
8 | //
|
---|
9 | // purpose: calcul de la vitesse à partir du flux optique
|
---|
10 | //
|
---|
11 | //
|
---|
12 | /*********************************************************************/
|
---|
13 |
|
---|
14 | #include "OpticalFlowSpeed.h"
|
---|
15 | #include "OpticalFlowData.h"
|
---|
16 | #include <Matrix.h>
|
---|
17 | #include <Object.h>
|
---|
18 | #include <Layout.h>
|
---|
19 | #include <GroupBox.h>
|
---|
20 | #include <SpinBox.h>
|
---|
21 | #include <CheckBox.h>
|
---|
22 | #include <DoubleSpinBox.h>
|
---|
23 |
|
---|
24 | using std::string;
|
---|
25 | using namespace flair::core;
|
---|
26 | using namespace flair::gui;
|
---|
27 |
|
---|
28 | namespace flair { namespace filter {
|
---|
29 |
|
---|
30 | OpticalFlowSpeed::OpticalFlowSpeed(const IODevice* parent, const LayoutPosition* position,string name) : IODevice(parent,name) {
|
---|
31 | cvmatrix_descriptor* desc=new cvmatrix_descriptor(2,1);
|
---|
32 | desc->SetElementName(0,0,"vx");
|
---|
33 | desc->SetElementName(1,0,"vy");
|
---|
34 | output=new Matrix(this,desc,floatType,name);
|
---|
35 | delete desc;
|
---|
36 |
|
---|
37 | AddDataToLog(output);
|
---|
38 |
|
---|
39 | GroupBox* reglages_groupbox=new GroupBox(position,name);
|
---|
40 | quality=new DoubleSpinBox(reglages_groupbox->LastRowLastCol(),"optical flow quality:",0.,100.,1.,1,5.);
|
---|
41 | weightedAverage=new CheckBox(reglages_groupbox->LastRowLastCol(),"Weighted average", true);
|
---|
42 | timeMultiplication=new CheckBox(reglages_groupbox->LastRowLastCol(),"Time multiplication", true);
|
---|
43 | }
|
---|
44 |
|
---|
45 | OpticalFlowSpeed::~OpticalFlowSpeed(void) { }
|
---|
46 |
|
---|
47 | void OpticalFlowSpeed::UpdateFrom(const io_data *data) {
|
---|
48 | OpticalFlowData *input=(OpticalFlowData*)data;
|
---|
49 | float deplx,deply;
|
---|
50 | float nb_depl=0;
|
---|
51 |
|
---|
52 | deplx=0;
|
---|
53 | deply=0;
|
---|
54 |
|
---|
55 | //error is 0 if perfect match and 7x7x255x255 at worst
|
---|
56 | float qualityThreshold=quality->Value()/100.*7*7*255*255;
|
---|
57 | input->GetMutex();
|
---|
58 | int nbUsedPoints=0;
|
---|
59 | for(int i=0;i<input->NbFeatures();i++) {
|
---|
60 | //if point is found in both images and quality is sufficient
|
---|
61 | if((input->FoundFeature()[i]!=0)&&(input->FeatureError()[i]<qualityThreshold)) {
|
---|
62 | nbUsedPoints++;
|
---|
63 | float qualityFactor=1.0;
|
---|
64 | if (weightedAverage->Value()) {
|
---|
65 | //displacement is weigthed by quality
|
---|
66 | qualityFactor/=(1+input->FeatureError()[i]);
|
---|
67 | }
|
---|
68 | deplx+=(input->PointsB()[i].x-input->PointsA()[i].x)*qualityFactor;
|
---|
69 | deply+=(input->PointsB()[i].y-input->PointsA()[i].y)*qualityFactor;
|
---|
70 | nb_depl+=qualityFactor;
|
---|
71 | }
|
---|
72 | }
|
---|
73 | input->ReleaseMutex();
|
---|
74 | float deltaT;
|
---|
75 | if (timeMultiplication->Value()) deltaT=(float)(data->DataTime()-output->DataTime())/(1000.*1000.*1000.);
|
---|
76 | else deltaT=1.;
|
---|
77 | output->SetDataTime(data->DataTime());
|
---|
78 |
|
---|
79 | if(nb_depl!=0) {
|
---|
80 | //Printf("Nombre de points=%d/%d (nb_depl=%f,pondération=%d), deltaT=%f\n",nbUsedPoints,input->NbFeatures(),nb_depl,weightedAverage->Value(),deltaT);
|
---|
81 | output->SetValue(0,0,deplx/(nb_depl*deltaT));
|
---|
82 | output->SetValue(1,0,deply/(nb_depl*deltaT));
|
---|
83 | }
|
---|
84 | // output->SetDataTime(data->DataTime());
|
---|
85 | ProcessUpdate(output);
|
---|
86 | }
|
---|
87 |
|
---|
88 | float OpticalFlowSpeed::Vx(void) const {
|
---|
89 | return output->Value(0,0);
|
---|
90 | }
|
---|
91 |
|
---|
92 | float OpticalFlowSpeed::Vy(void) const {
|
---|
93 | return output->Value(1,0);
|
---|
94 | }
|
---|
95 |
|
---|
96 | core::Matrix *OpticalFlowSpeed::Output() const {
|
---|
97 | return output;
|
---|
98 | }
|
---|
99 | } // end namespace filter
|
---|
100 | } // end namespace flair
|
---|