Skip to content

Commit

Permalink
Merge pull request opencv#1879 from LeszekSwirski:traincascade-2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Donchenko authored and OpenCV Buildbot committed Nov 28, 2013
2 parents 19b88a1 + 37a7546 commit 70c1b70
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 124 deletions.
2 changes: 2 additions & 0 deletions apps/traincascade/HOGfeatures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "HOGfeatures.h"
#include "cascadeclassifier.h"

using namespace std;
using namespace cv;

CvHOGFeatureParams::CvHOGFeatureParams()
{
Expand Down
24 changes: 12 additions & 12 deletions apps/traincascade/HOGfeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,33 @@ class CvHOGEvaluator : public CvFeatureEvaluator
public:
virtual ~CvHOGEvaluator() {}
virtual void init(const CvFeatureParams *_featureParams,
int _maxSampleCount, Size _winSize );
virtual void setImage(const Mat& img, uchar clsLabel, int idx);
int _maxSampleCount, cv::Size _winSize );
virtual void setImage(const cv::Mat& img, uchar clsLabel, int idx);
virtual float operator()(int varIdx, int sampleIdx) const;
virtual void writeFeatures( FileStorage &fs, const Mat& featureMap ) const;
virtual void writeFeatures( cv::FileStorage &fs, const cv::Mat& featureMap ) const;
protected:
virtual void generateFeatures();
virtual void integralHistogram(const Mat &img, vector<Mat> &histogram, Mat &norm, int nbins) const;
virtual void integralHistogram(const cv::Mat &img, std::vector<cv::Mat> &histogram, cv::Mat &norm, int nbins) const;
class Feature
{
public:
Feature();
Feature( int offset, int x, int y, int cellW, int cellH );
float calc( const vector<Mat> &_hists, const Mat &_normSum, size_t y, int featComponent ) const;
void write( FileStorage &fs ) const;
void write( FileStorage &fs, int varIdx ) const;
float calc( const std::vector<cv::Mat> &_hists, const cv::Mat &_normSum, size_t y, int featComponent ) const;
void write( cv::FileStorage &fs ) const;
void write( cv::FileStorage &fs, int varIdx ) const;

Rect rect[N_CELLS]; //cells
cv::Rect rect[N_CELLS]; //cells

struct
{
int p0, p1, p2, p3;
} fastRect[N_CELLS];
};
vector<Feature> features;
std::vector<Feature> features;

Mat normSum; //for nomalization calculation (L1 or L2)
vector<Mat> hist;
cv::Mat normSum; //for nomalization calculation (L1 or L2)
std::vector<cv::Mat> hist;
};

inline float CvHOGEvaluator::operator()(int varIdx, int sampleIdx) const
Expand All @@ -57,7 +57,7 @@ inline float CvHOGEvaluator::operator()(int varIdx, int sampleIdx) const
return features[featureIdx].calc( hist, normSum, sampleIdx, componentIdx);
}

inline float CvHOGEvaluator::Feature::calc( const vector<Mat>& _hists, const Mat& _normSum, size_t y, int featComponent ) const
inline float CvHOGEvaluator::Feature::calc( const std::vector<cv::Mat>& _hists, const cv::Mat& _normSum, size_t y, int featComponent ) const
{
float normFactor;
float res;
Expand Down
25 changes: 19 additions & 6 deletions apps/traincascade/boost.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
#include "opencv2/core/core.hpp"
#include "opencv2/core/internal.hpp"

using cv::Size;
using cv::Mat;
using cv::Point;
using cv::FileStorage;
using cv::Rect;
using cv::Ptr;
using cv::FileNode;
using cv::Mat_;
using cv::Range;
using cv::FileNodeIterator;
using cv::ParallelLoopBody;


#include "boost.h"
#include "cascadeclassifier.h"
#include <queue>
Expand Down Expand Up @@ -160,10 +173,10 @@ CvCascadeBoostParams::CvCascadeBoostParams( int _boostType,

void CvCascadeBoostParams::write( FileStorage &fs ) const
{
String boostTypeStr = boost_type == CvBoost::DISCRETE ? CC_DISCRETE_BOOST :
string boostTypeStr = boost_type == CvBoost::DISCRETE ? CC_DISCRETE_BOOST :
boost_type == CvBoost::REAL ? CC_REAL_BOOST :
boost_type == CvBoost::LOGIT ? CC_LOGIT_BOOST :
boost_type == CvBoost::GENTLE ? CC_GENTLE_BOOST : String();
boost_type == CvBoost::GENTLE ? CC_GENTLE_BOOST : string();
CV_Assert( !boostTypeStr.empty() );
fs << CC_BOOST_TYPE << boostTypeStr;
fs << CC_MINHITRATE << minHitRate;
Expand All @@ -175,7 +188,7 @@ void CvCascadeBoostParams::write( FileStorage &fs ) const

bool CvCascadeBoostParams::read( const FileNode &node )
{
String boostTypeStr;
string boostTypeStr;
FileNode rnode = node[CC_BOOST_TYPE];
rnode >> boostTypeStr;
boost_type = !boostTypeStr.compare( CC_DISCRETE_BOOST ) ? CvBoost::DISCRETE :
Expand Down Expand Up @@ -213,10 +226,10 @@ void CvCascadeBoostParams::printDefaults() const

void CvCascadeBoostParams::printAttrs() const
{
String boostTypeStr = boost_type == CvBoost::DISCRETE ? CC_DISCRETE_BOOST :
string boostTypeStr = boost_type == CvBoost::DISCRETE ? CC_DISCRETE_BOOST :
boost_type == CvBoost::REAL ? CC_REAL_BOOST :
boost_type == CvBoost::LOGIT ? CC_LOGIT_BOOST :
boost_type == CvBoost::GENTLE ? CC_GENTLE_BOOST : String();
boost_type == CvBoost::GENTLE ? CC_GENTLE_BOOST : string();
CV_Assert( !boostTypeStr.empty() );
cout << "boostType: " << boostTypeStr << endl;
cout << "minHitRate: " << minHitRate << endl;
Expand All @@ -226,7 +239,7 @@ void CvCascadeBoostParams::printAttrs() const
cout << "maxWeakCount: " << weak_count << endl;
}

bool CvCascadeBoostParams::scanAttr( const String prmName, const String val)
bool CvCascadeBoostParams::scanAttr( const string prmName, const string val)
{
bool res = true;

Expand Down
20 changes: 10 additions & 10 deletions apps/traincascade/boost.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ struct CvCascadeBoostParams : CvBoostParams
CvCascadeBoostParams( int _boostType, float _minHitRate, float _maxFalseAlarm,
double _weightTrimRate, int _maxDepth, int _maxWeakCount );
virtual ~CvCascadeBoostParams() {}
void write( FileStorage &fs ) const;
bool read( const FileNode &node );
void write( cv::FileStorage &fs ) const;
bool read( const cv::FileNode &node );
virtual void printDefaults() const;
virtual void printAttrs() const;
virtual bool scanAttr( const String prmName, const String val);
virtual bool scanAttr( const std::string prmName, const std::string val);
};

struct CvCascadeBoostTrainData : CvDTreeTrainData
Expand Down Expand Up @@ -45,7 +45,7 @@ struct CvCascadeBoostTrainData : CvDTreeTrainData
virtual void free_train_data();

const CvFeatureEvaluator* featureEvaluator;
Mat valCache; // precalculated feature values (CV_32FC1)
cv::Mat valCache; // precalculated feature values (CV_32FC1)
CvMat _resp; // for casting
int numPrecalcVal, numPrecalcIdx;
};
Expand All @@ -54,9 +54,9 @@ class CvCascadeBoostTree : public CvBoostTree
{
public:
virtual CvDTreeNode* predict( int sampleIdx ) const;
void write( FileStorage &fs, const Mat& featureMap );
void read( const FileNode &node, CvBoost* _ensemble, CvDTreeTrainData* _data );
void markFeaturesInMap( Mat& featureMap );
void write( cv::FileStorage &fs, const cv::Mat& featureMap );
void read( const cv::FileNode &node, CvBoost* _ensemble, CvDTreeTrainData* _data );
void markFeaturesInMap( cv::Mat& featureMap );
protected:
virtual void split_node_data( CvDTreeNode* n );
};
Expand All @@ -70,10 +70,10 @@ class CvCascadeBoost : public CvBoost
virtual float predict( int sampleIdx, bool returnSum = false ) const;

float getThreshold() const { return threshold; }
void write( FileStorage &fs, const Mat& featureMap ) const;
bool read( const FileNode &node, const CvFeatureEvaluator* _featureEvaluator,
void write( cv::FileStorage &fs, const cv::Mat& featureMap ) const;
bool read( const cv::FileNode &node, const CvFeatureEvaluator* _featureEvaluator,
const CvCascadeBoostParams& _params );
void markUsedFeaturesInMap( Mat& featureMap );
void markUsedFeaturesInMap( cv::Mat& featureMap );
protected:
virtual bool set_params( const CvBoostParams& _params );
virtual void update_weights( CvBoostTree* tree );
Expand Down
19 changes: 10 additions & 9 deletions apps/traincascade/cascadeclassifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <queue>

using namespace std;
using namespace cv;

static const char* stageTypes[] = { CC_BOOST };
static const char* featureTypes[] = { CC_HAAR, CC_LBP, CC_HOG };
Expand All @@ -24,10 +25,10 @@ CvCascadeParams::CvCascadeParams( int _stageType, int _featureType ) : stageType

void CvCascadeParams::write( FileStorage &fs ) const
{
String stageTypeStr = stageType == BOOST ? CC_BOOST : String();
string stageTypeStr = stageType == BOOST ? CC_BOOST : string();
CV_Assert( !stageTypeStr.empty() );
fs << CC_STAGE_TYPE << stageTypeStr;
String featureTypeStr = featureType == CvFeatureParams::HAAR ? CC_HAAR :
string featureTypeStr = featureType == CvFeatureParams::HAAR ? CC_HAAR :
featureType == CvFeatureParams::LBP ? CC_LBP :
featureType == CvFeatureParams::HOG ? CC_HOG :
0;
Expand All @@ -41,7 +42,7 @@ bool CvCascadeParams::read( const FileNode &node )
{
if ( node.empty() )
return false;
String stageTypeStr, featureTypeStr;
string stageTypeStr, featureTypeStr;
FileNode rnode = node[CC_STAGE_TYPE];
if ( !rnode.isString() )
return false;
Expand Down Expand Up @@ -96,7 +97,7 @@ void CvCascadeParams::printAttrs() const
cout << "sampleHeight: " << winSize.height << endl;
}

bool CvCascadeParams::scanAttr( const String prmName, const String val )
bool CvCascadeParams::scanAttr( const string prmName, const string val )
{
bool res = true;
if( !prmName.compare( "-stageType" ) )
Expand Down Expand Up @@ -126,9 +127,9 @@ bool CvCascadeParams::scanAttr( const String prmName, const String val )

//---------------------------- CascadeClassifier --------------------------------------

bool CvCascadeClassifier::train( const String _cascadeDirName,
const String _posFilename,
const String _negFilename,
bool CvCascadeClassifier::train( const string _cascadeDirName,
const string _posFilename,
const string _negFilename,
int _numPos, int _numNeg,
int _precalcValBufSize, int _precalcIdxBufSize,
int _numStages,
Expand Down Expand Up @@ -411,7 +412,7 @@ bool CvCascadeClassifier::readStages( const FileNode &node)
#define ICV_HAAR_PARENT_NAME "parent"
#define ICV_HAAR_NEXT_NAME "next"

void CvCascadeClassifier::save( const String filename, bool baseFormat )
void CvCascadeClassifier::save( const string filename, bool baseFormat )
{
FileStorage fs( filename, FileStorage::WRITE );

Expand Down Expand Up @@ -503,7 +504,7 @@ void CvCascadeClassifier::save( const String filename, bool baseFormat )
fs << "}";
}

bool CvCascadeClassifier::load( const String cascadeDirName )
bool CvCascadeClassifier::load( const string cascadeDirName )
{
FileStorage fs( cascadeDirName + CC_PARAMS_FILENAME, FileStorage::READ );
if ( !fs.isOpened() )
Expand Down
38 changes: 19 additions & 19 deletions apps/traincascade/cascadeclassifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,24 @@ class CvCascadeParams : public CvParams

CvCascadeParams();
CvCascadeParams( int _stageType, int _featureType );
void write( FileStorage &fs ) const;
bool read( const FileNode &node );
void write( cv::FileStorage &fs ) const;
bool read( const cv::FileNode &node );

void printDefaults() const;
void printAttrs() const;
bool scanAttr( const String prmName, const String val );
bool scanAttr( const std::string prmName, const std::string val );

int stageType;
int featureType;
Size winSize;
cv::Size winSize;
};

class CvCascadeClassifier
{
public:
bool train( const String _cascadeDirName,
const String _posFilename,
const String _negFilename,
bool train( const std::string _cascadeDirName,
const std::string _posFilename,
const std::string _negFilename,
int _numPos, int _numNeg,
int _precalcValBufSize, int _precalcIdxBufSize,
int _numStages,
Expand All @@ -99,25 +99,25 @@ class CvCascadeClassifier
bool baseFormatSave = false );
private:
int predict( int sampleIdx );
void save( const String cascadeDirName, bool baseFormat = false );
bool load( const String cascadeDirName );
void save( const std::string cascadeDirName, bool baseFormat = false );
bool load( const std::string cascadeDirName );
bool updateTrainingSet( double& acceptanceRatio );
int fillPassedSamples( int first, int count, bool isPositive, int64& consumed );

void writeParams( FileStorage &fs ) const;
void writeStages( FileStorage &fs, const Mat& featureMap ) const;
void writeFeatures( FileStorage &fs, const Mat& featureMap ) const;
bool readParams( const FileNode &node );
bool readStages( const FileNode &node );
void writeParams( cv::FileStorage &fs ) const;
void writeStages( cv::FileStorage &fs, const cv::Mat& featureMap ) const;
void writeFeatures( cv::FileStorage &fs, const cv::Mat& featureMap ) const;
bool readParams( const cv::FileNode &node );
bool readStages( const cv::FileNode &node );

void getUsedFeaturesIdxMap( Mat& featureMap );
void getUsedFeaturesIdxMap( cv::Mat& featureMap );

CvCascadeParams cascadeParams;
Ptr<CvFeatureParams> featureParams;
Ptr<CvCascadeBoostParams> stageParams;
cv::Ptr<CvFeatureParams> featureParams;
cv::Ptr<CvCascadeBoostParams> stageParams;

Ptr<CvFeatureEvaluator> featureEvaluator;
vector< Ptr<CvCascadeBoost> > stageClassifiers;
cv::Ptr<CvFeatureEvaluator> featureEvaluator;
std::vector< cv::Ptr<CvCascadeBoost> > stageClassifiers;
CvCascadeImageReader imgReader;
int numStages, curNumSamples;
int numPos, numNeg;
Expand Down
3 changes: 2 additions & 1 deletion apps/traincascade/features.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "cascadeclassifier.h"

using namespace std;
using namespace cv;

float calcNormFactor( const Mat& sum, const Mat& sqSum )
{
Expand All @@ -24,7 +25,7 @@ CvParams::CvParams() : name( "params" ) {}
void CvParams::printDefaults() const
{ cout << "--" << name << "--" << endl; }
void CvParams::printAttrs() const {}
bool CvParams::scanAttr( const String, const String ) { return false; }
bool CvParams::scanAttr( const string, const string ) { return false; }


//---------------------------- FeatureParams --------------------------------------
Expand Down
11 changes: 6 additions & 5 deletions apps/traincascade/haarfeatures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "cascadeclassifier.h"

using namespace std;
using namespace cv;

CvHaarFeatureParams::CvHaarFeatureParams() : mode(BASIC)
{
Expand All @@ -25,9 +26,9 @@ void CvHaarFeatureParams::init( const CvFeatureParams& fp )
void CvHaarFeatureParams::write( FileStorage &fs ) const
{
CvFeatureParams::write( fs );
String modeStr = mode == BASIC ? CC_MODE_BASIC :
string modeStr = mode == BASIC ? CC_MODE_BASIC :
mode == CORE ? CC_MODE_CORE :
mode == ALL ? CC_MODE_ALL : String();
mode == ALL ? CC_MODE_ALL : string();
CV_Assert( !modeStr.empty() );
fs << CC_MODE << modeStr;
}
Expand All @@ -40,7 +41,7 @@ bool CvHaarFeatureParams::read( const FileNode &node )
FileNode rnode = node[CC_MODE];
if( !rnode.isString() )
return false;
String modeStr;
string modeStr;
rnode >> modeStr;
mode = !modeStr.compare( CC_MODE_BASIC ) ? BASIC :
!modeStr.compare( CC_MODE_CORE ) ? CORE :
Expand All @@ -58,13 +59,13 @@ void CvHaarFeatureParams::printDefaults() const
void CvHaarFeatureParams::printAttrs() const
{
CvFeatureParams::printAttrs();
String mode_str = mode == BASIC ? CC_MODE_BASIC :
string mode_str = mode == BASIC ? CC_MODE_BASIC :
mode == CORE ? CC_MODE_CORE :
mode == ALL ? CC_MODE_ALL : 0;
cout << "mode: " << mode_str << endl;
}

bool CvHaarFeatureParams::scanAttr( const String prmName, const String val)
bool CvHaarFeatureParams::scanAttr( const string prmName, const string val)
{
if ( !CvFeatureParams::scanAttr( prmName, val ) )
{
Expand Down
Loading

0 comments on commit 70c1b70

Please sign in to comment.