关于opencv-CascadeClassifier(级联分类器)的初步认识

级联分类器包括两个:训练和检测;
这里主要是介绍检测部分;
如果需要利用cascade训练模型,可以参考:opencv级联分类器训练过程记载

关于CascadeClassifier的简介:

CascadeClassifier是opencv下objdetect模块中用来做目标检测的级联分类器的一个类;简而言之是滑动窗口机制+级联分类器的方式;早期opencv版本仅支持haar特征的目标检测,分别在opencv2.2和2.4之后开始支持LBP和HOG特征的目标检测。

Haar特征:
类Haar-like特征描述如下图:
在这里插入图片描述
在所有缩放尺度下,这些特征组成了boosting分类器使用的全部“原材料”。他们从原始灰度图像的积分图中快速计算得出。

LBP特征:
LBP是一种描述图像局部纹理特征的算子,被用于Viola-Jones检测器,回想Haar小波,它是在一小块邻域上通过小波变换的特征向量,而LBP在构造特征向量与此不同,在一个长宽都为3的倍数的矩形上,将它分割成不相重叠的3*3的小块,在每一个小块上,用积分图计算像素和,然后将中心点像素与周围8个像素点比较得到一个8位的特征值,用来描述相应矩形的特征。

在这里插入图片描述
HOG特征:
局部归一化的梯度方向直方图,是一种对图像局部重叠区域的密集型描述符, 它通过计算局部区域的梯度方向直方图来构成特征。
本质:统计图像局部区域的梯度方向信息来作为该局部图像区域的表征。

解读CascadeClassifier源代码:

打开:opencvXXX/include/opencv2/objdetect.hpp:

class CV_EXPORTS_W CascadeClassifier
{
pubilc:
		
		 CV_WRAP CascadeClassifier();
		 // @param filename Name of the file from which the classifier is loaded.
		 //构造函数;@参数:要加载的分类器名称
		 CV_WRAP CascadeClassifier(const String& filename);
		 
		 ~CascadeClassifier();
		 
		 //@brief Checks whether the classifier has been loaded.
		 //判断分类器是否已加载
		 CV_WRAP bool empty() const;
		
		 //@param filename Name of the file from which the classifier is loaded. 
		 //加载分类器;@参数:要加载的分类器名称
		CV_WRAP bool load( const String& filename );

		// @brief Reads a classifier from a FileStorage node.
		// @note The file may contain a new cascade classifier (trained traincascade application) only.
		//从FileStorage节点中读取一个分类器
		CV_WRAP bool read( const FileNode& node );

		//关于detectMultiScale有三个重载:		
		//@brief Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.
	    //@param image Matrix of the type CV_8U containing an image where objects are detected.
 	    //@param objects Vector of rectangles where each rectangle contains the detected object, the  rectangles may be partially outside the original image.
 	    //@param scaleFactor Parameter specifying how much the image size is reduced at each image scale.
  	    //@param minNeighbors Parameter specifying how many neighbors each candidate rectangle should have to retain it.
	    //@param flags Parameter with the same meaning for an old cascade as in the function  cvHaarDetectObjects. It is not used for a new cascade.
    	//@param minSize Minimum possible object size. Objects smaller than that are ignored.
  	 	//@param maxSize Maximum possible object size. Objects larger than that are ignored. If `maxSize == minSize` model is evaluated on single scale.
		//实现多尺度检测,检测物体返回一组矩形;
		//image:输入CV_8U的待检测图像
		//objects:返回vector<Rect>&类型的检测结果,一组包含检测目标的矩形,其中的矩形可能会部分的在原始图像外侧
		//scaleFactor:指定在每个图像缩放时图像大小减少了多少,搜索前后两次窗口大小比例系数
		//minNeighbors:指定每一个候选矩形至少保留着多少相邻矩形
		//flags:对于旧级联分类器与cvHaarDetectObjects中有相同的含义,不作用于新的级联分类器
		//目标可检测的最小尺寸,小于该尺寸的目标将被忽略
		//目标可检测的最大尺寸,大于该尺寸的目标将被忽略
		CV_WRAP void detectMultiScale( 
		InputArray image,
		CV_OUT std::vector<Rect>& objects,
		double scaleFactor = 1.1,
		int minNeighbors = 3,
		int flags=0,
		Size minSize=Size(),
		Size maxSize=Size())

		//@param numDetections Vector of detection numbers for the corresponding objects.
		//numDetections:vector<int>&,返回对应目标的检测数目的vector
		CV_WRAP_AS(detectMultiScale2) void detectMultiScale( InputArray image,
                          CV_OUT std::vector<Rect>& objects,
                          CV_OUT std::vector<int>& numDetections,
                          double scaleFactor=1.1,
                          int minNeighbors=3, int flags=0,
                          Size minSize=Size(),
                          Size maxSize=Size() );

		// if `outputRejectLevels` is `true` returns `rejectLevels` and `levelWeights`
	    CV_WRAP_AS(detectMultiScale2) void detectMultiScale( InputArray image,
                          CV_OUT std::vector<Rect>& objects,
                          CV_OUT std::vector<int>& numDetections,
                          double scaleFactor=1.1,
                          int minNeighbors=3, int flags=0,
                          Size minSize=Size(),
                          Size maxSize=Size() );

	CV_WRAP bool isOldFormatCascade() const;
    CV_WRAP Size getOriginalWindowSize() const;
    CV_WRAP int getFeatureType() const;
    void* getOldCascade();
    CV_WRAP static bool convert(const String& oldcascade, const String& newcascade);
    void setMaskGenerator(const Ptr<BaseCascadeClassifier::MaskGenerator>& maskGenerator);
    Ptr<BaseCascadeClassifier::MaskGenerator> getMaskGenerator();
    Ptr<BaseCascadeClassifier> cc;
    
}



一般检测步骤(视频取帧):

1.load()加载xml级联分类器

CascadeClassifier g_cascade
g_cascade.load("XXX/YYY.xml");

2.从视频中取帧,导出image;
3.图像灰度化;
4.图像resize;
5.调用detectMultiScale()实现多尺度检测:

g_cascade.detectMultiScale(InputArray image,	//输入图像
			CV_OUT std::vector<Rect>& objects,	//输出检测到的目标区域
			double scaleFactor =1.1,			//搜索前后两次窗口大小比例系数,默认1.1,即每次搜索窗口扩大10%
			int minNeighbors = 3,				//构成检测目标的相邻矩形的最小个数 如果组成检测目标的小矩形的个数和小于minneighbors - 1 都会被排除,如果minneighbors为0 则函数不做任何操作就返回所有被检候选矩形框
			int flags = 0,						//若设置为CV_HAAR_DO_CANNY_PRUNING 函数将会使用Canny边缘检测来排除边缘过多或过少的区域 
			Size minSize = Size(),				//能检测的最小尺寸
			Size maxSize = Size()				//能检测的最大尺寸
			);

注:detectMultiScale的检测过程是从最大的size逐步缩小,而不是从最小尺寸逐步扩大(在之前的一个项目中,我需要继续缩小最小框的检测尺寸,因此尝试修改函数的最小尺寸,却并没能影响检测结果;其实应该在减小最小框的检测尺寸的同时,调整scaleFactor,让函数可以继续缩小检测框而不到达下限)


关于对级联分类器理解的一个误区:
之前一直以为流程是这样的,从最大尺寸开始,每次根据scaleFactor进行缩小,直到缩小到下限。。。
后来发现不管怎么缩小下限,都会输出同一个结果:
比如原先输出到尺寸为24,然而24的尺寸不能检测到我希望捕捉到更小目标,不断地调节级联分类器的最小尺寸和每次缩放比例,并不能继续减小输出框的尺寸,始终是24.
不断摸索查资料,最终才知道检测框的尺寸必须比训练样本的尺寸大,训练模型的样本尺寸就是24,因此不可能低于24,调整原图像的缩放比例才是解决之道。


关于minNeighbors的直观感受:
minNeighbors过小会出现大量误检;minNeighbors过大则会有一些目标检测不出;
调节minNeighbors类似于调节一个阈值,来判断是否为目标;

如果需要利用cascade训练模型,可以参考:opencv级联分类器训练过程记载

  • 34
    点赞
  • 203
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值