source: flair-src/branches/sanscv/lib/FlairVisionFilter/src/OpticalFlow.cpp@ 324

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

removing opencv dependency

File size: 4.6 KB
Line 
1// created: 2012/04/12
2// filename: OpticalFlow.cpp
3//
4// author: Guillaume Sanahuja
5// Copyright Heudiasyc UMR UTC/CNRS 7253
6//
7// version: $Id: $
8//
9// purpose: calcul flux optique lk
10//
11//
12/*********************************************************************/
13
14#include "OpticalFlow.h"
15#include "OpticalFlowData.h"
16#include <Image.h>
17#include <Layout.h>
18#include <GroupBox.h>
19#include <SpinBox.h>
20//#include <algorithm>
21#include <OneAxisRotation.h>
22#include <Vector3D.h>
23#include <typeinfo>
24
25#define LEVEL_PYR 2
26
27using std::string;
28using std::swap;
29using namespace flair::core;
30using namespace flair::gui;
31
32namespace flair
33{
34namespace filter
35{
36
37OpticalFlow::OpticalFlow(const IODevice* parent,const LayoutPosition* position,string name) : IODevice(parent,name)
38{
39 Printf("optical flow: voir pour faire du multiple output\n");//pour pts A et B, found et error
40
41 try{
42 Image::Type const &imageType=dynamic_cast<Image::Type const &>(parent->GetOutputDataType());
43 pyr=new Image(this,imageType.GetWidth(),imageType.GetHeight(),Image::Type::Format::Gray);
44 pyr_old=new Image(this,imageType.GetWidth(),imageType.GetHeight(),Image::Type::Format::Gray);
45 } catch(std::bad_cast& bc) {
46 Err("io type mismatch\n");
47 pyr=NULL;
48 pyr_old=NULL;
49 return;
50 }
51
52 is_init=false;
53
54 GroupBox* reglages_groupbox=new GroupBox(position,name);
55 rotation=new OneAxisRotation(reglages_groupbox->NewRow(),"post rotation",OneAxisRotation::PostRotation);
56 max_features=new SpinBox(reglages_groupbox->NewRow(),"max features:",1,65535,1,1);
57
58 output=new OpticalFlowData(parent,max_features->Value());
59
60 pointsA=(CvPoint*)malloc(max_features->Value()*sizeof(CvPoint));
61 pointsB=(CvPoint*)malloc(max_features->Value()*sizeof(CvPoint));
62
63 found_feature=(char*)malloc(max_features->Value()*sizeof(char));
64 feature_error=(unsigned int*)malloc(max_features->Value()*sizeof(unsigned int));
65
66 SetIsReady(true);
67}
68
69OpticalFlow::~OpticalFlow(void) {
70 free((char*)pointsA);
71 free((char*)pointsB);
72 free((char*)found_feature);
73 free((char*)feature_error);
74}
75
76void OpticalFlow::UpdateFrom(const io_data *data) {/*
77 IplImage *gimg=((Image*)data)->img;
78 IplImage *gimg_old=((Image*)data->Prev(1))->img;
79
80 unsigned int count;
81 CvSize window = cvSize(3,3);
82 CvTermCriteria termination_criteria = cvTermCriteria( CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, .3 );
83 unsigned int i;
84
85 if(max_features->ValueChanged()==true) {
86 cvFree(&pointsA);
87 cvFree(&pointsB);
88 cvFree(&found_feature);
89 cvFree(&feature_error);
90 pointsA=(CvPoint *)cvAlloc(max_features->Value()*sizeof(CvPoint));
91 pointsB=(CvPoint *)cvAlloc(max_features->Value()*sizeof(CvPoint));
92 found_feature=(char *)cvAlloc(max_features->Value()*sizeof(char));
93 feature_error=(unsigned int *)cvAlloc(max_features->Value()*sizeof(unsigned int));
94
95 output->Resize(max_features->Value());
96 }
97
98 if(is_init==false)
99 {
100 data->GetMutex();
101 //init image old
102 dspPyrDown(gimg,pyr_old,LEVEL_PYR);
103 data->ReleaseMutex();
104 is_init=true;
105 printf("ajouter mise a 0 des points\n");
106 return;
107 }
108
109 data->GetMutex();
110 data->Prev(1)->GetMutex();
111
112 //select good features
113 count=max_features->Value();
114 dspGoodFeaturesToTrack(gimg_old,pointsA,&count,0.08,5);
115 //pyramide
116 dspPyrDown(gimg,pyr,LEVEL_PYR);
117 //lk
118 dspCalcOpticalFlowPyrLK(gimg_old,gimg,pyr_old,pyr,pointsA,pointsB,count,window,LEVEL_PYR,found_feature,feature_error,termination_criteria,0) ;
119
120 data->Prev(1)->ReleaseMutex();
121 data->ReleaseMutex();
122
123 //apply rotation
124 for(i=0;i<count;i++) {
125 Vector3Df tmp;
126 tmp.x=pointsA[i].x;
127 tmp.y=pointsA[i].y;
128 tmp.z=0;
129 rotation->ComputeRotation(tmp);
130 pointsA[i].x=tmp.x;
131 pointsA[i].y=tmp.y;
132
133 tmp.x=pointsB[i].x;
134 tmp.y=pointsB[i].y;
135 tmp.z=0;
136 rotation->ComputeRotation(tmp);
137 pointsB[i].x=tmp.x;
138 pointsB[i].y=tmp.y;
139 }
140
141 output->GetMutex();
142 CvPoint2D32f* pointsBf= output->PointsB();
143 for(i=0;i<count;i++)
144 {
145 pointsBf[i].x=pointsA[i].x+((float)pointsB[i].x)/256;
146 pointsBf[i].y=pointsA[i].y+((float)pointsB[i].y)/256;
147 }
148 output->ReleaseMutex();
149
150 output->SetPointsA(pointsA);
151 output->SetFoundFeature(found_feature);
152 output->SetFeatureError(feature_error);
153 output->SetNbFeatures(count);
154
155 //rotation
156 swap(pyr,pyr_old);
157*/
158 output->SetDataTime(data->DataTime());
159 ProcessUpdate(output);
160}
161
162} // end namespace filter
163} // end namespace flair
Note: See TracBrowser for help on using the repository browser.