1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
48 // class for grouping object candidates, detected by Cascade Classifier, HOG etc.
49 // instance of the class is to be passed to cv::partition (see cxoperations.hpp)
50 class CV_EXPORTS SimilarRects
53 SimilarRects(double _eps) : eps(_eps) {}
54 inline bool operator()(const Rect& r1, const Rect& r2) const
56 double delta = eps*(std::min(r1.width, r2.width) + std::min(r1.height, r2.height))*0.5;
57 return std::abs(r1.x - r2.x) <= delta &&
58 std::abs(r1.y - r2.y) <= delta &&
59 std::abs(r1.x + r1.width - r2.x - r2.width) <= delta &&
60 std::abs(r1.y + r1.height - r2.y - r2.height) <= delta;
66 void groupRectangles(Vector<Rect>& rectList, int groupThreshold, double eps)
68 if( groupThreshold <= 0 )
72 int nclasses = partition(rectList, labels, SimilarRects(eps));
73 Vector<Rect> rrects(nclasses);
74 Vector<int> rweights(nclasses, 0);
75 int i, nlabels = (int)labels.size();
76 for( i = 0; i < nlabels; i++ )
79 rrects[cls].x += rectList[i].x;
80 rrects[cls].y += rectList[i].y;
81 rrects[cls].width += rectList[i].width;
82 rrects[cls].height += rectList[i].height;
86 for( i = 0; i < nclasses; i++ )
89 if( rweights[i] <= groupThreshold )
91 float s = 1.f/rweights[i];
92 rectList.push_back(Rect(saturate_cast<int>(r.x*s),
93 saturate_cast<int>(r.y*s),
94 saturate_cast<int>(r.width*s),
95 saturate_cast<int>(r.height*s)));
99 //-----------------------------------------------------------------------------------------------------------------
100 #define CC_CASCADE_PARAMS "cascadeParams"
101 #define CC_STAGE_TYPE "stageType"
102 #define CC_FEATURE_TYPE "featureType"
103 #define CC_HEIGHT "height"
104 #define CC_WIDTH "width"
106 #define CC_STAGE_NUM "stageNum"
107 #define CC_STAGES "stages"
108 #define CC_STAGE_PARAMS "stageParams"
110 #define CC_BOOST "BOOST"
111 #define CC_MAX_DEPTH "maxDepth"
112 #define CC_WEAK_COUNT "maxWeakCount"
113 #define CC_STAGE_THRESHOLD "stageThreshold"
114 #define CC_WEAK_CLASSIFIERS "weakClassifiers"
115 #define CC_INTERNAL_NODES "internalNodes"
116 #define CC_LEAF_VALUES "leafValues"
118 #define CC_FEATURES "features"
119 #define CC_FEATURE_PARAMS "featureParams"
120 #define CC_MAX_CAT_COUNT "maxCatCount"
122 #define CC_HAAR "HAAR"
123 #define CC_RECTS "rects"
124 #define CC_TILTED "tilted"
127 #define CC_RECT "rect"
129 #define CV_SUM_PTRS( p0, p1, p2, p3, sum, rect, step ) \
131 (p0) = sum + (rect).x + (step) * (rect).y, \
133 (p1) = sum + (rect).x + (rect).width + (step) * (rect).y, \
135 (p2) = sum + (rect).x + (step) * ((rect).y + (rect).height), \
136 /* (x + w, y + h) */ \
137 (p3) = sum + (rect).x + (rect).width + (step) * ((rect).y + (rect).height)
139 #define CV_TILTED_PTRS( p0, p1, p2, p3, tilted, rect, step ) \
141 (p0) = tilted + (rect).x + (step) * (rect).y, \
142 /* (x - h, y + h) */ \
143 (p1) = tilted + (rect).x - (rect).height + (step) * ((rect).y + (rect).height), \
144 /* (x + w, y + w) */ \
145 (p2) = tilted + (rect).x + (rect).width + (step) * ((rect).y + (rect).width), \
146 /* (x + w - h, y + w + h) */ \
147 (p3) = tilted + (rect).x + (rect).width - (rect).height \
148 + (step) * ((rect).y + (rect).width + (rect).height)
150 #define CALC_SUM_(p0, p1, p2, p3, offset) \
151 ((p0)[offset] - (p1)[offset] - (p2)[offset] + (p3)[offset])
153 #define CALC_SUM(rect,offset) CALC_SUM_((rect)[0], (rect)[1], (rect)[2], (rect)[3], offset)
156 FeatureEvaluator::~FeatureEvaluator() {}
157 bool FeatureEvaluator::read(const FileNode&) {return true;}
158 int FeatureEvaluator::getFeatureType() const {return -1;}
160 bool FeatureEvaluator::setImage(const Mat&, Size) { return true; }
161 int FeatureEvaluator::setWindow(Point) { return true; }
163 double FeatureEvaluator::calcOrd(int, int) const { return 0.; }
164 int FeatureEvaluator::calcCat(int, int) const { return 0; }
167 class HaarEvaluator : public FeatureEvaluator
174 float calc( int offset ) const;
175 void updatePtrs( const Mat& sum );
176 bool read( const FileNode& node );
180 enum { RECT_NUM = 3 };
188 const int* p[RECT_NUM][4];
192 virtual ~HaarEvaluator();
194 virtual bool read( const FileNode& node );
195 virtual bool setImage(const Mat& image, Size _origWinSize );
196 virtual int setWindow( Point pt );
197 virtual int getFeatureType() const { return FeatureEvaluator::HAAR; }
199 double operator()(int featureIdx, int pOffset) const
200 { return features[featureIdx].calc(pOffset) * varianceNormFactor; }
202 virtual double calcOrd(int featureIdx, int pOffset) const
203 { return (*this)(featureIdx, pOffset); }
207 Vector<Feature> features;
208 bool hasTiltedFeatures;
210 Mat sum0, sqsum0, tilted0;
211 Mat sum, sqsum, tilted;
217 double varianceNormFactor;
220 //---------------------------------------------- Haar Features ------------------------------------------------
221 inline HaarEvaluator::Feature :: Feature()
224 rect[0].r = rect[1].r = rect[2].r = Rect();
225 rect[0].weight = rect[1].weight = rect[2].weight = 0;
226 p[0][0] = p[0][1] = p[0][2] = p[0][3] =
227 p[1][0] = p[1][1] = p[1][2] = p[1][3] =
228 p[2][0] = p[2][1] = p[2][2] = p[2][3] = 0;
231 inline float HaarEvaluator::Feature :: calc( int offset ) const
233 float ret = rect[0].weight * CALC_SUM(p[0], offset) + rect[1].weight * CALC_SUM(p[1], offset);
235 if( rect[2].weight != 0.0f )
236 ret += rect[2].weight * CALC_SUM(p[2], offset);
241 inline void HaarEvaluator::Feature :: updatePtrs( const Mat& sum )
243 const int* ptr = (const int*)sum.data;
244 size_t step = sum.step/sizeof(ptr[0]);
247 CV_TILTED_PTRS( p[0][0], p[0][1], p[0][2], p[0][3], ptr, rect[0].r, step );
248 CV_TILTED_PTRS( p[1][0], p[1][1], p[1][2], p[1][3], ptr, rect[1].r, step );
250 CV_TILTED_PTRS( p[2][0], p[2][1], p[2][2], p[2][3], ptr, rect[2].r, step );
254 CV_SUM_PTRS( p[0][0], p[0][1], p[0][2], p[0][3], ptr, rect[0].r, step );
255 CV_SUM_PTRS( p[1][0], p[1][1], p[1][2], p[1][3], ptr, rect[1].r, step );
257 CV_SUM_PTRS( p[2][0], p[2][1], p[2][2], p[2][3], ptr, rect[2].r, step );
261 bool HaarEvaluator::Feature :: read( const FileNode& node )
263 FileNode rnode = node[CC_RECTS];
264 FileNodeIterator it = rnode.begin(), it_end = rnode.end();
267 for( ri = 0; ri < RECT_NUM; ri++ )
270 rect[ri].weight = 0.f;
273 for(ri = 0; it != it_end; ++it, ri++)
275 FileNodeIterator it2 = (*it).begin();
276 it2 >> rect[ri].r.x >> rect[ri].r.y >>
277 rect[ri].r.width >> rect[ri].r.height >> rect[ri].weight;
280 tilted = (int)node[CC_TILTED] != 0;
285 HaarEvaluator::HaarEvaluator()
289 HaarEvaluator::~HaarEvaluator()
293 bool HaarEvaluator::read(const FileNode& node)
295 features.resize(node.size());
296 FileNodeIterator it = node.begin(), it_end = node.end();
297 hasTiltedFeatures = false;
299 for(int i = 0; it != it_end; ++it, i++)
301 if(!features[i].read(*it))
303 if( features[i].tilted )
304 hasTiltedFeatures = true;
309 bool HaarEvaluator::setImage( const Mat& image, Size _origWinSize )
311 int rn = image.rows+1, cn = image.cols+1;
312 origWinSize = _origWinSize;
313 normrect = Rect(1, 1, origWinSize.width-2, origWinSize.height-2);
315 if (image.cols < origWinSize.width || image.rows < origWinSize.height)
318 if( sum0.rows < rn || sum0.cols < cn )
320 sum0.create(rn, cn, CV_32S);
321 sqsum0.create(rn, cn, CV_64F);
322 if (hasTiltedFeatures)
323 tilted0.create( rn, cn, CV_32S);
325 sum = Mat(rn, cn, CV_32S, sum0.data);
326 sqsum = Mat(rn, cn, CV_32S, sqsum0.data);
327 if( hasTiltedFeatures )
329 tilted = Mat(rn, cn, CV_32S, tilted0.data);
330 integral(image, sum, sqsum, tilted);
333 integral(image, sum, sqsum);
335 const int* sdata = (const int*)sum.data;
336 const double* sqdata = (const double*)sqsum.data;
337 size_t sumStep = sum.step/sizeof(sdata[0]);
338 size_t sqsumStep = sqsum.step/sizeof(sqdata[0]);
340 CV_SUM_PTRS( p[0], p[1], p[2], p[3], sdata, normrect, sumStep );
341 CV_SUM_PTRS( pq[0], pq[1], pq[2], pq[3], sqdata, normrect, sqsumStep );
343 size_t fi, nfeatures = features.size();
345 for( fi = 0; fi < nfeatures; fi++ )
346 features[fi].updatePtrs( !features[fi].tilted ? sum : tilted );
352 int HaarEvaluator::setWindow( Point pt )
354 if( pt.x < 0 || pt.y < 0 ||
355 pt.x + origWinSize.width >= sum.cols-2 ||
356 pt.y + origWinSize.height >= sum.rows-2 )
359 size_t pOffset = pt.y * (sum.step/sizeof(int)) + pt.x;
360 size_t pqOffset = pt.y * (sqsum.step/sizeof(double)) + pt.x;
361 int valsum = CALC_SUM(p,pOffset);
362 double valsqsum = CALC_SUM(pq,pqOffset);
364 varianceNormFactor = (double)normrect.area() * valsqsum - (double)valsum * valsum;
365 if( varianceNormFactor > 0. )
366 varianceNormFactor = sqrt(varianceNormFactor);
368 varianceNormFactor = 1.;
369 varianceNormFactor = 1./varianceNormFactor;
373 //---------------------------------------------- LBP Features ------------------------------------------------
375 class LBPEvaluator : public FeatureEvaluator
381 Feature( int x, int y, int _block_w, int _block_h ) :
382 rect(x, y, _block_w, _block_h) {}
384 int calc( int offset ) const;
385 void updatePtrs( const Mat& sum );
386 bool read(const FileNode& node );
388 enum { POINT_NUM = 16 };
390 Rect rect; // weight and height for block
391 const int* p[POINT_NUM]; // fast
395 virtual ~LBPEvaluator();
397 virtual bool read( const FileNode& node );
398 virtual bool setImage(const Mat& image, Size _origWinSize);
399 virtual int setWindow( Point pt );
400 virtual int getFeatureType() const { return FeatureEvaluator::LBP; }
402 int operator()(int featureIdx, int pOffset) const
403 { return features[featureIdx].calc(pOffset); }
405 virtual int calcCat(int featureIdx, int pOffset) const
406 { return (*this)(featureIdx, pOffset); }
410 Vector<LBPEvaluator::Feature> features;
417 inline LBPEvaluator::Feature :: Feature()
420 for( int i = 0; i < POINT_NUM; i++ )
424 inline int LBPEvaluator::Feature :: calc( int offset ) const
426 int cval = CALC_SUM_( p[5], p[6], p[9], p[10], offset );
428 return (CALC_SUM_( p[0], p[1], p[4], p[5], offset ) >= cval ? 128 : 0) | // 0
429 (CALC_SUM_( p[1], p[2], p[5], p[6], offset ) >= cval ? 64 : 0) | // 1
430 (CALC_SUM_( p[2], p[3], p[6], p[7], offset ) >= cval ? 32 : 0) | // 2
431 (CALC_SUM_( p[6], p[7], p[10], p[11], offset ) >= cval ? 16 : 0) | // 5
432 (CALC_SUM_( p[10], p[11], p[14], p[15], offset ) >= cval ? 8 : 0)| // 8
433 (CALC_SUM_( p[9], p[10], p[13], p[14], offset ) >= cval ? 4 : 0)| // 7
434 (CALC_SUM_( p[8], p[9], p[12], p[13], offset ) >= cval ? 2 : 0)| // 6
435 (CALC_SUM_( p[4], p[5], p[8], p[9], offset ) >= cval ? 1 : 0);
438 inline void LBPEvaluator::Feature :: updatePtrs( const Mat& sum )
440 const int* ptr = (const int*)sum.data;
441 size_t step = sum.step/sizeof(ptr[0]);
443 CV_SUM_PTRS( p[0], p[1], p[4], p[5], ptr, tr, step );
444 tr.x += 2*rect.width;
445 CV_SUM_PTRS( p[2], p[3], p[6], p[7], ptr, tr, step );
446 tr.y += 2*rect.height;
447 CV_SUM_PTRS( p[10], p[11], p[14], p[15], ptr, tr, step );
448 tr.x -= 2*rect.width;
449 CV_SUM_PTRS( p[8], p[9], p[12], p[13], ptr, tr, step );
452 bool LBPEvaluator::Feature :: read(const FileNode& node )
454 FileNode rnode = node[CC_RECT];
455 FileNodeIterator it = rnode.begin();
456 it >> rect.x >> rect.y >> rect.width >> rect.height;
460 //--------------------------------------- LBPEvaluator -------------------------------------------
462 LBPEvaluator::LBPEvaluator()
466 LBPEvaluator::~LBPEvaluator()
469 bool LBPEvaluator::read( const FileNode& node )
471 features.resize(node.size());
472 FileNodeIterator it = node.begin(), it_end = node.end();
474 for(int i = 0; it != it_end; ++it, i++)
476 if(!features[i].read(*it))
482 bool LBPEvaluator::setImage( const Mat& image, Size _origWinSize )
484 int rn = image.rows+1, cn = image.cols+1;
485 origWinSize = _origWinSize;
487 if( image.cols < origWinSize.width || image.rows < origWinSize.height )
490 if( sum0.rows < rn || sum0.cols < cn )
491 sum0.create(rn, cn, CV_32S);
492 sum = Mat(rn, cn, CV_32S, sum0.data);
493 integral(image, sum);
495 size_t fi, nfeatures = features.size();
497 for( fi = 0; fi < nfeatures; fi++ )
498 features[fi].updatePtrs( sum );
503 int LBPEvaluator::setWindow( Point pt )
505 if( pt.x < 0 || pt.y < 0 ||
506 pt.x + origWinSize.width >= sum.cols-2 ||
507 pt.y + origWinSize.height >= sum.rows-2 )
509 return pt.y * ((int)sum.step/sizeof(int)) + pt.x;
513 Ptr<FeatureEvaluator> FeatureEvaluator::create(int featureType)
515 return featureType == HAAR ? Ptr<FeatureEvaluator>(new HaarEvaluator) :
516 featureType == LBP ? Ptr<FeatureEvaluator>(new LBPEvaluator) : Ptr<FeatureEvaluator>();
519 /////////////////////////////////// Classifier Cascade ////////////////////////////////////////////////
521 CascadeClassifier::CascadeClassifier()
525 CascadeClassifier::CascadeClassifier(const String& filename)
528 CascadeClassifier::~CascadeClassifier()
532 bool CascadeClassifier::empty() const
534 return oldCascade.empty() && stages.empty();
537 bool CascadeClassifier::load(const String& filename)
539 oldCascade.release();
541 FileStorage fs(filename, FileStorage::READ);
545 if( read(fs.getFirstTopLevelNode()) )
550 oldCascade = Ptr<CvHaarClassifierCascade>((CvHaarClassifierCascade*)cvLoad(filename.c_str(), 0, 0, 0));
551 return !oldCascade.empty();
555 template<class FEval>
556 inline int predictOrdered( CascadeClassifier& cascade, int pOffset)
558 int si, nstages = (int)cascade.stages.size();
559 int nodeOfs = 0, leafOfs = 0;
560 FEval& feval = (FEval&)*cascade.feval;
562 for( si = 0; si < nstages; si++ )
564 CascadeClassifier::Stage& stage = cascade.stages[si];
565 int wi, ntrees = stage.ntrees;
568 for( wi = 0; wi < ntrees; wi++ )
570 CascadeClassifier::DTree& weak = cascade.classifiers[stage.first + wi];
571 int idx = 0, root = nodeOfs;
574 CascadeClassifier::DTreeNode& node = cascade.nodes[root + idx];
575 double val = feval(node.featureIdx, pOffset);
576 idx = val < node.threshold ? node.left : node.right;
579 sum += cascade.leaves[leafOfs - idx];
580 nodeOfs += weak.nodeCount;
581 leafOfs += weak.nodeCount + 1;
583 if( sum < stage.threshold )
589 template<class FEval>
590 inline int predictCategorical( CascadeClassifier& cascade, int pOffset)
592 int si, nstages = (int)cascade.stages.size();
593 int nodeOfs = 0, leafOfs = 0;
594 FEval& feval = (FEval&)*cascade.feval;
595 size_t subsetSize = (cascade.ncategories + 31)/32;
597 for( si = 0; si < nstages; si++ )
599 CascadeClassifier::Stage& stage = cascade.stages[si];
600 int wi, ntrees = stage.ntrees;
603 for( wi = 0; wi < ntrees; wi++ )
605 CascadeClassifier::DTree& weak = cascade.classifiers[stage.first + wi];
606 int idx = 0, root = nodeOfs;
609 CascadeClassifier::DTreeNode& node = cascade.nodes[root + idx];
610 int c = feval(node.featureIdx, pOffset);
611 const int* subset = &cascade.subsets[(root + idx)*subsetSize];
612 idx = (subset[c>>5] & (1 << (c & 31))) ? node.left : node.right;
615 sum += cascade.leaves[leafOfs - idx];
616 nodeOfs += weak.nodeCount;
617 leafOfs += weak.nodeCount + 1;
619 if( sum < stage.threshold )
626 int CascadeClassifier::runAt(Point pt)
628 CV_Assert( oldCascade.empty() );
629 /*if( !oldCascade.empty() )
630 return cvRunHaarClassifierCascade(oldCascade, pt, 0);*/
632 assert(featureType == FeatureEvaluator::HAAR ||
633 featureType == FeatureEvaluator::LBP);
634 int offset = feval->setWindow(pt);
635 return offset < 0 ? -1 :
636 featureType == FeatureEvaluator::HAAR ?
637 predictOrdered<HaarEvaluator>(*this, offset) :
638 predictCategorical<LBPEvaluator>(*this, offset);
642 bool CascadeClassifier::setImage(const Mat& image)
644 /*if( !oldCascade.empty() )
646 Mat sum(image.rows+1, image.cols+1, CV_32S);
647 Mat tilted(image.rows+1, image.cols+1, CV_32S);
648 Mat sqsum(image.rows+1, image.cols+1, CV_64F);
649 integral(image, sum, sqsum, tilted);
650 CvMat _sum = sum, _sqsum = sqsum, _tilted = tilted;
651 cvSetImagesForHaarClassifierCascade( oldCascade, &_sum, &_sqsum, &_tilted, 1. );
654 return empty() ? false : feval->setImage(image, origWinSize);
658 struct getRect { Rect operator ()(const CvAvgComp& e) const { return e.rect; } };
659 void CascadeClassifier::detectMultiScale( const Mat& image, Vector<Rect>& objects,
660 double scaleFactor, int minNeighbors,
661 int flags, Size minSize )
663 CV_Assert( scaleFactor > 1 && image.depth() == CV_8U );
668 if( !oldCascade.empty() )
670 MemStorage storage(cvCreateMemStorage(0));
671 CvMat _image = image;
672 CvSeq* _objects = cvHaarDetectObjects( &_image, oldCascade, storage, scaleFactor,
673 minNeighbors, flags, minSize );
674 Vector<CvAvgComp> vecAvgComp;
675 Seq<CvAvgComp>(_objects).copyTo(vecAvgComp);
676 objects.resize(vecAvgComp.size());
677 std::transform(vecAvgComp.begin(), vecAvgComp.end(), objects.begin(), getRect());
683 Mat img = image, imgbuf(image.rows+1, image.cols+1, CV_8U);
685 if( img.channels() > 1 )
688 cvtColor(img, temp, CV_BGR2GRAY);
692 for( double factor = 1; ; factor *= scaleFactor )
695 Size winSize( cvRound(origWinSize.width*factor), cvRound(origWinSize.height*factor) );
696 Size sz( cvRound( img.cols/factor ), cvRound( img.rows/factor ) );
697 Size sz1( sz.width - origWinSize.width, sz.height - origWinSize.height );
699 if( sz1.width <= 0 || sz1.height <= 0 )
701 if( winSize.width < minSize.width || winSize.height < minSize.height )
704 Mat img1( sz, CV_8U, imgbuf.data );
706 resize( img, img1, sz, 0, 0, CV_INTER_LINEAR );
707 if( !feval->setImage( img1, origWinSize ) )
709 int yStep = factor > 2. ? 1 : 2;
711 for( int y = 0; y < sz1.height; y += yStep )
712 for( int x = 0; x < sz1.width; x += yStep )
714 int r = runAt(Point(x,y));
716 objects.push_back(Rect(cvRound(x*factor), cvRound(y*factor),
717 winSize.width, winSize.height));
723 groupRectangles( objects, minNeighbors, 0.2 );
727 bool CascadeClassifier::read(const FileNode& root)
730 String stageTypeStr = (String)root[CC_STAGE_TYPE];
731 if( stageTypeStr == CC_BOOST )
736 String featureTypeStr = (String)root[CC_FEATURE_TYPE];
737 if( featureTypeStr == CC_HAAR )
738 featureType = FeatureEvaluator::HAAR;
739 else if( featureTypeStr == CC_LBP )
740 featureType = FeatureEvaluator::LBP;
744 origWinSize.width = (int)root[CC_WIDTH];
745 origWinSize.height = (int)root[CC_HEIGHT];
746 CV_Assert( origWinSize.height > 0 && origWinSize.width > 0 );
748 // load feature params
749 FileNode fn = root[CC_FEATURE_PARAMS];
753 ncategories = fn[CC_MAX_CAT_COUNT];
754 int subsetSize = (ncategories + 31)/32,
755 nodeStep = 3 + ( ncategories>0 ? subsetSize : 1 );
758 fn = root[CC_STAGES];
762 stages.reserve(fn.size());
766 FileNodeIterator it = fn.begin(), it_end = fn.end();
768 for( int si = 0; it != it_end; si++, ++it )
772 stage.threshold = fns[CC_STAGE_THRESHOLD];
773 fns = fns[CC_WEAK_CLASSIFIERS];
776 stage.ntrees = (int)fns.size();
777 stage.first = (int)classifiers.size();
778 stages.push_back(stage);
779 classifiers.reserve(stages[si].first + stages[si].ntrees);
781 FileNodeIterator it1 = fns.begin(), it1_end = fns.end();
782 for( ; it1 != it1_end; ++it1 ) // weak trees
785 FileNode internalNodes = fnw[CC_INTERNAL_NODES];
786 FileNode leafValues = fnw[CC_LEAF_VALUES];
787 if( internalNodes.empty() || leafValues.empty() )
790 tree.nodeCount = (int)internalNodes.size()/nodeStep;
791 classifiers.push_back(tree);
793 nodes.reserve(nodes.size() + tree.nodeCount);
794 leaves.reserve(leaves.size() + leafValues.size());
796 subsets.reserve(subsets.size() + tree.nodeCount*subsetSize);
798 FileNodeIterator it2 = internalNodes.begin(), it2_end = internalNodes.end();
800 for( ; it2 != it2_end; ) // nodes
803 node.left = (int)*it2; ++it2;
804 node.right = (int)*it2; ++it2;
805 node.featureIdx = (int)*it2; ++it2;
807 for( int j = 0; j < subsetSize; j++, ++it2 )
808 subsets.push_back((int)*it2);
811 node.threshold = (float)*it2; ++it2;
813 nodes.push_back(node);
816 it2 = leafValues.begin(), it2_end = leafValues.end();
818 for( ; it2 != it2_end; ++it2 ) // leaves
819 leaves.push_back((float)*it2);
824 feval = FeatureEvaluator::create(featureType);
825 fn = root[CC_FEATURES];
829 return feval->read(fn);