【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=, int flags=,
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=, int flags=,
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 = };
enum { DO_CANNY_PRUNING = , SCALE_IMAGE = ,
FIND_BIGGEST_OBJECT = , DO_ROUGH_SEARCH = }; 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 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<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)=;
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 block
const int* p[]; // 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 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[];/* 特征在积分图中的地址 */
}CP_LBP_FEATURE_S; typedef struct tagCpClassifier
{
bool isStumpBased; int stageType;
int featureType;
int ncategories;
CP_SIZE_S origWinSize; CP_STAGE_S stages[]; /*强分类器级数*/
int stagerNum;
CP_DTREE_S classifiers[];
int classfierNum;
CP_DTREE_NODE_S nodes[];
int nodeNum;
CP_LBP_FEATURE_S feature[];
int featureNum;
float leaves[];
int leaveNum;
int subsets[];
int subsetNum;
}CP_CLASSIFIER_S; #ifdef __cplusplus
}
#endif
#endif /* _CP_ADABOOST_ */
【Adaboost算法】C++转C, 分类器结构设计的更多相关文章
- Adaboost 算法
一 Boosting 算法的起源 boost 算法系列的起源来自于PAC Learnability(PAC 可学习性).这套理论主要研究的是什么时候一个问题是可被学习的,当然也会探讨针对可学习的问题的 ...
- Adaboost 算法的原理与推导
0 引言 一直想写Adaboost来着,但迟迟未能动笔.其算法思想虽然简单“听取多人意见,最后综合决策”,但一般书上对其算法的流程描述实在是过于晦涩.昨日11月1日下午,邹博在我组织的机器学习班第8次 ...
- Adaboost算法初识
1.算法思想很简单: AdaBoost 是一种迭代算法,其核心思想是针对同一个训练集训练不同的分类器,即弱分类器,然后把这些弱分类器集合起来,构造一个更强的最终分类器.(三个臭皮匠,顶个诸葛亮) 它的 ...
- adaboost算法
三 Adaboost 算法 AdaBoost 是一种迭代算法,其核心思想是针对同一个训练集训练不同的分类器,即弱分类器,然后把这些弱分类器集合起来,构造一个更强的最终分类器.(很多博客里说的三个臭皮匠 ...
- 前向分步算法 && AdaBoost算法 && 提升树(GBDT)算法 && XGBoost算法
1. 提升方法 提升(boosting)方法是一种常用的统计学方法,在分类问题中,它通过逐轮不断改变训练样本的权重,学习多个分类器,并将这些分类器进行线性组合,提高分类的性能 0x1: 提升方法的基本 ...
- 集成学习值Adaboost算法原理和代码小结(转载)
在集成学习原理小结中,我们讲到了集成学习按照个体学习器之间是否存在依赖关系可以分为两类: 第一个是个体学习器之间存在强依赖关系: 另一类是个体学习器之间不存在强依赖关系. 前者的代表算法就是提升(bo ...
- Adaboost 算法实例解析
Adaboost 算法实例解析 1 Adaboost的原理 1.1 Adaboost基本介绍 AdaBoost,是英文"Adaptive Boosting"(自适应增强)的缩写,由 ...
- 浅谈 Adaboost 算法
http://blog.csdn.net/haidao2009/article/details/7514787 菜鸟最近开始学习machine learning.发现adaboost 挺有趣,就把自己 ...
- 机器学习--boosting家族之Adaboost算法
最近在系统研究集成学习,到Adaboost算法这块,一直不能理解,直到看到一篇博文,才有种豁然开朗的感觉,真的讲得特别好,原文地址是(http://blog.csdn.net/guyuealian/a ...
- Adaboost 算法的原理与推导——转载及修改完善
<Adaboost算法的原理与推导>一文为他人所写,原文链接: http://blog.csdn.net/v_july_v/article/details/40718799 另外此文大部分 ...
随机推荐
- iOS-Debug调试
转载:http://www.cnblogs.com/Leo_wl/p/4423922.html
- python内置模块(4)
这一部分是python内置模块系列的最后一部分,介绍了一些小巧有用的内置模块. 目录: 1.random 2.shelve 3.getpass 4.zipfile 5.tarfile 6.bisect ...
- java的poi技术下载Excel模板上传Excel读取Excel中内容(SSM框架)
使用到的jar包 JSP: client.jsp <%@ page language="java" contentType="text/html; charset= ...
- c# 编程语言 编译器 Roslyn
4 月3日,微软向公众发布了Roslyn编译器项目,该项目采用了Apache开源许可协议.C#的创始人 Anders Hejlsberg在Build大会的第二场主题演讲中将这一令人震惊的消息公之于众. ...
- 改造一下C# Substring()函数
C#的Substring()函数中,如果我们一不小心输入一个截取长度大于字符串的长时,就会收到一个异常:startIndex cannot be larger than length of strin ...
- Python基础:模块
一.概述 二.导入语句 1.基本语法 2.推荐风格 三.模块 1.模块名 2.模块属性 3.可导出的公有属性 4.直接执行 四.包 1.包名 2.包属性 3.可导出的公有属性 4.其他 五.导入原理 ...
- sencha grid templatecolumn模板列,actioncolumn和renderer实现单元格重绘
templatecolumn列: { xtype: 'templatecolumn', ...
- MVC分页之MvcPager使用
最近刚刚接触MVC不久,因项目中要用到分页,网上找了下资料,最后采用了MvcPager(http://www.webdiyer.com/),支持同步和Ajax异步分页.废话不多说了直接上代码. 一.M ...
- mfc110.dll丢失,解决方法
mfc110.dll下载_附文件使用方法 mfc110.dll是存放在windows系统中的一个重要dll文件,缺少它可能会造成部分软件或游戏无法正常运行.当系统提示“没有找到mfc110.dll”或 ...
- jquery fadeOut 异步
1. 概述 jquery实现动画效果的函数使用起来很方便,不过动画执行是异步的, 所以要把自定义的操作放在回调函数里. 2. example <html> <body> < ...