Commit 7dca000b authored by Alex Leontiev's avatar Alex Leontiev

vadim 21

parent 2848831c
...@@ -88,14 +88,13 @@ using namespace tld; ...@@ -88,14 +88,13 @@ using namespace tld;
* *
* ?10. all in one class * ?10. all in one class
* *
* 21. precompute offset * -->21. precompute offset
*
* 16. loops limits * 16. loops limits
* 17. inner scope loops * 17. inner scope loops
*/ */
/* design decisions: /* design decisions:
* blur --> resize (vs. resize-->blur) in detect(), ensembleClassifier stage
* no random gauss noise, when making examples for ensembleClassifier
*/ */
namespace cv namespace cv
...@@ -144,7 +143,6 @@ protected: ...@@ -144,7 +143,6 @@ 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);
inline bool ensembleClassifier(const uchar* data,int rowstep);
TrackerTLD::Params params_; TrackerTLD::Params params_;
}; };
...@@ -200,13 +198,15 @@ class TrackerTLDModel : public TrackerModel{ ...@@ -200,13 +198,15 @@ class TrackerTLDModel : public TrackerModel{
Rect2d getBoundingBox(){return boundingBox_;} Rect2d getBoundingBox(){return boundingBox_;}
void setBoudingBox(Rect2d boundingBox){boundingBox_=boundingBox;} void setBoudingBox(Rect2d boundingBox){boundingBox_=boundingBox;}
double getOriginalVariance(){return originalVariance_;} double getOriginalVariance(){return originalVariance_;}
inline double ensembleClassifierNum(const uchar* data,int rowstep); inline double ensembleClassifierNum(const uchar* data);
inline void prepareClassifiers(int rowstep){for(int i=0;i<(int)classifiers.size();i++)classifiers[i].prepareClassifier(rowstep);}
double Sr(const Mat_<uchar>& patch); double Sr(const Mat_<uchar>& patch);
double Sc(const Mat_<uchar>& patch); double Sc(const Mat_<uchar>& patch);
void integrateRelabeled(Mat& img,Mat& imgBlurred,const std::vector<TLDDetector::LabeledPatch>& patches); void integrateRelabeled(Mat& img,Mat& imgBlurred,const std::vector<TLDDetector::LabeledPatch>& patches);
void integrateAdditional(const std::vector<Mat_<uchar> >& eForModel,const std::vector<Mat_<uchar> >& eForEnsemble,bool isPositive); void integrateAdditional(const std::vector<Mat_<uchar> >& eForModel,const std::vector<Mat_<uchar> >& eForEnsemble,bool isPositive);
Size getMinSize(){return minSize_;} Size getMinSize(){return minSize_;}
void printme(FILE* port=stdout); void printme(FILE* port=stdout);
protected: protected:
Size minSize_; Size minSize_;
unsigned int timeStampPositiveNext,timeStampNegativeNext; unsigned int timeStampPositiveNext,timeStampNegativeNext;
...@@ -517,6 +517,7 @@ bool TLDDetector::detect(const Mat& img,const Mat& imgBlurred,Rect2d& res,std::v ...@@ -517,6 +517,7 @@ bool TLDDetector::detect(const Mat& img,const Mat& imgBlurred,Rect2d& res,std::v
Mat_<double> intImgP,intImgP2; Mat_<double> intImgP,intImgP2;
computeIntegralImages(resized_img,intImgP,intImgP2); computeIntegralImages(resized_img,intImgP,intImgP2);
tldModel->prepareClassifiers((int)blurred_img.step[0]);
for(int i=0,imax=cvFloor((0.0+resized_img.cols-initSize.width)/dx);i<imax;i++){ for(int i=0,imax=cvFloor((0.0+resized_img.cols-initSize.width)/dx);i<imax;i++){
for(int j=0,jmax=cvFloor((0.0+resized_img.rows-initSize.height)/dy);j<jmax;j++){ for(int j=0,jmax=cvFloor((0.0+resized_img.rows-initSize.height)/dy);j<jmax;j++){
LabeledPatch labPatch; LabeledPatch labPatch;
...@@ -524,7 +525,7 @@ bool TLDDetector::detect(const Mat& img,const Mat& imgBlurred,Rect2d& res,std::v ...@@ -524,7 +525,7 @@ bool TLDDetector::detect(const Mat& img,const Mat& imgBlurred,Rect2d& res,std::v
if(!patchVariance(intImgP,intImgP2,originalVariance,Point(dx*i,dy*j),initSize)){ if(!patchVariance(intImgP,intImgP2,originalVariance,Point(dx*i,dy*j),initSize)){
continue; continue;
} }
if(!ensembleClassifier(&blurred_img.at<uchar>(dy*j,dx*i),(int)blurred_img.step[0])){ if(!(tldModel->ensembleClassifierNum(&blurred_img.at<uchar>(dy*j,dx*i))>ENSEMBLE_THRESHOLD)){
continue; continue;
} }
pass++; pass++;
...@@ -603,10 +604,10 @@ bool TLDDetector::patchVariance(Mat_<double>& intImgP,Mat_<double>& intImgP2,dou ...@@ -603,10 +604,10 @@ bool TLDDetector::patchVariance(Mat_<double>& intImgP,Mat_<double>& intImgP2,dou
return ((p2-p*p)>VARIANCE_THRESHOLD*originalVariance); return ((p2-p*p)>VARIANCE_THRESHOLD*originalVariance);
} }
double TrackerTLDModel::ensembleClassifierNum(const uchar* data,int rowstep){ double TrackerTLDModel::ensembleClassifierNum(const uchar* data){
double p=0; double p=0;
for(int k=0;k<(int)classifiers.size();k++){ for(int k=0;k<(int)classifiers.size();k++){
p+=classifiers[k].posteriorProbability(data,rowstep); p+=classifiers[k].posteriorProbabilityFast(data);
} }
p/=classifiers.size(); p/=classifiers.size();
return p; return p;
...@@ -820,9 +821,10 @@ void MyMouseCallbackDEBUG::onMouse( int event, int x, int y){ ...@@ -820,9 +821,10 @@ void MyMouseCallbackDEBUG::onMouse( int event, int x, int y){
i=(int)(x/scale/dx), j=(int)(y/scale/dy); i=(int)(x/scale/dx), j=(int)(y/scale/dy);
dfprintf((stderr,"patchVariance=%s\n",(detector_->patchVariance(intImgP,intImgP2,originalVariance,Point(dx*i,dy*j),initSize))?"true":"false")); dfprintf((stderr,"patchVariance=%s\n",(detector_->patchVariance(intImgP,intImgP2,originalVariance,Point(dx*i,dy*j),initSize))?"true":"false"));
dfprintf((stderr,"p=%f\n",(tldModel->ensembleClassifierNum(&blurred_img.at<uchar>(dy*j,dx*i),(int)blurred_img.step[0])))); tldModel->prepareClassifiers((int)blurred_img.step[0]);
dfprintf((stderr,"p=%f\n",(tldModel->ensembleClassifierNum(&blurred_img.at<uchar>(dy*j,dx*i)))));
fprintf(stderr,"ensembleClassifier=%s\n", fprintf(stderr,"ensembleClassifier=%s\n",
(detector_->ensembleClassifier(&blurred_img.at<uchar>(dy*j,dx*i),(int)blurred_img.step[0]))?"true":"false"); (!(tldModel->ensembleClassifierNum(&blurred_img.at<uchar>(dy*j,dx*i))>ENSEMBLE_THRESHOLD))?"true":"false");
resample(resized_img,Rect2d(Point(dx*i,dy*j),initSize),standardPatch); resample(resized_img,Rect2d(Point(dx*i,dy*j),initSize),standardPatch);
tmp=tldModel->Sr(standardPatch); tmp=tldModel->Sr(standardPatch);
...@@ -859,8 +861,5 @@ void TrackerTLDModel::pushIntoModel(const Mat_<uchar>& example,bool positive){ ...@@ -859,8 +861,5 @@ void TrackerTLDModel::pushIntoModel(const Mat_<uchar>& example,bool positive){
} }
(*proxyN)++; (*proxyN)++;
} }
bool TLDDetector::ensembleClassifier(const uchar* data,int rowstep){
return (((TrackerTLDModel*)static_cast<TrackerModel*>(model))->ensembleClassifierNum(data,rowstep))>ENSEMBLE_THRESHOLD;
}
} /* namespace cv */ } /* namespace cv */
...@@ -96,12 +96,17 @@ public: ...@@ -96,12 +96,17 @@ public:
static int makeClassifiers(Size size,int measurePerClassifier,int gridSize,std::vector<TLDEnsembleClassifier>& classifiers); 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;
double posteriorProbabilityFast(const uchar* data)const;
void prepareClassifier(int rowstep);
private: private:
TLDEnsembleClassifier(std::vector<Vec4b> meas,int beg,int end); TLDEnsembleClassifier(std::vector<Vec4b> meas,int beg,int end);
static void stepPrefSuff(std::vector<Vec4b>& arr,int pos,int len,int gridSize); static void stepPrefSuff(std::vector<Vec4b>& arr,int pos,int len,int gridSize);
int code(const uchar* data,int rowstep)const; int code(const uchar* data,int rowstep)const;
int codeFast(const uchar* data)const;
std::vector<Point2i> posAndNeg; std::vector<Point2i> posAndNeg;
std::vector<Vec4b> measurements; std::vector<Vec4b> measurements;
std::vector<Point2i> offset;
int lastStep_;
}; };
class TrackerProxy{ class TrackerProxy{
......
...@@ -268,11 +268,21 @@ void TLDEnsembleClassifier::stepPrefSuff(std::vector<Vec4b>& arr,int pos,int len ...@@ -268,11 +268,21 @@ void TLDEnsembleClassifier::stepPrefSuff(std::vector<Vec4b>& arr,int pos,int len
} }
#endif #endif
} }
TLDEnsembleClassifier::TLDEnsembleClassifier(std::vector<Vec4b> meas,int beg,int end){ void TLDEnsembleClassifier::prepareClassifier(int rowstep){
int posSize=1; if(lastStep_!=rowstep){
for(int i=0,mpc=end-beg;i<mpc;i++)posSize*=2; lastStep_=rowstep;
for(int i=0;i<(int)offset.size();i++){
offset[i].x=rowstep*measurements[i].val[0]+measurements[i].val[1];
offset[i].y=rowstep*measurements[i].val[2]+measurements[i].val[3];
}
}
}
TLDEnsembleClassifier::TLDEnsembleClassifier(std::vector<Vec4b> meas,int beg,int end):lastStep_(-1){
int posSize=1,mpc=end-beg;
for(int i=0;i<mpc;i++)posSize*=2;
posAndNeg.assign(posSize,Point2i(0,0)); posAndNeg.assign(posSize,Point2i(0,0));
measurements.assign(meas.begin()+beg,meas.begin()+end); measurements.assign(meas.begin()+beg,meas.begin()+end);
offset.assign(mpc,Point2i(0,0));
} }
void TLDEnsembleClassifier::integrate(const Mat_<uchar>& patch,bool isPositive){ void TLDEnsembleClassifier::integrate(const Mat_<uchar>& patch,bool isPositive){
int position=code(patch.data,(int)patch.step[0]); int position=code(patch.data,(int)patch.step[0]);
...@@ -291,13 +301,31 @@ double TLDEnsembleClassifier::posteriorProbability(const uchar* data,int rowstep ...@@ -291,13 +301,31 @@ double TLDEnsembleClassifier::posteriorProbability(const uchar* data,int rowstep
return posNum/(posNum+negNum); return posNum/(posNum+negNum);
} }
} }
double TLDEnsembleClassifier::posteriorProbabilityFast(const uchar* data)const{
int position=codeFast(data);
double posNum=(double)posAndNeg[position].x, negNum=(double)posAndNeg[position].y;
if(posNum==0.0 && negNum==0.0){
return 0.0;
}else{
return posNum/(posNum+negNum);
}
}
int TLDEnsembleClassifier::codeFast(const uchar* data)const{
int position=0;
for(int i=0;i<(int)measurements.size();i++){
position=position<<1;
if(data[offset[i].x]<data[offset[i].y]){
position++;
}
}
return position;
}
int TLDEnsembleClassifier::code(const uchar* data,int rowstep)const{ int TLDEnsembleClassifier::code(const uchar* data,int rowstep)const{
unsigned short int position=0; int position=0;
for(int i=0;i<(int)measurements.size();i++){ for(int i=0;i<(int)measurements.size();i++){
position=position<<1; position=position<<1;
if(*(data+rowstep*measurements[i].val[0]+measurements[i].val[1])<*(data+rowstep*measurements[i].val[2]+measurements[i].val[3])){ if(*(data+rowstep*measurements[i].val[0]+measurements[i].val[1])<*(data+rowstep*measurements[i].val[2]+measurements[i].val[3])){
position++; position++;
}else{
} }
} }
return position; return position;
......
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