博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Adaboost算法】C++转C, 分类器结构设计
阅读量:6647 次
发布时间:2019-06-25

本文共 5769 字,大约阅读时间需要 19 分钟。

一、参考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
& 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
& objects, vector
& rejectLevels, vector
& 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
& candidates ); virtual bool detectSingleScale( const Mat& image, int stripCount, Size processingRectSize, int stripSize, int yStep, double factor, vector
& candidates, vector
& rejectLevels, vector
& 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
friend int predictOrdered( CascadeClassifier& cascade, Ptr
&featureEvaluator, double& weight); template
friend int predictCategorical( CascadeClassifier& cascade, Ptr
&featureEvaluator, double& weight); template
friend int predictOrderedStump( CascadeClassifier& cascade, Ptr
&featureEvaluator, double& weight); template
friend int predictCategoricalStump( CascadeClassifier& cascade, Ptr
&featureEvaluator, double& weight); bool setImage( Ptr
& feval, const Mat& image); virtual int runAt( Ptr
& feval, Point pt, double& weight ); class Data { public: struct CV_EXPORTS DTreeNode { int featureIdx; float threshold; // for ordered features only int 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
stages; vector
classifiers; vector
nodes; vector
leaves; vector
subsets; }; Data data; Ptr
featureEvaluator; Ptr
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); Ptr
getMaskGenerator(); void setFaceDetectionMaskGenerator();protected: Ptr
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 block const int* p[16]; // fast }; LBPEvaluator(); virtual ~LBPEvaluator(); virtual bool read( const FileNode& node ); virtual Ptr
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
> features; Feature* featuresPtr; // optimization Mat sum0, sum; Rect normrect; int offset;};

二、开始设计适合自己分类器的数据结构

如下图,因为我们打算使用数组方式存储信息,为避免溢出,首先了解自己分类器的强分类器级数,nodes,leaves等信息,由于我们的分类器是通过opencv训练的,所以可以直接Debug查看分类器信息,或者通过xml文件查看。

设计结构体如下:

#ifndef    _CP_ADABOOST_#define    _CP_ADABOOST_#ifdef   __cplusplusextern "C"{#endif  typedef struct tagCpSize{    int iWidth;    int iHeight;}CP_SIZE_S;typedef struct  tagCPDTreeNode{    int featureIdx;    float threshold; // for ordered features only    int 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_ */
你可能感兴趣的文章
ActionBar自己定义改动无效解决方法
查看>>
设计模式总结篇系列:外观模式(Facade)
查看>>
多谋而少决
查看>>
省市区级联选择
查看>>
Hyper-V虚机跨NUMA节点性能影响
查看>>
spring配置,spring中的bean 的id不能相同
查看>>
Swift语言指南(七)--语言基础之布尔值和类型别名
查看>>
与机房收费系统重相见
查看>>
Spark核心概念
查看>>
百度究竟是哪国的公司
查看>>
netstat命令
查看>>
杂谈---一个人的两种心理
查看>>
赣南师范学院数学竞赛培训第06套模拟试卷参考解答
查看>>
DropDownList中显示无限级树形结构
查看>>
关于Android界面编程与视图(View)组件
查看>>
C#和VC++字符集和编码
查看>>
[转]Android开发环境搭建(图文教程)
查看>>
你认为A和B所在方格颜色相同吗?
查看>>
dorado抽取js
查看>>
Windows下面安装和配置Solr 4.9(二)
查看>>