Commit b564f1b6 authored by Alex Leontiev's avatar Alex Leontiev

vadim 14

parent 8c38fa76
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -55,6 +55,9 @@ ...@@ -55,6 +55,9 @@
#define MAX_EXAMPLES_IN_MODEL 500 #define MAX_EXAMPLES_IN_MODEL 500
#define MEASURES_PER_CLASSIFIER 13 #define MEASURES_PER_CLASSIFIER 13
#define SCALE_STEP 1.2 #define SCALE_STEP 1.2
#define ENSEMBLE_THRESHOLD 0.5
#define VARIANCE_THRESHOLD 0.5
#define GRIDSIZE 15
#define DOWNSCALE_MODE INTER_LINEAR #define DOWNSCALE_MODE INTER_LINEAR
#define BLUR_AS_VADIM #define BLUR_AS_VADIM
#undef CLOSED_LOOP #undef CLOSED_LOOP
...@@ -84,10 +87,8 @@ using namespace tld; ...@@ -84,10 +87,8 @@ using namespace tld;
* ?10. all in one class * ?10. all in one class
* 11. group decls logically, order of statements * 11. group decls logically, order of statements
* 12. not v=vector(n), but assign(n,0) * 12. not v=vector(n), but assign(n,0)
* -->14. TLDEnsembleClassifier
* 16. loops limits * 16. loops limits
* 17. inner scope loops * 17. inner scope loops
* 18. classify in TLDEnsembleClassifier
* 19. var checker * 19. var checker
* 20. NCC using plain loops * 20. NCC using plain loops
* 21. precompute offset * 21. precompute offset
...@@ -145,7 +146,7 @@ protected: ...@@ -145,7 +146,7 @@ protected:
Ptr<TrackerModel> model; Ptr<TrackerModel> model;
void computeIntegralImages(const Mat& img,Mat_<double>& intImgP,Mat_<double>& intImgP2){integral(img,intImgP,intImgP2,CV_64F);} void computeIntegralImages(const Mat& img,Mat_<double>& intImgP,Mat_<double>& intImgP2){integral(img,intImgP,intImgP2,CV_64F);}
inline bool patchVariance(Mat_<double>& intImgP,Mat_<double>& intImgP2,double originalVariance,Point pt,Size size); inline bool patchVariance(Mat_<double>& intImgP,Mat_<double>& intImgP2,double originalVariance,Point pt,Size size);
bool ensembleClassifier(const uchar* data,int rowstep){return ensembleClassifierNum(data,rowstep)>0.5;} bool ensembleClassifier(const uchar* data,int rowstep){return ensembleClassifierNum(data,rowstep)>ENSEMBLE_THRESHOLD;}
double ensembleClassifierNum(const uchar* data,int rowstep); double ensembleClassifierNum(const uchar* data,int rowstep);
TrackerTLD::Params params_; TrackerTLD::Params params_;
}; };
...@@ -417,9 +418,7 @@ timeStampPositiveNext(0),timeStampNegativeNext(0),params_(params){ ...@@ -417,9 +418,7 @@ timeStampPositiveNext(0),timeStampNegativeNext(0),params_(params){
getClosestN(scanGrid,Rect2d(boundingBox.x/scale,boundingBox.y/scale,boundingBox.width/scale,boundingBox.height/scale),10,closest); getClosestN(scanGrid,Rect2d(boundingBox.x/scale,boundingBox.y/scale,boundingBox.width/scale,boundingBox.height/scale),10,closest);
Mat_<uchar> blurredPatch(minSize); Mat_<uchar> blurredPatch(minSize);
for(int i=0,howMany=TLDEnsembleClassifier::getMaxOrdinal();i<howMany;i++){ TLDEnsembleClassifier::makeClassifiers(minSize,MEASURES_PER_CLASSIFIER,GRIDSIZE,classifiers);
classifiers.push_back(TLDEnsembleClassifier(i+1,minSize,MEASURES_PER_CLASSIFIER));
}
positiveExamples.reserve(200); positiveExamples.reserve(200);
Point2f center; Point2f center;
...@@ -586,7 +585,7 @@ bool TLDDetector::detect(const Mat& img,const Mat& imgBlurred,Rect2d& res,std::v ...@@ -586,7 +585,7 @@ bool TLDDetector::detect(const Mat& img,const Mat& imgBlurred,Rect2d& res,std::v
} }
bool TLDDetector::patchVariance(Mat_<double>& intImgP,Mat_<double>& intImgP2,double originalVariance,Point pt,Size size){ bool TLDDetector::patchVariance(Mat_<double>& intImgP,Mat_<double>& intImgP2,double originalVariance,Point pt,Size size){
return variance(intImgP,intImgP2,Rect(pt.x,pt.y,size.width,size.height)) >= 0.5*originalVariance; return variance(intImgP,intImgP2,Rect(pt.x,pt.y,size.width,size.height)) >= VARIANCE_THRESHOLD*originalVariance;
} }
double TLDDetector::ensembleClassifierNum(const uchar* data,int rowstep){ double TLDDetector::ensembleClassifierNum(const uchar* data,int rowstep){
...@@ -694,7 +693,7 @@ void TrackerTLDModel::integrateAdditional(const std::vector<Mat_<uchar> >& eForM ...@@ -694,7 +693,7 @@ void TrackerTLDModel::integrateAdditional(const std::vector<Mat_<uchar> >& eForM
p+=classifiers[i].posteriorProbability(eForEnsemble[k].data,(int)eForEnsemble[k].step[0]); p+=classifiers[i].posteriorProbability(eForEnsemble[k].data,(int)eForEnsemble[k].step[0]);
} }
p/=classifiers.size(); p/=classifiers.size();
if((p>0.5)!=isPositive){ if((p>ENSEMBLE_THRESHOLD)!=isPositive){
if(isPositive){ if(isPositive){
positiveIntoEnsemble++; positiveIntoEnsemble++;
}else{ }else{
......
...@@ -61,7 +61,7 @@ namespace cv {namespace tld ...@@ -61,7 +61,7 @@ namespace cv {namespace tld
clock_t start;float milisec=0.0;\ clock_t start;float milisec=0.0;\
start=clock();{a} milisec=1000.0*(clock()-start)/CLOCKS_PER_SEC;\ start=clock();{a} milisec=1000.0*(clock()-start)/CLOCKS_PER_SEC;\
dprintf(("%-90s took %f milis\n",#a,milisec)); } dprintf(("%-90s took %f milis\n",#a,milisec)); }
#define HERE dprintf(("%d\n",__LINE__));fflush(stderr); #define HERE dprintf(("line %d\n",__LINE__));fflush(stderr);
#define START_TICK(name) { clock_t start;double milisec=0.0; start=clock(); #define START_TICK(name) { clock_t start;double milisec=0.0; start=clock();
#define END_TICK(name) milisec=1000.0*(clock()-start)/CLOCKS_PER_SEC;\ #define END_TICK(name) milisec=1000.0*(clock()-start)/CLOCKS_PER_SEC;\
dprintf(("%s took %f milis\n",name,milisec)); } dprintf(("%s took %f milis\n",name,milisec)); }
...@@ -96,17 +96,15 @@ unsigned int getMedian(const std::vector<unsigned int>& values, int size=-1); ...@@ -96,17 +96,15 @@ unsigned int getMedian(const std::vector<unsigned int>& values, int size=-1);
class TLDEnsembleClassifier{ class TLDEnsembleClassifier{
public: public:
TLDEnsembleClassifier(int ordinal,Size size,int measurePerClassifier); static int makeClassifiers(Size size,int measurePerClassifier,int gridSize,std::vector<TLDEnsembleClassifier>& classifiers);
void integrate(const Mat_<uchar>& patch,bool isPositive); void integrate(const Mat_<uchar>& patch,bool isPositive);
double posteriorProbability(const uchar* data,int rowstep)const; double posteriorProbability(const uchar* data,int rowstep)const;
static int getMaxOrdinal();
private: private:
static int getGridSize(); TLDEnsembleClassifier(std::vector<Vec4b> meas,int beg,int end);
inline void stepPrefSuff(std::vector<uchar>& arr,int len); static void stepPrefSuff(std::vector<Vec4b>& arr,int pos,int len,int gridSize);
void preinit(int ordinal);
unsigned short int code(const uchar* data,int rowstep)const; unsigned short int code(const uchar* data,int rowstep)const;
std::vector<unsigned int> pos,neg; std::vector<unsigned int> pos,neg;
std::vector<uchar> x1,y1,x2,y2; std::vector<Vec4b> measurements;
}; };
class TrackerProxy{ class TrackerProxy{
......
...@@ -252,8 +252,7 @@ void resample(const Mat& img,const Rect2d& r2,Mat_<uchar>& samples){ ...@@ -252,8 +252,7 @@ void resample(const Mat& img,const Rect2d& r2,Mat_<uchar>& samples){
} }
//other stuff //other stuff
void TLDEnsembleClassifier::stepPrefSuff(std::vector<uchar>& arr,int len){ void TLDEnsembleClassifier::stepPrefSuff(std::vector<Vec4b>& arr,int pos,int len,int gridSize){
int gridSize=getGridSize();
#if 0 #if 0
int step=len/(gridSize-1), pref=(len-step*(gridSize-1))/2; int step=len/(gridSize-1), pref=(len-step*(gridSize-1))/2;
for(int i=0;i<(int)(sizeof(x1)/sizeof(x1[0]));i++){ for(int i=0;i<(int)(sizeof(x1)/sizeof(x1[0]));i++){
...@@ -266,39 +265,29 @@ void TLDEnsembleClassifier::stepPrefSuff(std::vector<uchar>& arr,int len){ ...@@ -266,39 +265,29 @@ void TLDEnsembleClassifier::stepPrefSuff(std::vector<uchar>& arr,int len){
int bigOnes=rem,smallOnes=gridSize-bigOnes-1; int bigOnes=rem,smallOnes=gridSize-bigOnes-1;
int bigOnes_front=bigOnes/2,bigOnes_back=bigOnes-bigOnes_front; int bigOnes_front=bigOnes/2,bigOnes_back=bigOnes-bigOnes_front;
for(int i=0;i<(int)arr.size();i++){ for(int i=0;i<(int)arr.size();i++){
if(arr[i]<bigOnes_back){ if(arr[i].val[pos]<bigOnes_back){
arr[i]=(uchar)(arr[i]*bigStep+arr[i]); arr[i].val[pos]=(uchar)(arr[i].val[pos]*bigStep+arr[i].val[pos]);
continue; continue;
} }
if(arr[i]<(bigOnes_front+smallOnes)){ if(arr[i].val[pos]<(bigOnes_front+smallOnes)){
arr[i]=(uchar)(bigOnes_front*bigStep+(arr[i]-bigOnes_front)*smallStep+arr[i]); arr[i].val[pos]=(uchar)(bigOnes_front*bigStep+(arr[i].val[pos]-bigOnes_front)*smallStep+arr[i].val[pos]);
continue; continue;
} }
if(arr[i]<(bigOnes_front+smallOnes+bigOnes_back)){ if(arr[i].val[pos]<(bigOnes_front+smallOnes+bigOnes_back)){
arr[i]=(uchar)(bigOnes_front*bigStep+smallOnes*smallStep+(arr[i]-(bigOnes_front+smallOnes))*bigStep+arr[i]); arr[i].val[pos]=
(uchar)(bigOnes_front*bigStep+smallOnes*smallStep+(arr[i].val[pos]-(bigOnes_front+smallOnes))*bigStep+arr[i].val[pos]);
continue; continue;
} }
arr[i]=(uchar)(len-1); arr[i].val[pos]=(uchar)(len-1);
} }
#endif #endif
} }
TLDEnsembleClassifier::TLDEnsembleClassifier(int ordinal,Size size,int measurePerClassifier){ TLDEnsembleClassifier::TLDEnsembleClassifier(std::vector<Vec4b> meas,int beg,int end){
x1=std::vector<uchar>(measurePerClassifier,0);
x2=std::vector<uchar>(measurePerClassifier,0);
y1=std::vector<uchar>(measurePerClassifier,0);
y2=std::vector<uchar>(measurePerClassifier,0);
preinit(ordinal);
stepPrefSuff(x1,size.width);
stepPrefSuff(x2,size.width);
stepPrefSuff(y1,size.height);
stepPrefSuff(y2,size.height);
int posSize=1; int posSize=1;
for(int i=0;i<measurePerClassifier;i++)posSize*=2; for(int i=0,mpc=end-beg;i<mpc;i++)posSize*=2;
pos=std::vector<unsigned int>(posSize,0); pos=std::vector<unsigned int>(posSize,0);
neg=std::vector<unsigned int>(posSize,0); neg=std::vector<unsigned int>(posSize,0);
measurements.assign(meas.begin()+beg,meas.begin()+end);
} }
void TLDEnsembleClassifier::integrate(const Mat_<uchar>& patch,bool isPositive){ void TLDEnsembleClassifier::integrate(const Mat_<uchar>& patch,bool isPositive){
unsigned short int position=code(patch.data,(int)patch.step[0]); unsigned short int position=code(patch.data,(int)patch.step[0]);
...@@ -318,15 +307,45 @@ double TLDEnsembleClassifier::posteriorProbability(const uchar* data,int rowstep ...@@ -318,15 +307,45 @@ double TLDEnsembleClassifier::posteriorProbability(const uchar* data,int rowstep
} }
} }
unsigned short int TLDEnsembleClassifier::code(const uchar* data,int rowstep)const{ unsigned short int TLDEnsembleClassifier::code(const uchar* data,int rowstep)const{
unsigned short int position=0; unsigned short int position=0;//TODO: this --> encapsule
for(int i=0;i<(int)x1.size();i++){ for(int i=0;i<(int)measurements.size();i++){
position=position<<1; position=position<<1;
if(*(data+rowstep*y1[i]+x1[i])<*(data+rowstep*y2[i]+x2[i])){ if(*(data+rowstep*measurements[i].val[0]+measurements[i].val[1])<*(data+rowstep*measurements[i].val[2]+measurements[i].val[3])){
position++; position++;
}else{ }else{
} }
} }
return position; return position;
} }
int TLDEnsembleClassifier::makeClassifiers(Size size,int measurePerClassifier,int gridSize,
std::vector<TLDEnsembleClassifier>& classifiers){
std::vector<Vec4b> measurements;
for(int i=0;i<gridSize;i++){
for(int j=0;j<gridSize;j++){
for(int k=0;k<j;k++){
Vec4b m;
m.val[0]=m.val[2]=i;
m.val[1]=j;m.val[3]=k;
measurements.push_back(m);
m.val[1]=m.val[3]=i;
m.val[0]=j;m.val[2]=k;
measurements.push_back(m);
}
}
}
random_shuffle(measurements.begin(),measurements.end());
stepPrefSuff(measurements,0,size.width,gridSize);
stepPrefSuff(measurements,1,size.width,gridSize);
stepPrefSuff(measurements,2,size.height,gridSize);
stepPrefSuff(measurements,3,size.height,gridSize);
for(int i=0,howMany=measurements.size()/measurePerClassifier;i<howMany;i++){
classifiers.push_back(TLDEnsembleClassifier(measurements,i*measurePerClassifier,(i+1)*measurePerClassifier));
}
return (int)classifiers.size();
}
}} }}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment