Skip to content

Commit

Permalink
converted Haar cascades to the new format; now they are handled with …
Browse files Browse the repository at this point in the history
…C++ code.
  • Loading branch information
vpisarev committed Dec 10, 2013
1 parent fdf1996 commit 302a5ad
Show file tree
Hide file tree
Showing 29 changed files with 503,139 additions and 584,144 deletions.
27,570 changes: 12,166 additions & 15,404 deletions data/haarcascades/haarcascade_eye.xml

Large diffs are not rendered by default.

55,682 changes: 22,572 additions & 33,110 deletions data/haarcascades/haarcascade_eye_tree_eyeglasses.xml

Large diffs are not rendered by default.

50,416 changes: 24,303 additions & 26,113 deletions data/haarcascades/haarcascade_frontalface_alt.xml

Large diffs are not rendered by default.

44,174 changes: 20,672 additions & 23,502 deletions data/haarcascades/haarcascade_frontalface_alt2.xml

Large diffs are not rendered by default.

199,880 changes: 96,436 additions & 103,444 deletions data/haarcascades/haarcascade_frontalface_alt_tree.xml

Large diffs are not rendered by default.

68,931 changes: 33,267 additions & 35,664 deletions data/haarcascades/haarcascade_frontalface_default.xml

Large diffs are not rendered by default.

34,865 changes: 16,889 additions & 17,976 deletions data/haarcascades/haarcascade_fullbody.xml

Large diffs are not rendered by default.

17,096 changes: 7,342 additions & 9,754 deletions data/haarcascades/haarcascade_lefteye_2splits.xml

Large diffs are not rendered by default.

28,858 changes: 13,915 additions & 14,943 deletions data/haarcascades/haarcascade_lowerbody.xml

Large diffs are not rendered by default.

19,545 changes: 8,703 additions & 10,842 deletions data/haarcascades/haarcascade_mcs_eyepair_big.xml

Large diffs are not rendered by default.

22,467 changes: 9,968 additions & 12,499 deletions data/haarcascades/haarcascade_mcs_eyepair_small.xml

Large diffs are not rendered by default.

16,768 changes: 7,512 additions & 9,256 deletions data/haarcascades/haarcascade_mcs_leftear.xml

Large diffs are not rendered by default.

42,790 changes: 19,086 additions & 23,704 deletions data/haarcascades/haarcascade_mcs_lefteye.xml

Large diffs are not rendered by default.

39,460 changes: 17,556 additions & 21,904 deletions data/haarcascades/haarcascade_mcs_mouth.xml

Large diffs are not rendered by default.

87,555 changes: 39,209 additions & 48,346 deletions data/haarcascades/haarcascade_mcs_nose.xml

Large diffs are not rendered by default.

17,414 changes: 7,809 additions & 9,605 deletions data/haarcascades/haarcascade_mcs_rightear.xml

Large diffs are not rendered by default.

76,147 changes: 33,982 additions & 42,165 deletions data/haarcascades/haarcascade_mcs_righteye.xml

Large diffs are not rendered by default.

83,439 changes: 37,197 additions & 46,242 deletions data/haarcascades/haarcascade_mcs_upperbody.xml

Large diffs are not rendered by default.

61,525 changes: 29,643 additions & 31,882 deletions data/haarcascades/haarcascade_profileface.xml

Large diffs are not rendered by default.

17,143 changes: 7,359 additions & 9,784 deletions data/haarcascades/haarcascade_righteye_2splits.xml

Large diffs are not rendered by default.

15,083 changes: 6,730 additions & 8,353 deletions data/haarcascades/haarcascade_smile.xml

Large diffs are not rendered by default.

57,618 changes: 27,993 additions & 29,625 deletions data/haarcascades/haarcascade_upperbody.xml

Large diffs are not rendered by default.

19 changes: 10 additions & 9 deletions modules/objdetect/src/cascadedetect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,16 +474,18 @@ HaarEvaluator::~HaarEvaluator()

bool HaarEvaluator::read(const FileNode& node)
{
features->resize(node.size());
featuresPtr = &(*features)[0];
FileNodeIterator it = node.begin(), it_end = node.end();
size_t i, n = node.size();
CV_Assert(n > 0);
features.resize(n);
featuresPtr = &features[0];
FileNodeIterator it = node.begin();
hasTiltedFeatures = false;

for(int i = 0; it != it_end; ++it, i++)
for(i = 0; i < n; i++, ++it)
{
if(!featuresPtr[i].read(*it))
if(!features[i].read(*it))
return false;
if( featuresPtr[i].tilted )
if( features[i].tilted )
hasTiltedFeatures = true;
}
return true;
Expand All @@ -494,7 +496,6 @@ Ptr<FeatureEvaluator> HaarEvaluator::clone() const
Ptr<HaarEvaluator> ret = makePtr<HaarEvaluator>();
ret->origWinSize = origWinSize;
ret->features = features;
ret->featuresPtr = &(*ret->features)[0];
ret->hasTiltedFeatures = hasTiltedFeatures;
ret->sum0 = sum0, ret->sqsum0 = sqsum0, ret->tilted0 = tilted0;
ret->sum = sum, ret->sqsum = sqsum, ret->tilted = tilted;
Expand Down Expand Up @@ -540,10 +541,10 @@ bool HaarEvaluator::setImage( const Mat &image, Size _origWinSize )
CV_SUM_PTRS( p[0], p[1], p[2], p[3], sdata, normrect, sumStep );
CV_SUM_PTRS( pq[0], pq[1], pq[2], pq[3], sqdata, normrect, sqsumStep );

size_t fi, nfeatures = features->size();
size_t fi, nfeatures = features.size();

for( fi = 0; fi < nfeatures; fi++ )
featuresPtr[fi].updatePtrs( !featuresPtr[fi].tilted ? sum : tilted );
optfeaturesPtr[fi].updatePtrs( !featuresPtr[fi].tilted ? sum : tilted );
return true;
}

Expand Down
73 changes: 59 additions & 14 deletions modules/objdetect/src/cascadedetect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,32 @@ class CascadeClassifierImpl : public BaseCascadeClassifier

#define CALC_SUM(rect,offset) CALC_SUM_((rect)[0], (rect)[1], (rect)[2], (rect)[3], offset)

#define CV_SUM_OFS( p0, p1, p2, p3, sum, rect, step ) \
/* (x, y) */ \
(p0) = sum + (rect).x + (step) * (rect).y, \
/* (x + w, y) */ \
(p1) = sum + (rect).x + (rect).width + (step) * (rect).y, \
/* (x + w, y) */ \
(p2) = sum + (rect).x + (step) * ((rect).y + (rect).height), \
/* (x + w, y + h) */ \
(p3) = sum + (rect).x + (rect).width + (step) * ((rect).y + (rect).height)

#define CV_TILTED_OFS( p0, p1, p2, p3, tilted, rect, step ) \
/* (x, y) */ \
(p0) = tilted + (rect).x + (step) * (rect).y, \
/* (x - h, y + h) */ \
(p1) = tilted + (rect).x - (rect).height + (step) * ((rect).y + (rect).height), \
/* (x + w, y + w) */ \
(p2) = tilted + (rect).x + (rect).width + (step) * ((rect).y + (rect).width), \
/* (x + w - h, y + w + h) */ \
(p3) = tilted + (rect).x + (rect).width - (rect).height \
+ (step) * ((rect).y + (rect).width + (rect).height)

#define CALC_SUM_(p0, p1, p2, p3, offset) \
((p0)[offset] - (p1)[offset] - (p2)[offset] + (p3)[offset])

#define CALC_SUM(rect,offset) CALC_SUM_((rect)[0], (rect)[1], (rect)[2], (rect)[3], offset)


//---------------------------------------------- HaarEvaluator ---------------------------------------
class HaarEvaluator : public FeatureEvaluator
Expand All @@ -195,8 +221,6 @@ class HaarEvaluator : public FeatureEvaluator
{
Feature();

float calc( int offset ) const;
void updatePtrs( const Mat& sum );
bool read( const FileNode& node );

bool tilted;
Expand All @@ -208,8 +232,19 @@ class HaarEvaluator : public FeatureEvaluator
Rect r;
float weight;
} rect[RECT_NUM];
};

struct OptFeature
{
OptFeature();

const int* p[RECT_NUM][4];
enum { RECT_NUM = Feature::RECT_NUM };
float calc( const int* pwin ) const;

void setPtrs( const Mat& sum, const Feature& f );

int ofs[RECT_NUM][4];
float weight[RECT_NUM];
};

HaarEvaluator();
Expand All @@ -223,23 +258,26 @@ class HaarEvaluator : public FeatureEvaluator
virtual bool setWindow(Point pt);

double operator()(int featureIdx) const
{ return featuresPtr[featureIdx].calc(offset) * varianceNormFactor; }
{ return optfeaturesPtr[featureIdx].calc(pwin) * varianceNormFactor; }
virtual double calcOrd(int featureIdx) const
{ return (*this)(featureIdx); }

protected:
Size origWinSize;
Ptr<std::vector<Feature> > features;
Feature* featuresPtr; // optimization
std::vector<Feature> features;
std::vector<OptFeature> optfeatures;
OptFeature* optfeaturesPtr; // optimization
bool hasTiltedFeatures;

Mat sum0, sqsum0, tilted0;
Mat sum, sqsum, tilted;

Rect normrect;
const int *p[4];
const double *pq[4];
int p[4];
int pq[4];

const int* pwin;
const double* pqwin;
int offset;
double varianceNormFactor;
};
Expand All @@ -249,25 +287,32 @@ inline HaarEvaluator::Feature :: Feature()
tilted = false;
rect[0].r = rect[1].r = rect[2].r = Rect();
rect[0].weight = rect[1].weight = rect[2].weight = 0;
p[0][0] = p[0][1] = p[0][2] = p[0][3] =
p[1][0] = p[1][1] = p[1][2] = p[1][3] =
p[2][0] = p[2][1] = p[2][2] = p[2][3] = 0;
}

inline float HaarEvaluator::Feature :: calc( int _offset ) const
inline HaarEvaluator::OptFeature :: OptFeature()
{
weight[0] = weight[1] = weight[2] = 0.f;

ofs[0][0] = ofs[0][1] = ofs[0][2] = ofs[0][3] =
ofs[1][0] = ofs[1][1] = ofs[1][2] = ofs[1][3] =
ofs[2][0] = ofs[2][1] = ofs[2][2] = ofs[2][3] = 0;
}

/*inline float HaarEvaluator::Feature :: calc( int _offset ) const
{
float ret = rect[0].weight * CALC_SUM(p[0], _offset) + rect[1].weight * CALC_SUM(p[1], _offset);
if( rect[2].weight != 0.0f )
ret += rect[2].weight * CALC_SUM(p[2], _offset);
return ret;
}
}*/

inline void HaarEvaluator::Feature :: updatePtrs( const Mat& _sum )
inline void HaarEvaluator::OptFeature :: setPtrs( const Mat& _sum, const Feature& _f )
{
const int* ptr = (const int*)_sum.data;
size_t step = _sum.step/sizeof(ptr[0]);
size_t tiltedofs =
if (tilted)
{
CV_TILTED_PTRS( p[0][0], p[0][1], p[0][2], p[0][3], ptr, rect[0].r, step );
Expand Down
9 changes: 5 additions & 4 deletions modules/objdetect/src/cascadedetect_convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ static bool convert(const String& oldcascade, const String& newcascade)
for( i = 0; i < nstages; i++ )
maxWeakCount = std::max(maxWeakCount, stages[i].weaks.size());

newfs << "stageType" << "BOOST"
newfs << "cascade" << "{:opencv-cascade-classifier"
<< "stageType" << "BOOST"
<< "featureType" << "HAAR"
<< "height" << cascadesize.width
<< "width" << cascadesize.height
Expand Down Expand Up @@ -250,16 +251,16 @@ static bool convert(const String& oldcascade, const String& newcascade)
{
if( j >= 2 && fabs(f.rect[j].weight) < FLT_EPSILON )
break;
newfs << f.rect[j].r.x << f.rect[j].r.y <<
f.rect[j].r.width << f.rect[j].r.height << f.rect[j].weight;
newfs << "[" << f.rect[j].r.x << f.rect[j].r.y <<
f.rect[j].r.width << f.rect[j].r.height << f.rect[j].weight << "]";
}
newfs << "]";
if( f.tilted )
newfs << "tilted" << 1;
newfs << "}";
}

newfs << "]";
newfs << "]" << "}";
return true;
}

Expand Down
Loading

0 comments on commit 302a5ad

Please sign in to comment.