Changeset 325 in flair-src for branches/sanscv/lib/FlairVisionFilter/src/OpticalFlow.cpp
- Timestamp:
- 08/28/19 16:12:11 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/sanscv/lib/FlairVisionFilter/src/OpticalFlow.cpp
r324 r325 14 14 #include "OpticalFlow.h" 15 15 #include "OpticalFlowData.h" 16 #include "VisionFilter.h" 16 17 #include <Image.h> 17 18 #include <Layout.h> 18 19 #include <GroupBox.h> 19 20 #include <SpinBox.h> 20 //#include <algorithm> 21 //#include <dspcv_gpp.h> 22 #include <algorithm> 21 23 #include <OneAxisRotation.h> 22 24 #include <Vector3D.h> … … 30 32 using namespace flair::gui; 31 33 34 class OpticalFlow_impl { 35 public: 36 OpticalFlow_impl(flair::filter::OpticalFlow *self,const LayoutPosition* position,string name) { 37 this->self=self; 38 is_init=false; 39 40 GroupBox* reglages_groupbox=new GroupBox(position,name); 41 rotation=new OneAxisRotation(reglages_groupbox->NewRow(),"post rotation",OneAxisRotation::PostRotation); 42 max_features=new SpinBox(reglages_groupbox->NewRow(),"max features:",1,65535,1,1); 43 44 try{ 45 Image::Type const &imageType=dynamic_cast<Image::Type const &>(((IODevice*)(self->Parent()))->GetOutputDataType()); 46 pyr=new Image(self,imageType.GetWidth(),imageType.GetHeight(),Image::Type::Format::Gray); 47 pyr_old=new Image(self,imageType.GetWidth(),imageType.GetHeight(),Image::Type::Format::Gray); 48 output=new flair::filter::OpticalFlowData(self->Parent(),max_features->Value()); 49 /* 50 pointsA=(CvPoint*)AllocFunction(max_features->Value()*sizeof(CvPoint)); 51 pointsB=(CvPoint*)AllocFunction(max_features->Value()*sizeof(CvPoint)); 52 53 found_feature=(char*)AllocFunction(max_features->Value()*sizeof(char)); 54 feature_error=(unsigned int*)AllocFunction(max_features->Value()*sizeof(unsigned int)); 55 56 iplgimg = (IplImage*)AllocFunction(sizeof(IplImage)); 57 iplgimg_old = (IplImage*)AllocFunction(sizeof(IplImage)); 58 iplpyr = (IplImage*)AllocFunction(sizeof(IplImage)); 59 iplpyr_old = (IplImage*)AllocFunction(sizeof(IplImage)); 60 61 iplpyr->width=imageType.GetWidth(); 62 iplpyr->height=imageType.GetHeight(); 63 iplpyr_old->width=imageType.GetWidth(); 64 iplpyr_old->height=imageType.GetHeight(); 65 */ 66 } catch(std::bad_cast& bc) { 67 self->Err("io type mismatch\n"); 68 pyr=NULL; 69 pyr_old=NULL; 70 } 71 72 } 73 74 ~OpticalFlow_impl() { 75 /* 76 FreeFunction((char*)pointsA); 77 FreeFunction((char*)pointsB); 78 FreeFunction((char*)found_feature); 79 FreeFunction((char*)feature_error); 80 FreeFunction((char*)iplgimg); 81 FreeFunction((char*)iplgimg_old); 82 FreeFunction((char*)iplpyr); 83 FreeFunction((char*)iplpyr_old);*/ 84 } 85 86 void UpdateFrom(const io_data *data){ 87 //Time tStart=GetTime(); 88 Image *img=(Image*)data; 89 Image *img_old=((Image*)data->Prev(1)); 90 /* 91 iplgimg->width=img->GetDataType().GetWidth(); 92 iplgimg->height=img->GetDataType().GetHeight(); 93 iplgimg->imageData=img->buffer; 94 95 iplgimg_old->width=img_old->GetDataType().GetWidth(); 96 iplgimg_old->height=img_old->GetDataType().GetHeight(); 97 iplgimg_old->imageData=img_old->buffer; 98 99 iplpyr->imageData=pyr->buffer; 100 iplpyr_old->imageData=pyr_old->buffer; 101 102 unsigned int count; 103 CvSize window = {3,3}; 104 CvTermCriteria termination_criteria ={CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, .3 }; 105 unsigned int i; 106 107 if(max_features->ValueChanged()==true) { 108 FreeFunction((char*)pointsA); 109 FreeFunction((char*)pointsB); 110 FreeFunction((char*)found_feature); 111 FreeFunction((char*)feature_error); 112 pointsA=(CvPoint*)AllocFunction(max_features->Value()*sizeof(CvPoint)); 113 pointsB=(CvPoint*)AllocFunction(max_features->Value()*sizeof(CvPoint)); 114 found_feature=(char*)AllocFunction(max_features->Value()*sizeof(char)); 115 feature_error=(unsigned int*)AllocFunction(max_features->Value()*sizeof(unsigned int)); 116 117 output->Resize(max_features->Value()); 118 } 119 120 if(is_init==false) { 121 data->GetMutex(); 122 //init image old 123 dspPyrDown(iplgimg,iplpyr_old,LEVEL_PYR); 124 data->ReleaseMutex(); 125 is_init=true; 126 printf("ajouter mise a 0 des points\n"); 127 return; 128 } 129 130 data->GetMutex(); 131 data->Prev(1)->GetMutex(); 132 133 //select good features 134 count=max_features->Value(); 135 dspGoodFeaturesToTrack(iplgimg_old,pointsA,&count,0.08,5); 136 //pyramide 137 dspPyrDown(iplgimg,iplpyr,LEVEL_PYR); 138 //lk 139 dspCalcOpticalFlowPyrLK(iplgimg_old,iplgimg,iplpyr_old,iplpyr,pointsA,pointsB,count,window,LEVEL_PYR,found_feature,feature_error,termination_criteria,0) ; 140 141 data->Prev(1)->ReleaseMutex(); 142 data->ReleaseMutex(); 143 144 //apply rotation 145 for(i=0;i<count;i++) { 146 Vector3Df tmp; 147 tmp.x=pointsA[i].x; 148 tmp.y=pointsA[i].y; 149 tmp.z=0; 150 rotation->ComputeRotation(tmp); 151 pointsA[i].x=tmp.x; 152 pointsA[i].y=tmp.y; 153 154 tmp.x=pointsB[i].x; 155 tmp.y=pointsB[i].y; 156 tmp.z=0; 157 rotation->ComputeRotation(tmp); 158 pointsB[i].x=tmp.x; 159 pointsB[i].y=tmp.y; 160 } 161 162 output->GetMutex(); 163 CvPoint2D32f* pointsBf= output->PointsB(); 164 for(i=0;i<count;i++) { 165 pointsBf[i].x=pointsA[i].x+((float)pointsB[i].x)/256; 166 pointsBf[i].y=pointsA[i].y+((float)pointsB[i].y)/256; 167 } 168 output->ReleaseMutex(); 169 170 output->SetPointsA(pointsA); 171 output->SetFoundFeature(found_feature); 172 output->SetFeatureError(feature_error); 173 output->SetNbFeatures(count); 174 175 //rotation 176 swap(pyr,pyr_old); 177 */ 178 output->SetDataTime(data->DataTime()); 179 //Printf("Optical flow computation time=%f\n",(GetTime()-tStart)/(1000.*1000)); 180 }; 181 182 flair::filter::OpticalFlowData *output; 183 184 private: 185 flair::filter::OpticalFlow *self; 186 SpinBox *max_features; 187 OneAxisRotation* rotation; 188 189 //CvPoint* pointsA; 190 //CvPoint* pointsB; 191 char *found_feature; 192 unsigned int *feature_error; 193 Image *pyr,*pyr_old; 194 //IplImage *iplgimg,*iplgimg_old; 195 //IplImage *iplpyr,*iplpyr_old; 196 197 bool is_init; 198 }; 199 200 32 201 namespace flair 33 202 { … … 35 204 { 36 205 37 OpticalFlow::OpticalFlow(const IODevice* parent,const LayoutPosition* position,string name) : IODevice(parent,name) 38 { 206 OpticalFlow::OpticalFlow(const IODevice* parent,const LayoutPosition* position,string name) : IODevice(parent,name) { 39 207 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; 208 pimpl_=new OpticalFlow_impl(this,position,name); 209 } 210 211 OpticalFlow::~OpticalFlow(void) { 212 delete pimpl_; 213 } 214 215 void OpticalFlow::UpdateFrom(const io_data *data) { 216 pimpl_->UpdateFrom(data); 217 ProcessUpdate(pimpl_->output); 218 } 219 220 OpticalFlowData* OpticalFlow::Output(void) { 221 return pimpl_->output; 222 223 } 224 225 DataType const &OpticalFlow::GetOutputDataType() const { 226 if(pimpl_->output!=NULL) { 227 return pimpl_->output->GetDataType(); 228 } else { 229 return dummyType; 50 230 } 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 69 OpticalFlow::~OpticalFlow(void) {70 free((char*)pointsA);71 free((char*)pointsB);72 free((char*)found_feature);73 free((char*)feature_error);74 }75 76 void 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 old102 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 features113 count=max_features->Value();114 dspGoodFeaturesToTrack(gimg_old,pointsA,&count,0.08,5);115 //pyramide116 dspPyrDown(gimg,pyr,LEVEL_PYR);117 //lk118 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 rotation124 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 //rotation156 swap(pyr,pyr_old);157 */158 output->SetDataTime(data->DataTime());159 ProcessUpdate(output);160 231 } 161 232
Note:
See TracChangeset
for help on using the changeset viewer.