【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 另外此文大部分 ...
随机推荐
- EB-SAM9G45裸机程序下载方法
开发板:EB-SAM9G45 这里提供一种裸程序下载的方法. 在官方提供的下载方法中有手动下载和自动下载,它们都离不开SAM-BA软件,而该软件使用比较麻烦,而且操作不当很容易导致电脑蓝屏,还有一个很 ...
- sql order by 排序多个字段
order by 多个字段,每个字段后面都有排序方式,默认ASC 例如:select table a order by a.time1 ,a.time2 desc,a.time3 asc
- RandomUser – 生成随机用户 JSON 数据的 API
RandomUser 是一个 API,它为您提供了一个或者一批随机生成的用户.这些用户可以在 Web 应用程序原型中用作占位符,将节省您创建自己的占位符信息的时间.您可以使用 AJAX 或其他方法来调 ...
- js获取html5 audio 音频时长方法
<audio src="我的好兄弟.mp3" controls="controls" id="audio" style=" ...
- 如何用参数化SQL语句污染你的计划缓存
你的SQL语句的参数化总是个好想法.使用参数化SQL语句你不会污染你的计划缓存——错!!!在这篇文章里我想向你展示下用参数化SQL语句就可以污染你的计划缓存,这是非常简单的! ADO.NET-AddW ...
- 警惕SQL语句陷井
以下SQL段,大家认为结果是什么呢? DECLARE @A VARCHAR(50) SET @A='Zuowenjun.cn' SELECT TOP 1 @A=ISNULL(FIELDNAME,'DE ...
- Spring总结——AOP、JDBC和事务的总结
1.上一次总结了 Spring 的核心三大组件(Core,Beans,Context),今天总结的 AOP.JDBC和事务都可以看成是核心三大组件的应用. 其中 Spring 的事务管理又以 AOP ...
- spring日记------部署环境、写demo
一.安装jdk1.7 祥见http://zhinan.sogou.com/guide/detail/?id=1610006590 二.创建web项目 略 三.配置ssm环境 3.1添加spring.m ...
- 无法将类型为“System.Windows.Controls.SelectedItemCollection”的对象强制转换为类型“System.Collections.Generic.IList`1
在WPF中DataGrid 选择事件中获取SelectedItems 报错如下 无法将类型为“System.Windows.Controls.SelectedItemCollection”的对象强制转 ...
- sql添加合计
在项目中发现有这样的写法 SELECT ZoneID,CountSQAZFZSBJZ3G+CountSQGZJRJZSL3G AS column1FROM G3MulticarrierSiteCove ...