【Adaboost算法】C++转C, 分类器结构设计
生活随笔
收集整理的這篇文章主要介紹了
【Adaboost算法】C++转C, 分类器结构设计
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、參考OpenCV的CascadeClassifier類LBPEvaluator類
如下,篩選出存放分類器相關信息的成員變量:
class CV_EXPORTS_W CascadeClassifier { public:CV_WRAP CascadeClassifier();CV_WRAP CascadeClassifier( const string& filename );virtual ~CascadeClassifier();CV_WRAP virtual bool empty() const;CV_WRAP bool load( const string& filename );virtual bool read( const FileNode& node );CV_WRAP virtual void detectMultiScale( const Mat& image,CV_OUT vector<Rect>& objects,double scaleFactor=1.1,int minNeighbors=3, int flags=0,Size minSize=Size(),Size maxSize=Size() );CV_WRAP virtual void detectMultiScale( const Mat& image,CV_OUT vector<Rect>& objects,vector<int>& rejectLevels,vector<double>& levelWeights,double scaleFactor=1.1,int minNeighbors=3, int flags=0,Size minSize=Size(),Size maxSize=Size(),bool outputRejectLevels=false );bool isOldFormatCascade() const;virtual Size getOriginalWindowSize() const;int getFeatureType() const;bool setImage( const Mat& );protected://virtual bool detectSingleScale( const Mat& image, int stripCount, Size processingRectSize,// int stripSize, int yStep, double factor, vector<Rect>& candidates );virtual bool detectSingleScale( const Mat& image, int stripCount, Size processingRectSize,int stripSize, int yStep, double factor, vector<Rect>& candidates,vector<int>& rejectLevels, vector<double>& levelWeights, bool outputRejectLevels=false);protected:enum { BOOST = 0 };enum { DO_CANNY_PRUNING = 1, SCALE_IMAGE = 2,FIND_BIGGEST_OBJECT = 4, DO_ROUGH_SEARCH = 8 };friend class CascadeClassifierInvoker;template<class FEval>friend int predictOrdered( CascadeClassifier& cascade, Ptr<FeatureEvaluator> &featureEvaluator, double& weight);template<class FEval>friend int predictCategorical( CascadeClassifier& cascade, Ptr<FeatureEvaluator> &featureEvaluator, double& weight);template<class FEval>friend int predictOrderedStump( CascadeClassifier& cascade, Ptr<FeatureEvaluator> &featureEvaluator, double& weight);template<class FEval>friend int predictCategoricalStump( CascadeClassifier& cascade, Ptr<FeatureEvaluator> &featureEvaluator, double& weight);bool setImage( Ptr<FeatureEvaluator>& feval, const Mat& image);virtual int runAt( Ptr<FeatureEvaluator>& feval, Point pt, double& weight );class Data{public:struct CV_EXPORTS DTreeNode{int featureIdx;float threshold; // for ordered features onlyint left;int right;};struct CV_EXPORTS DTree{int nodeCount;};struct CV_EXPORTS Stage{int first;int ntrees;float threshold;};bool read(const FileNode &node);bool isStumpBased;int stageType;int featureType;int ncategories;Size origWinSize;vector<Stage> stages;vector<DTree> classifiers;vector<DTreeNode> nodes;vector<float> leaves;vector<int> subsets;};Data data;Ptr<FeatureEvaluator> featureEvaluator;Ptr<CvHaarClassifierCascade> oldCascade;public:class CV_EXPORTS MaskGenerator{public:virtual ~MaskGenerator() {}virtual cv::Mat generateMask(const cv::Mat& src)=0;virtual void initializeMask(const cv::Mat& /*src*/) {};};void setMaskGenerator(Ptr<MaskGenerator> maskGenerator);Ptr<MaskGenerator> getMaskGenerator();void setFaceDetectionMaskGenerator();protected:Ptr<MaskGenerator> maskGenerator; };class LBPEvaluator : public FeatureEvaluator { public:struct Feature{Feature();Feature( int x, int y, int _block_w, int _block_h ) :rect(x, y, _block_w, _block_h) {}int calc( int offset ) const;void updatePtrs( const Mat& sum );bool read(const FileNode& node ); Rect rect; // weight and height for blockconst int* p[16]; // fast };LBPEvaluator();virtual ~LBPEvaluator();virtual bool read( const FileNode& node );virtual Ptr<FeatureEvaluator> clone() const;virtual int getFeatureType() const { return FeatureEvaluator::LBP; }virtual bool setImage(const Mat& image, Size _origWinSize);virtual bool setWindow(Point pt);int operator()(int featureIdx) const{ return featuresPtr[featureIdx].calc(offset); }virtual int calcCat(int featureIdx) const{ return (*this)(featureIdx); } protected:Size origWinSize;Ptr<vector<Feature> > features;Feature* featuresPtr; // optimization Mat sum0, sum;Rect normrect;int offset; };二、開始設計適合自己分類器的數據結構
如下圖,因為我們打算使用數組方式存儲信息,為避免溢出,首先了解自己分類器的強分類器級數,nodes,leaves等信息,由于我們的分類器是通過opencv訓練的,所以可以直接Debug查看分類器信息,或者通過xml文件查看。
設計結構體如下:
#ifndef _CP_ADABOOST_ #define _CP_ADABOOST_ #ifdef __cplusplus extern "C"{ #endif typedef struct tagCpSize {int iWidth;int iHeight; }CP_SIZE_S;typedef struct tagCPDTreeNode {int featureIdx;float threshold; // for ordered features onlyint left;int right; }CP_DTREE_NODE_S;typedef struct tagCpDTree {int nodeCount; }CP_DTREE_S;typedef struct tagCpStage {int first;int ntrees;float threshold; }CP_STAGE_S;typedef struct tagCPRect {int x;int y;int width;int height; }CP_RECT_S;typedef struct tagLBPFeature {CP_RECT_S rect;/*特征位置*/int* p[16];/* 特征在積分圖中的地址 */ }CP_LBP_FEATURE_S;typedef struct tagCpClassifier {bool isStumpBased;int stageType;int featureType;int ncategories;CP_SIZE_S origWinSize;CP_STAGE_S stages[50]; /*強分類器級數*/int stagerNum;CP_DTREE_S classifiers[500];
int classfierNum;CP_DTREE_NODE_S nodes[500];
int nodeNum;CP_LBP_FEATURE_S feature[500];
int featureNum;float leaves[1000];
int leaveNum;int subsets[5000];
int subsetNum; }CP_CLASSIFIER_S;#ifdef __cplusplus } #endif #endif /* _CP_ADABOOST_ */
轉載于:https://www.cnblogs.com/chenpi/p/5128229.html
總結
以上是生活随笔為你收集整理的【Adaboost算法】C++转C, 分类器结构设计的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU2571:命运(简单dp)
- 下一篇: 一个 bug / Masonry的引入