From 70803dff04c0010240b4764e1c0927724d666e43 Mon Sep 17 00:00:00 2001 From: Alex Leontiev Date: Wed, 2 Jul 2014 21:21:29 +0900 Subject: [PATCH] Vadim --- modules/tracking/src/TLD.cpp | 49 ++++++++++++++++------------- modules/tracking/src/TLD.hpp | 10 +++--- modules/tracking/src/trackerTLD.cpp | 39 ++++++----------------- 3 files changed, 42 insertions(+), 56 deletions(-) diff --git a/modules/tracking/src/TLD.cpp b/modules/tracking/src/TLD.cpp index 66104d146..74e6e0034 100644 --- a/modules/tracking/src/TLD.cpp +++ b/modules/tracking/src/TLD.cpp @@ -181,23 +181,23 @@ double variance(const Mat& img){ p2/=(img.cols*img.rows); return p2-p*p; } -double variance(Mat_& intImgP,Mat_& intImgP2,Rect box){ +double variance(Mat_& intImgP,Mat_& intImgP2,Rect box){ int x=(box.x),y=(box.y),width=(box.width),height=(box.height); - CV_Assert(0<=x && (x+width)<=intImgP.cols && (x+width)<=intImgP2.cols); - CV_Assert(0<=y && (y+height)<=intImgP.rows && (y+height)<=intImgP2.rows); + CV_Assert(0<=x && (x+width)0&&x>0)?intImgP(y-1,x-1):0); - B=((y>0)?intImgP(y-1,x+width-1):0); - C=((x>0)?intImgP(y+height-1,x-1):0); - D=intImgP(y+height-1,x+width-1); + A=intImgP(y,x); + B=intImgP(y,x+width); + C=intImgP(y+height,x); + D=intImgP(y+height,x+width); p=(0.0+A+D-B-C)/(width*height); - A=((y>0&&x>0)?intImgP2(y-1,x-1):0); - B=((y>0)?intImgP2(y-1,x+width-1):0); - C=((x>0)?intImgP2(y+height-1,x-1):0); - D=intImgP2(y+height-1,x+width-1); + A=intImgP2(y,x); + B=intImgP2(y,x+width); + C=intImgP2(y+height,x); + D=intImgP2(y+height,x+width); p2=(0.0+(D-B)-(C-A))/(width*height); return p2-p*p; @@ -253,11 +253,10 @@ void resample(const Mat& img,const Rect2d& r2,Mat_& samples){ M(0,0)=(float)(samples.cols/r2.width); M(0,1)=0.0f; M(0,2)=(float)(-r2.x*samples.cols/r2.width); M(1,0)=0.0f; M(1,1)=(float)(samples.rows/r2.height); M(1,2)=(float)(-r2.y*samples.rows/r2.height); warpAffine(img,samples,M,samples.size()); - } //other stuff -void TLDEnsembleClassifier::stepPrefSuff(uchar* arr,int len){ +void TLDEnsembleClassifier::stepPrefSuff(std::vector& arr,int len){ int gridSize=getGridSize(); #if 0 int step=len/(gridSize-1), pref=(len-step*(gridSize-1))/2; @@ -270,7 +269,7 @@ void TLDEnsembleClassifier::stepPrefSuff(uchar* arr,int len){ int smallStep=quo,bigStep=quo+1; int bigOnes=rem,smallOnes=gridSize-bigOnes-1; int bigOnes_front=bigOnes/2,bigOnes_back=bigOnes-bigOnes_front; - for(int i=0;i<(int)(sizeof(x1)/sizeof(x1[0]));i++){ + for(int i=0;i<(int)arr.size();i++){ if(arr[i](measurePerClassifier,0); + x2=std::vector(measurePerClassifier,0); + y1=std::vector(measurePerClassifier,0); + y2=std::vector(measurePerClassifier,0); + preinit(ordinal); + stepPrefSuff(x1,size.width); stepPrefSuff(x2,size.width); stepPrefSuff(y1,size.height); stepPrefSuff(y2,size.height); + + int posSize=1; + for(int i=0;i(posSize,0); + neg=std::vector(posSize,0); } void TLDEnsembleClassifier::integrate(Mat_ patch,bool isPositive){ unsigned short int position=code(patch.data,(int)patch.step[0]); @@ -313,18 +323,13 @@ double TLDEnsembleClassifier::posteriorProbability(const uchar* data,int rowstep } unsigned short int TLDEnsembleClassifier::code(const uchar* data,int rowstep)const{ unsigned short int position=0; - //char codeS[20]; - for(int i=0;i<(int)(sizeof(x1)/sizeof(x1[0]));i++){ + for(int i=0;i<(int)x1.size();i++){ position=position<<1; if(*(data+rowstep*y1[i]+x1[i])<*(data+rowstep*y2[i]+x2[i])){ position++; - //codeS[i]='o'; }else{ - //codeS[i]='x'; } } - //codeS[13]='\0'; - //dprintf(("integrate with code %s\n",codeS)); return position; } diff --git a/modules/tracking/src/TLD.hpp b/modules/tracking/src/TLD.hpp index 148f21a8e..890835537 100644 --- a/modules/tracking/src/TLD.hpp +++ b/modules/tracking/src/TLD.hpp @@ -79,7 +79,7 @@ double overlap(const Rect2d& r1,const Rect2d& r2); void resample(const Mat& img,const RotatedRect& r2,Mat_& samples); void resample(const Mat& img,const Rect2d& r2,Mat_& samples); double variance(const Mat& img); -double variance(Mat_& intImgP,Mat_& intImgP2,Rect box); +double variance(Mat_& intImgP,Mat_& intImgP2,Rect box); double NCC(Mat_ patch1,Mat_ patch2); void getClosestN(std::vector& scanGrid,Rect2d bBox,int n,std::vector& res); double scaleAndBlur(const Mat& originalImg,int scale,Mat& scaledImg,Mat& blurredImg,Size GaussBlurKernelSize); @@ -87,17 +87,17 @@ unsigned int getMedian(const std::vector& values, int size=-1); class TLDEnsembleClassifier{ public: - TLDEnsembleClassifier(int ordinal,Size size); + TLDEnsembleClassifier(int ordinal,Size size,int measurePerClassifier); void integrate(Mat_ patch,bool isPositive); double posteriorProbability(const uchar* data,int rowstep)const; static int getMaxOrdinal(); private: static int getGridSize(); - inline void stepPrefSuff(uchar* arr,int len); + inline void stepPrefSuff(std::vector& arr,int len); void preinit(int ordinal); unsigned short int code(const uchar* data,int rowstep)const; - unsigned int pos[8192],neg[8192];//8192=2^13 - uchar x1[13],y1[13],x2[13],y2[13]; + std::vector pos,neg; + std::vector x1,y1,x2,y2; }; class TrackerProxy : public TrackerTLD::Private{ diff --git a/modules/tracking/src/trackerTLD.cpp b/modules/tracking/src/trackerTLD.cpp index 5343b8805..3291dff04 100644 --- a/modules/tracking/src/trackerTLD.cpp +++ b/modules/tracking/src/trackerTLD.cpp @@ -52,6 +52,7 @@ #define CORE_THRESHOLD 0.5 #define NEG_EXAMPLES_IN_INIT_MODEL 300 #define MAX_EXAMPLES_IN_MODEL 500 +#define MEASURES_PER_CLASSIFIER 13 static const cv::Size GaussBlurKernelSize(3,3); using namespace cv; @@ -65,19 +66,14 @@ using namespace tld; * THETA_NN 0.5<->0.6 dramatic change vs video 6 !! * TODO: * schoolPC: codec, libopencv-dev - * fix pushbot ->pick commits -> compare_branches->all in 1 - * ||video(0.5<->0.6) --> debug if box size is less than 20 --> (remove ensemble self-loop) --> (try inter_area) + * fix pushbot ->pick commits -> compare_branches->all in 1->resubmit + * ||video(0.5<->0.6) --> debug if box size is less than 20 --> (remove ensemble self-loop) --> (try inter_area for resize) * perfect PN * * vadim: * - * private members - * - * cv::integral - * - * 13 as enum - * * blurred in TrackerTLDModel() + * private members * * warpAffine -- ? */ @@ -129,8 +125,8 @@ public: protected: friend class MyMouseCallbackDEBUG; Ptr model; - void computeIntegralImages(const Mat& img,Mat_& intImgP,Mat_& intImgP2); - bool patchVariance(Mat_& intImgP,Mat_& intImgP2,double originalVariance,Point pt,Size size); + inline void computeIntegralImages(const Mat& img,Mat_& intImgP,Mat_& intImgP2){integral(img,intImgP,intImgP2,CV_64F);} + inline bool patchVariance(Mat_& intImgP,Mat_& intImgP2,double originalVariance,Point pt,Size size); bool ensembleClassifier(const uchar* data,int rowstep){return ensembleClassifierNum(data,rowstep)>0.5;} double ensembleClassifierNum(const uchar* data,int rowstep); TrackerTLD::Params params_; @@ -406,7 +402,7 @@ timeStampPositiveNext(0),timeStampNegativeNext(0),params_(params){ Mat_ blurredPatch(minSize); for(int i=0,howMany=TLDEnsembleClassifier::getMaxOrdinal();i intImgP(resized_img.rows,resized_img.cols),intImgP2(resized_img.rows,resized_img.cols); + Mat_ intImgP,intImgP2; computeIntegralImages(resized_img,intImgP,intImgP2); for(int i=0,imax=cvFloor((0.0+resized_img.cols-initSize.width)/dx);i& intImgP,Mat_& intImgP2){ - intImgP(0,0)=img.at(0,0); - for(int j=1;j(0,j);} - for(int i=1;i(i,0);} - for(int i=1;i(i,j);}} - - unsigned int p; - p=img.at(0,0);intImgP2(0,0)=p*p; - for(int j=1;j(0,j);intImgP2(0,j)=intImgP2(0,j-1)+p*p;} - for(int i=1;i(i,0);intImgP2(i,0)=intImgP2(i-1,0)+p*p;} - for(int i=1;i(i,j); - intImgP2(i,j)=intImgP2(i,j-1)-intImgP2(i-1,j-1)+intImgP2(i-1,j)+p*p;}} -} - -bool TLDDetector::patchVariance(Mat_& intImgP,Mat_& intImgP2,double originalVariance,Point pt,Size size){ +bool TLDDetector::patchVariance(Mat_& intImgP,Mat_& intImgP2,double originalVariance,Point pt,Size size){ return variance(intImgP,intImgP2,Rect(pt.x,pt.y,size.width,size.height)) >= 0.5*originalVariance; } @@ -818,7 +799,7 @@ void MyMouseCallbackDEBUG::onMouse( int event, int x, int y){ resize(img_,resized_img,size); resize(imgBlurred_,blurred_img,size); - Mat_ intImgP(resized_img.rows,resized_img.cols),intImgP2(resized_img.rows,resized_img.cols); + Mat_ intImgP,intImgP2; detector_->computeIntegralImages(resized_img,intImgP,intImgP2); int dx=initSize.width/10, dy=initSize.height/10,