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

vadim 21

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