source: flair-src/trunk/lib/FlairVisionFilter/src/OpticalFlow.cpp @ 144

Last change on this file since 144 was 144, checked in by Bayard Gildas, 4 years ago

Go!

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 <cvimage.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        cvimage::Type const &imageType=dynamic_cast<cvimage::Type const &>(parent->GetOutputDataType());
43        pyr=cvCreateImage(cvSize(imageType.GetWidth(),imageType.GetHeight()),IPL_DEPTH_8U,1);
44        pyr_old=cvCreateImage(cvSize(imageType.GetWidth(),imageType.GetHeight()),IPL_DEPTH_8U,1);
45
46    } catch(std::bad_cast& bc) {
47        Err("io type mismatch\n");
48        pyr=NULL;
49        pyr_old=NULL;
50    }
51
52    is_init=false;
53
54    GroupBox* reglages_groupbox=new GroupBox(position,name);
55    rotation=new OneAxisRotation(reglages_groupbox->NewRow(),"post rotation");
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 *)cvAlloc(max_features->Value()*sizeof(CvPoint));
61    pointsB=(CvPoint *)cvAlloc(max_features->Value()*sizeof(CvPoint));
62
63    found_feature=(char *)cvAlloc(max_features->Value()*sizeof(char));
64    feature_error=(unsigned int *)cvAlloc(max_features->Value()*sizeof(unsigned int));
65}
66
67OpticalFlow::~OpticalFlow(void)
68{
69    cvReleaseImage(&pyr);
70    cvReleaseImage(&pyr_old);
71    cvFree(&pointsA);
72    cvFree(&pointsB);
73    cvFree(&found_feature);
74    cvFree(&feature_error);
75}
76
77void OpticalFlow::UpdateFrom(const io_data *data)
78{
79    IplImage *gimg=((cvimage*)data)->img;
80    IplImage *gimg_old=((cvimage*)data->Prev(1))->img;
81
82    unsigned int count;
83    CvSize window = cvSize(3,3);
84    CvTermCriteria termination_criteria = cvTermCriteria( CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, .3 );
85    unsigned int i;
86
87    if(max_features->ValueChanged()==true) {
88        cvFree(&pointsA);
89        cvFree(&pointsB);
90        cvFree(&found_feature);
91        cvFree(&feature_error);
92        pointsA=(CvPoint *)cvAlloc(max_features->Value()*sizeof(CvPoint));
93        pointsB=(CvPoint *)cvAlloc(max_features->Value()*sizeof(CvPoint));
94        found_feature=(char *)cvAlloc(max_features->Value()*sizeof(char));
95        feature_error=(unsigned int *)cvAlloc(max_features->Value()*sizeof(unsigned int));
96
97        output->Resize(max_features->Value());
98    }
99/*
100    if(is_init==false)
101    {
102        data->GetMutex();
103        //init image old
104        dspPyrDown(gimg,pyr_old,LEVEL_PYR);
105        data->ReleaseMutex();
106        is_init=true;
107        printf("ajouter mise a 0 des points\n");
108        return;
109    }
110
111    data->GetMutex();
112    data->Prev(1)->GetMutex();
113
114    //select good features
115    count=max_features->Value();
116    dspGoodFeaturesToTrack(gimg_old,pointsA,&count,0.08,5);
117    //pyramide
118    dspPyrDown(gimg,pyr,LEVEL_PYR);
119    //lk
120    dspCalcOpticalFlowPyrLK(gimg_old,gimg,pyr_old,pyr,pointsA,pointsB,count,window,LEVEL_PYR,found_feature,feature_error,termination_criteria,0) ;
121
122    data->Prev(1)->ReleaseMutex();
123    data->ReleaseMutex();
124*/
125    //apply rotation
126    for(i=0;i<count;i++) {
127        Vector3D tmp;
128        tmp.x=pointsA[i].x;
129        tmp.y=pointsA[i].y;
130        tmp.z=0;
131        rotation->ComputeRotation(tmp);
132        pointsA[i].x=tmp.x;
133        pointsA[i].y=tmp.y;
134
135        tmp.x=pointsB[i].x;
136        tmp.y=pointsB[i].y;
137        tmp.z=0;
138        rotation->ComputeRotation(tmp);
139        pointsB[i].x=tmp.x;
140        pointsB[i].y=tmp.y;
141    }
142
143    output->GetMutex();
144    CvPoint2D32f* pointsBf= output->PointsB();
145    for(i=0;i<count;i++)
146    {
147        pointsBf[i].x=pointsA[i].x+((float)pointsB[i].x)/256;
148        pointsBf[i].y=pointsA[i].y+((float)pointsB[i].y)/256;
149    }
150    output->ReleaseMutex();
151
152    output->SetPointsA(pointsA);
153    output->SetFoundFeature(found_feature);
154    output->SetFeatureError(feature_error);
155    output->SetNbFeatures(count);
156
157    //rotation
158    swap(pyr,pyr_old);
159
160    output->SetDataTime(data->DataTime());
161    ProcessUpdate(output);
162}
163
164} // end namespace filter
165} // end namespace flair
Note: See TracBrowser for help on using the repository browser.