coding style

pull/55/head
Alex Leontiev 11 years ago
parent b6b8196261
commit ed4aeef77f
  1. 639
      modules/tracking/src/tld_tracker.cpp
  2. 29
      modules/tracking/src/tld_tracker.hpp
  3. 260
      modules/tracking/src/tld_utils.cpp

File diff suppressed because it is too large Load Diff

@ -42,8 +42,8 @@
#include "precomp.hpp" #include "precomp.hpp"
#include "opencv2/video/tracking.hpp" #include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc.hpp" #include "opencv2/imgproc.hpp"
#include <algorithm> #include<algorithm>
#include <limits.h> #include<limits.h>
namespace cv {namespace tld namespace cv {namespace tld
{ {
@ -58,22 +58,21 @@ namespace cv {namespace tld
#define dprintf(x) #define dprintf(x)
#endif #endif
#define MEASURE_TIME(a) {\ #define MEASURE_TIME(a) {\
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(("line %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)); }
extern Rect2d etalon; extern Rect2d etalon;
void myassert(const Mat& img); void myassert(const Mat& img);
void printPatch(const Mat_<uchar>& standardPatch); void printPatch(const Mat_<uchar>& standardPatch);
std::string type2str(const Mat& mat); std::string type2str(const Mat& mat);
void drawWithRects(const Mat& img,std::vector<Rect2d>& blackOnes,Rect2d whiteOne=Rect2d(-1.0,-1.0,-1.0,-1.0)); void drawWithRects(const Mat& img,std::vector<Rect2d>& blackOnes,Rect2d whiteOne = Rect2d(-1.0,-1.0,-1.0,-1.0));
void drawWithRects(const Mat& img,std::vector<Rect2d>& blackOnes,std::vector<Rect2d>& whiteOnes,String fileName=""); void drawWithRects(const Mat& img,std::vector<Rect2d>& blackOnes,std::vector<Rect2d>& whiteOnes,String fileName = "");
//aux functions and variables //aux functions and variables
//#define CLIP(x,a,b) std::min(std::max((x),(a)),(b))
template<typename T> inline T CLIP(T x,T a,T b){return std::min(std::max(x,a),b);} template<typename T> inline T CLIP(T x,T a,T b){return std::min(std::max(x,a),b);}
/** Computes overlap between the two given rectangles. Overlap is computed as ratio of rectangles' intersection to that /** Computes overlap between the two given rectangles. Overlap is computed as ratio of rectangles' intersection to that
* of their union.*/ * of their union.*/
@ -88,8 +87,8 @@ double variance(const Mat& img);
* of the same size).*/ * of the same size).*/
double NCC(const Mat_<uchar>& patch1,const Mat_<uchar>& patch2); double NCC(const Mat_<uchar>& patch1,const Mat_<uchar>& patch2);
void getClosestN(std::vector<Rect2d>& scanGrid,Rect2d bBox,int n,std::vector<Rect2d>& res); void getClosestN(std::vector<Rect2d>& scanGrid,Rect2d bBox,int n,std::vector<Rect2d>& res);
double scaleAndBlur(const Mat& originalImg,int scale,Mat& scaledImg,Mat& blurredImg,Size GaussBlurKernelSize); double scaleAndBlur(const Mat& originalImg,int scale,Mat& scaledImg,Mat& blurredImg,Size GaussBlurKernelSize, double scaleStep);
unsigned int getMedian(const std::vector<unsigned int>& values, int size=-1); int getMedian(const std::vector<int>& values, int size = -1);
class TLDEnsembleClassifier{ class TLDEnsembleClassifier{
public: public:
@ -99,8 +98,8 @@ public:
double posteriorProbabilityFast(const uchar* data)const; double posteriorProbabilityFast(const uchar* data)const;
void prepareClassifier(int rowstep); void prepareClassifier(int rowstep);
private: private:
TLDEnsembleClassifier(std::vector<Vec4b> meas,int beg,int end); TLDEnsembleClassifier(const 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; int codeFast(const uchar* data)const;
std::vector<Point2i> posAndNeg; std::vector<Point2i> posAndNeg;
@ -111,8 +110,8 @@ private:
class TrackerProxy{ class TrackerProxy{
public: public:
virtual bool init( const Mat& image, const Rect2d& boundingBox)=0; virtual bool init( const Mat& image, const Rect2d& boundingBox) = 0;
virtual bool update(const Mat& image, Rect2d& boundingBox)=0; virtual bool update(const Mat& image, Rect2d& boundingBox) = 0;
virtual ~TrackerProxy(){} virtual ~TrackerProxy(){}
}; };

@ -43,10 +43,10 @@
#include "opencv2/video/tracking.hpp" #include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc.hpp" #include "opencv2/imgproc.hpp"
#include "time.h" #include "time.h"
#include <algorithm> #include<algorithm>
#include <limits.h> #include<limits.h>
#include <math.h> #include<math.h>
#include <opencv2/highgui.hpp> #include<opencv2/highgui.hpp>
#include "tld_tracker.hpp" #include "tld_tracker.hpp"
namespace cv {namespace tld namespace cv {namespace tld
@ -57,26 +57,26 @@ Rect2d etalon(14.0,110.0,20.0,20.0);
void drawWithRects(const Mat& img,std::vector<Rect2d>& blackOnes,Rect2d whiteOne){ void drawWithRects(const Mat& img,std::vector<Rect2d>& blackOnes,Rect2d whiteOne){
Mat image; Mat image;
img.copyTo(image); img.copyTo(image);
if(whiteOne.width>=0){ if(whiteOne.width >= 0){
rectangle( image,whiteOne, 255, 1, 1 ); rectangle( image,whiteOne, 255, 1, 1 );
} }
for(int i=0;i<(int)blackOnes.size();i++){ for(int i = 0;i < (int)blackOnes.size();i++){
rectangle( image,blackOnes[i], 0, 1, 1 ); rectangle( image,blackOnes[i], 0, 1, 1 );
} }
imshow("img",image); imshow("img",image);
} }
void drawWithRects(const Mat& img,std::vector<Rect2d>& blackOnes,std::vector<Rect2d>& whiteOnes,String filename){ void drawWithRects(const Mat& img,std::vector<Rect2d>& blackOnes,std::vector<Rect2d>& whiteOnes,String filename){
Mat image; Mat image;
static int frameCounter=1; static int frameCounter = 1;
img.copyTo(image); img.copyTo(image);
for(int i=0;i<(int)whiteOnes.size();i++){ for(int i = 0;i < (int)whiteOnes.size();i++){
rectangle( image,whiteOnes[i], 255, 1, 1 ); rectangle( image,whiteOnes[i], 255, 1, 1 );
} }
for(int i=0;i<(int)blackOnes.size();i++){ for(int i = 0;i < (int)blackOnes.size();i++){
rectangle( image,blackOnes[i], 0, 1, 1 ); rectangle( image,blackOnes[i], 0, 1, 1 );
} }
imshow("img",image); imshow("img",image);
if(filename.length()>0){ if(filename.length() > 0){
char inbuf[100]; char inbuf[100];
sprintf(inbuf,"%s%d.jpg",filename.c_str(),frameCounter); sprintf(inbuf,"%s%d.jpg",filename.c_str(),frameCounter);
imwrite(inbuf,image); imwrite(inbuf,image);
@ -84,20 +84,20 @@ void drawWithRects(const Mat& img,std::vector<Rect2d>& blackOnes,std::vector<Rec
} }
} }
void myassert(const Mat& img){ void myassert(const Mat& img){
int count=0; int count = 0;
for(int i=0;i<img.rows;i++){ for(int i = 0;i < img.rows;i++){
for(int j=0;j<img.cols;j++){ for(int j = 0;j < img.cols;j++){
if(img.at<uchar>(i,j)==0){ if(img.at<uchar>(i,j) == 0){
count++; count++;
} }
} }
} }
dprintf(("black: %d out of %d (%f)\n",count,img.rows*img.cols,1.0*count/img.rows/img.cols)); dprintf(("black: %d out of %d (%f)\n",count,img.rows * img.cols,1.0 * count / img.rows / img.cols));
} }
void printPatch(const Mat_<uchar>& standardPatch){ void printPatch(const Mat_<uchar>& standardPatch){
for(int i=0;i<standardPatch.rows;i++){ for(int i = 0;i < standardPatch.rows;i++){
for(int j=0;j<standardPatch.cols;j++){ for(int j = 0;j < standardPatch.cols;j++){
dprintf(("%5.2f, ",(double)standardPatch(i,j))); dprintf(("%5.2f, ",(double)standardPatch(i,j)));
} }
dprintf(("\n")); dprintf(("\n"));
@ -105,11 +105,11 @@ void printPatch(const Mat_<uchar>& standardPatch){
} }
std::string type2str(const Mat& mat){ std::string type2str(const Mat& mat){
int type=mat.type(); int type = mat.type();
std::string r; std::string r;
uchar depth = type & CV_MAT_DEPTH_MASK; uchar depth = type & CV_MAT_DEPTH_MASK;
uchar chans =(uchar)( 1 + (type >> CV_CN_SHIFT)); uchar chans = (uchar)(1 + (type >> CV_CN_SHIFT));
switch ( depth ) { switch ( depth ) {
case CV_8U: r = "8U"; break; case CV_8U: r = "8U"; break;
@ -123,31 +123,31 @@ std::string type2str(const Mat& mat){
} }
r += "C"; r += "C";
r += (chans+'0'); r += (chans + '0');
return r; return r;
} }
//generic functions //generic functions
double scaleAndBlur(const Mat& originalImg,int scale,Mat& scaledImg,Mat& blurredImg,Size GaussBlurKernelSize){ double scaleAndBlur(const Mat& originalImg,int scale,Mat& scaledImg,Mat& blurredImg,Size GaussBlurKernelSize, double scaleStep){
double dScale=1.0; double dScale = 1.0;
for(int i=0;i<scale;i++,dScale*=1.2); for(int i = 0; i < scale; i++, dScale *= scaleStep);
Size2d size=originalImg.size(); Size2d size = originalImg.size();
size.height/=dScale;size.width/=dScale; size.height /= dScale; size.width /= dScale;
resize(originalImg,scaledImg,size); resize(originalImg,scaledImg,size);
GaussianBlur(scaledImg,blurredImg,GaussBlurKernelSize,0.0); GaussianBlur(scaledImg,blurredImg,GaussBlurKernelSize,0.0);
return dScale; return dScale;
} }
void getClosestN(std::vector<Rect2d>& scanGrid,Rect2d bBox,int n,std::vector<Rect2d>& res){ void getClosestN(std::vector<Rect2d>& scanGrid,Rect2d bBox,int n,std::vector<Rect2d>& res){
if(n>=(int)scanGrid.size()){ if(n >= (int)scanGrid.size()){
res.assign(scanGrid.begin(),scanGrid.end()); res.assign(scanGrid.begin(),scanGrid.end());
return; return;
} }
std::vector<double> overlaps; std::vector<double> overlaps;
overlaps.assign(n,0.0); overlaps.assign(n,0.0);
res.assign(scanGrid.begin(),scanGrid.begin()+n); res.assign(scanGrid.begin(),scanGrid.begin() + n);
for(int i=0;i<n;i++){ for(int i = 0;i < n;i++){
overlaps[i]=overlap(res[i],bBox); overlaps[i] = overlap(res[i],bBox);
} }
double otmp; double otmp;
Rect2d rtmp; Rect2d rtmp;
@ -160,142 +160,144 @@ void getClosestN(std::vector<Rect2d>& scanGrid,Rect2d bBox,int n,std::vector<Rec
} }
} }
for(int i=n;i<(int)scanGrid.size();i++){ for(int i = n;i < (int)scanGrid.size();i++){
double o=0.0; double o = 0.0;
if((o=overlap(scanGrid[i],bBox))<=overlaps[0]){ if((o = overlap(scanGrid[i],bBox)) <= overlaps[0]){
continue; continue;
} }
int j=0; int j = 0;
while( j < n && overlaps[j] < o ) while( j < n && overlaps[j] < o )
{ {
j++; j++;
} }
j--; j--;
for(int k=0;k<j;overlaps[k]=overlaps[k+1],res[k]=res[k+1],k++); for(int k = 0;k < j;overlaps[k] = overlaps[k + 1],res[k] = res[k + 1],k++);
overlaps[j]=o;res[j]=scanGrid[i]; overlaps[j] = o;res[j] = scanGrid[i];
} }
} }
double variance(const Mat& img){ double variance(const Mat& img){
double p=0,p2=0; double p = 0,p2 = 0;
for(int i=0;i<img.rows;i++){ for(int i = 0;i < img.rows;i++){
for(int j=0;j<img.cols;j++){ for(int j = 0;j < img.cols;j++){
p+=img.at<uchar>(i,j); p += img.at<uchar>(i,j);
p2+=img.at<uchar>(i,j)*img.at<uchar>(i,j); p2 += img.at<uchar>(i,j) * img.at<uchar>(i,j);
} }
} }
p/=(img.cols*img.rows); p /= (img.cols * img.rows);
p2/=(img.cols*img.rows); p2 /= (img.cols * img.rows);
return p2-p*p; return p2 - p * p;
} }
double NCC(const Mat_<uchar>& patch1,const Mat_<uchar>& patch2){ double NCC(const Mat_<uchar>& patch1,const Mat_<uchar>& patch2){
CV_Assert(patch1.rows==patch2.rows); CV_Assert(patch1.rows == patch2.rows);
CV_Assert(patch1.cols==patch2.cols); CV_Assert(patch1.cols == patch2.cols);
int N=patch1.rows*patch1.cols; int N = patch1.rows * patch1.cols;
int s1=0,s2=0,n1=0,n2=0,prod=0; int s1 = 0,s2 = 0,n1 = 0,n2 = 0,prod = 0;
for(int i=0;i<patch1.rows;i++){ for(int i = 0;i < patch1.rows;i++){
for(int j=0;j<patch1.cols;j++){ for(int j = 0;j < patch1.cols;j++){
int p1=patch1(i,j), p2=patch2(i,j); int p1 = patch1(i,j), p2 = patch2(i,j);
s1+=p1; s2+=p2; s1 += p1; s2 += p2;
n1+=(p1*p1); n2+=(p2*p2); n1 += (p1 * p1); n2 += (p2 * p2);
prod+=(p1*p2); prod += (p1 * p2);
} }
} }
double sq1=sqrt(std::max(0.0,n1-1.0*s1*s1/N)),sq2=sqrt(std::max(0.0,n2-1.0*s2*s2/N)); double sq1 = sqrt(std::max(0.0,n1 - 1.0 * s1 * s1 / N)),sq2 = sqrt(std::max(0.0,n2 - 1.0 * s2 * s2 / N));
double ares=(sq2==0)?sq1/abs(sq1):(prod-s1*s2/N)/sq1/sq2; double ares = (sq2 == 0) ? sq1 / abs(sq1) : (prod - s1 * s2 / N) / sq1 / sq2;
return ares; return ares;
} }
unsigned int getMedian(const std::vector<unsigned int>& values, int size){ int getMedian(const std::vector<int>& values, int size){
if(size==-1){ if(size == -1){
size=(int)values.size(); size = (int)values.size();
} }
std::vector<int> copy(values.begin(),values.begin()+size); std::vector<int> copy(values.begin(),values.begin() + size);
std::sort(copy.begin(),copy.end()); std::sort(copy.begin(),copy.end());
if(size%2==0){ if(size % 2 == 0){
return (copy[size/2-1]+copy[size/2])/2; return (copy[size / 2 - 1] + copy[size / 2]) / 2;
}else{ }else{
return copy[(size-1)/2]; return copy[(size - 1) / 2];
} }
} }
double overlap(const Rect2d& r1,const Rect2d& r2){ double overlap(const Rect2d& r1,const Rect2d& r2){
double a1=r1.area(), a2=r2.area(), a0=(r1&r2).area(); double a1 = r1.area(), a2 = r2.area(), a0 = (r1&r2).area();
return a0/(a1+a2-a0); return a0 / (a1 + a2 - a0);
} }
void resample(const Mat& img,const RotatedRect& r2,Mat_<uchar>& samples){ void resample(const Mat& img,const RotatedRect& r2,Mat_<uchar>& samples){
Mat_<float> M(2,3),R(2,2),Si(2,2),s(2,1),o(2,1); Mat_<float> M(2,3),R(2,2),Si(2,2),s(2,1),o(2,1);
R(0,0)=(float)cos(r2.angle*CV_PI/180);R(0,1)=(float)(-sin(r2.angle*CV_PI/180)); R(0,0) = (float)cos(r2.angle * CV_PI / 180);R(0,1) = (float)(-sin(r2.angle * CV_PI / 180));
R(1,0)=(float)sin(r2.angle*CV_PI/180);R(1,1)=(float)cos(r2.angle*CV_PI/180); R(1,0) = (float)sin(r2.angle * CV_PI / 180);R(1,1) = (float)cos(r2.angle * CV_PI / 180);
Si(0,0)=(float)(samples.cols/r2.size.width); Si(0,1)=0.0f; Si(0,0) = (float)(samples.cols / r2.size.width); Si(0,1) = 0.0f;
Si(1,0)=0.0f; Si(1,1)=(float)(samples.rows/r2.size.height); Si(1,0) = 0.0f; Si(1,1) = (float)(samples.rows / r2.size.height);
s(0,0)=(float)samples.cols; s(1,0)=(float)samples.rows; s(0,0) = (float)samples.cols; s(1,0) = (float)samples.rows;
o(0,0)=r2.center.x;o(1,0)=r2.center.y; o(0,0) = r2.center.x;o(1,0) = r2.center.y;
Mat_<float> A(2,2),b(2,1); Mat_<float> A(2,2),b(2,1);
A=Si*R; A = Si * R;
b=s/2.0-Si*R*o; b = s / 2.0 - Si * R * o;
A.copyTo(M.colRange(Range(0,2))); A.copyTo(M.colRange(Range(0,2)));
b.copyTo(M.colRange(Range(2,3))); b.copyTo(M.colRange(Range(2,3)));
warpAffine(img,samples,M,samples.size()); warpAffine(img,samples,M,samples.size());
} }
void resample(const Mat& img,const Rect2d& r2,Mat_<uchar>& samples){ void resample(const Mat& img,const Rect2d& r2,Mat_<uchar>& samples){
Mat_<float> M(2,3); Mat_<float> M(2,3);
M(0,0)=(float)(samples.cols/r2.width); M(0,1)=0.0f; M(0,2)=(float)(-r2.x*samples.cols/r2.width); 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); 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()); warpAffine(img,samples,M,samples.size());
} }
//other stuff //other stuff
void TLDEnsembleClassifier::stepPrefSuff(std::vector<Vec4b>& arr,int pos,int len,int gridSize){ void TLDEnsembleClassifier::stepPrefSuff(std::vector<Vec4b>& arr,int pos,int len,int gridSize){
#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++){
arr[i]=pref+arr[i]*step; arr[i] = pref + arr[i] * step;
} }
#else #else
int total=len-gridSize; int total = len - gridSize;
int quo=total/(gridSize-1),rem=total%(gridSize-1); int quo = total / (gridSize - 1),rem = total % (gridSize - 1);
int smallStep=quo,bigStep=quo+1; int smallStep = quo,bigStep = quo + 1;
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].val[pos]<bigOnes_back){ if(arr[i].val[pos] < bigOnes_back){
arr[i].val[pos]=(uchar)(arr[i].val[pos]*bigStep+arr[i].val[pos]); arr[i].val[pos] = (uchar)(arr[i].val[pos] * bigStep + arr[i].val[pos]);
continue; continue;
} }
if(arr[i].val[pos]<(bigOnes_front+smallOnes)){ if(arr[i].val[pos] < (bigOnes_front + smallOnes)){
arr[i].val[pos]=(uchar)(bigOnes_front*bigStep+(arr[i].val[pos]-bigOnes_front)*smallStep+arr[i].val[pos]); arr[i].val[pos] = (uchar)(bigOnes_front * bigStep + (arr[i].val[pos] - bigOnes_front) * smallStep + arr[i].val[pos]);
continue; continue;
} }
if(arr[i].val[pos]<(bigOnes_front+smallOnes+bigOnes_back)){ if(arr[i].val[pos] < (bigOnes_front + smallOnes + bigOnes_back)){
arr[i].val[pos]= arr[i].val[pos] =
(uchar)(bigOnes_front*bigStep+smallOnes*smallStep+(arr[i].val[pos]-(bigOnes_front+smallOnes))*bigStep+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].val[pos]=(uchar)(len-1); arr[i].val[pos] = (uchar)(len - 1);
} }
#endif #endif
} }
void TLDEnsembleClassifier::prepareClassifier(int rowstep){ void TLDEnsembleClassifier::prepareClassifier(int rowstep){
if(lastStep_!=rowstep){ if(lastStep_ != rowstep){
lastStep_=rowstep; lastStep_ = rowstep;
for(int i=0;i<(int)offset.size();i++){ for(int i = 0;i < (int)offset.size();i++){
offset[i].x=rowstep*measurements[i].val[0]+measurements[i].val[1]; offset[i].x = rowstep * measurements[i].val[0] + measurements[i].val[1];
offset[i].y=rowstep*measurements[i].val[2]+measurements[i].val[3]; offset[i].y = rowstep * measurements[i].val[2] + measurements[i].val[3];
} }
} }
} }
TLDEnsembleClassifier::TLDEnsembleClassifier(std::vector<Vec4b> meas,int beg,int end):lastStep_(-1){ TLDEnsembleClassifier::TLDEnsembleClassifier(const std::vector<Vec4b>& meas,int beg,int end):lastStep_(-1){
int posSize=1,mpc=end-beg; int posSize = 1, mpc = end - beg;
for(int i=0;i<mpc;i++)posSize*=2; 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)); 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]);
if(isPositive){ if(isPositive){
posAndNeg[position].x++; posAndNeg[position].x++;
}else{ }else{
@ -303,38 +305,40 @@ void TLDEnsembleClassifier::integrate(const Mat_<uchar>& patch,bool isPositive){
} }
} }
double TLDEnsembleClassifier::posteriorProbability(const uchar* data,int rowstep)const{ double TLDEnsembleClassifier::posteriorProbability(const uchar* data,int rowstep)const{
int position=code(data,rowstep); int position = code(data,rowstep);
double posNum=(double)posAndNeg[position].x, negNum=(double)posAndNeg[position].y; double posNum = (double)posAndNeg[position].x, negNum = (double)posAndNeg[position].y;
if(posNum==0.0 && negNum==0.0){ if(posNum == 0.0 && negNum == 0.0){
return 0.0; return 0.0;
}else{ }else{
return posNum/(posNum+negNum); return posNum / (posNum + negNum);
} }
} }
double TLDEnsembleClassifier::posteriorProbabilityFast(const uchar* data)const{ double TLDEnsembleClassifier::posteriorProbabilityFast(const uchar* data)const{
int position=codeFast(data); int position = codeFast(data);
double posNum=(double)posAndNeg[position].x, negNum=(double)posAndNeg[position].y; double posNum = (double)posAndNeg[position].x, negNum = (double)posAndNeg[position].y;
if(posNum==0.0 && negNum==0.0){ if(posNum == 0.0 && negNum == 0.0){
return 0.0; return 0.0;
}else{ }else{
return posNum/(posNum+negNum); return posNum / (posNum + negNum);
} }
} }
int TLDEnsembleClassifier::codeFast(const uchar* data)const{ int TLDEnsembleClassifier::codeFast(const uchar* data)const{
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[offset[i].x]<data[offset[i].y]){ if(data[offset[i].x] < data[offset[i].y]){
position++; position++;
} }
} }
return position; return position;
} }
int TLDEnsembleClassifier::code(const uchar* data,int rowstep)const{ int TLDEnsembleClassifier::code(const uchar* data,int rowstep)const{
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++;
} }
} }
@ -345,15 +349,15 @@ int TLDEnsembleClassifier::makeClassifiers(Size size,int measurePerClassifier,in
std::vector<Vec4b> measurements; std::vector<Vec4b> measurements;
for(int i=0;i<gridSize;i++){ for(int i = 0;i < gridSize;i++){
for(int j=0;j<gridSize;j++){ for(int j = 0;j < gridSize;j++){
for(int k=0;k<j;k++){ for(int k = 0;k < j;k++){
Vec4b m; Vec4b m;
m.val[0]=m.val[2]=i; m.val[0] = m.val[2] = i;
m.val[1]=j;m.val[3]=k; m.val[1] = j;m.val[3] = k;
measurements.push_back(m); measurements.push_back(m);
m.val[1]=m.val[3]=i; m.val[1] = m.val[3] = i;
m.val[0]=j;m.val[2]=k; m.val[0] = j;m.val[2] = k;
measurements.push_back(m); measurements.push_back(m);
} }
} }
@ -365,8 +369,8 @@ int TLDEnsembleClassifier::makeClassifiers(Size size,int measurePerClassifier,in
stepPrefSuff(measurements,2,size.height,gridSize); stepPrefSuff(measurements,2,size.height,gridSize);
stepPrefSuff(measurements,3,size.height,gridSize); stepPrefSuff(measurements,3,size.height,gridSize);
for(int i=0,howMany=measurements.size()/measurePerClassifier;i<howMany;i++){ for(int i = 0,howMany = measurements.size() / measurePerClassifier;i < howMany;i++){
classifiers.push_back(TLDEnsembleClassifier(measurements,i*measurePerClassifier,(i+1)*measurePerClassifier)); classifiers.push_back(TLDEnsembleClassifier(measurements,i * measurePerClassifier, (i + 1) * measurePerClassifier));
} }
return (int)classifiers.size(); return (int)classifiers.size();
} }

Loading…
Cancel
Save