OpenCV Tutorials —— Cascade Classifier

Haar Feature-based Cascade Classifier for Object Detection

First, a classifier (namely a cascade of boosted classifiers working with haar-like features) is trained with a few hundred sample views of a particular object (i.e., a face or a car), called positive examples, that are scaled to the same size (say, 20x20), and negative examples - arbitrary images of the same size.  使用相同规格的正负样本进行训练

 

After a classifier is trained, it can be applied to a region of interest (of the same size as used during the training) in an input image. The classifier outputs a “1” if the region is likely to show the object (i.e., face/car), and “0” otherwise.

To search for the object in the whole image one can move the search window across the image and check every location using the classifier. The classifier is designed so that it can be easily “resized” in order to be able to find the objects of interest at different sizes, which is more efficient than resizing the image itself. So, to find an object of an unknown size in the image the scan procedure should be done several times at different scales. 不同尺度滑动窗口 ~~ 缩放模型而不是缩放图像

 

The word “cascade” in the classifier name means that the resultant classifier consists of several simpler classifiers (stages) that are applied subsequently to a region of interest until at some stage the candidate is rejected or all the stages are passed. 级联分类器是多个简单分类器的合成

The word “boosted” means that the classifiers at every stage of the cascade are complex themselves and they are built out of basic classifiers using one of four different boosting techniques (weighted voting). Currently Discrete Adaboost, Real Adaboost, Gentle Adaboost and Logitboost are supported.  每阶段的简单分类器加权投票

The basic classifiers are decision-tree classifiers with at least 2 leaves. Haar-like features are the input to the basic classifiers, and are calculated as described below. 两层决策树,Haar 特征作为初始特征输入,计算出如下中层特征

The feature used in a particular classifier is specified by its shape (1a, 2b etc.), position within the region of interest and the scale (this scale is not the same as the scale used at the detection stage, though these two scales are multiplied).

For example, in the case of the third line feature (2c) the response is calculated as the difference between the sum of image pixels under the rectangle covering the whole feature (including the two white stripes and the black stripe in the middle) and the sum of the image pixels under the black stripe multiplied by 3 in order to compensate for the differences in the size of areas. The sums of pixel values over a rectangular regions are calculated rapidly using integral images (see below and the integral() description).

 

CascadeClassifier::load

Loads a classifier from a file.

 

CascadeClassifier::read

Reads a classifier from a FileStorage node.

 

CascadeClassifier::detectMultiScale

Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.

void CascadeClassifier::detectMultiScale(const Mat& image, vector<Rect>& objects, double scaleFactor=1.1, intminNeighbors=3, int flags=0, Size minSize=Size(), Size maxSize=Size())

 

CascadeClassifier::setImage

The function is automatically called by & objects, double scaleFactor, int minNeighbors, int flags, Size minSize, Size maxSize)">CascadeClassifier::detectMultiScale() at every image scale. But if you want to test various locations manually using & feval, Point pt, double& weight)">CascadeClassifier::runAt(),

 

CascadeClassifier::runAt

The function returns 1 if the cascade classifier detects an object in the given location. Otherwise, it returns negated index of the stage at which the candidate has been rejected.

 

Code

#include "stdafx.h"

#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

/** Function Headers */
void detectAndDisplay( Mat frame );

/** Global variables */
String face_cascade_name = "haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
string window_name = "Capture - Face detection";
RNG rng(12345);

/** @function main */
int main( int argc, const char** argv )
{
	CvCapture* capture;
	Mat frame;

	//-- 1. Load the cascades
	if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };
	if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };

	//-- 2. Read the video stream
	capture = cvCaptureFromCAM( -1 );
	if( capture )
	{
		while( true )
		{
			frame = cvQueryFrame( capture );

			//-- 3. Apply the classifier to the frame
			if( !frame.empty() )
			{ detectAndDisplay( frame ); }
			else
			{ printf(" --(!) No captured frame -- Break!"); break; }

			int c = waitKey(10);
			if( (char)c == ‘c‘ ) { break; }
		}
	}
	return 0;
}

/** @function detectAndDisplay */
void detectAndDisplay( Mat frame )
{
	std::vector<Rect> faces;
	Mat frame_gray;

	cvtColor( frame, frame_gray, CV_BGR2GRAY );
	equalizeHist( frame_gray, frame_gray );	// 直方图均衡化,增强对比度

	//-- Detect faces
	face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );

	for( size_t i = 0; i < faces.size(); i++ )
	{
		Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
		ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );

		Mat faceROI = frame_gray( faces[i] );
		std::vector<Rect> eyes;

		//-- In each face, detect eyes
		eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );

		for( size_t j = 0; j < eyes.size(); j++ )
		{
			Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
			int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
			circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
		}
	}
	//-- Show what you got
	imshow( window_name, frame );
}
时间: 2024-10-12 13:41:35

OpenCV Tutorials —— Cascade Classifier的相关文章

Cascade Classifier Training 没有基础也会目标检测啦

Cascade Classifier Training 具体自己看: http://docs.opencv.org/2.4.13.2/doc/user_guide/ug_traincascade.html#cascade-training --------------好像不太地道,这样就蹭发了篇博客

学习opencv tutorials

1.opencv里头动态库和静态库的区别 lib是动态库,staticlib是静态库. 这是opencv tutorials中对动态库和静态库的说明.动态库是在runtime时候才load的库文件.而静态库文件会在你build的时候build-in inside your exe file.优点是可以避免误删,缺点是应用程序变大,加载时间也会变长. 2.  Visual Studio中solution和project的关系 在VS中,一个solution中可以包含多个project. 3.  两

OpenCV Tutorials &mdash;&mdash; Hough Line Transform

霍夫直线变换 -- 用于检测图像中的直线 利用图像空间和Hough参数空间的点--直线对偶性,把图像空间中的检测问题转换到参数空间,通过在参数空间进行简单的累加统计,然后在Hough参数空间中寻找累加器峰值的方法检测直线 Standard and Probabilistic Hough Line Transform OpenCV implements two kind of Hough Line Transforms: The Standard Hough Transform It consis

OpenCV Tutorials &mdash;&mdash; Feature Matching with FLANN

Extractors of keypoint descriptors in OpenCV have wrappers with a common interface that enables you to easily switch between different algorithms solving the same problem.   DescriptorExtractor::compute Computes the descriptors for a set of keypoints

OpenCV Tutorials &mdash;&mdash; Creating a video with OpenCV

写video 需要用到 VideoWriter  视频文件可看作一个容器 视频的类型由视频文件的后缀名来指定   Due to this OpenCV for video containers supports only the avi extension, its first version. A direct limitation of this is that you cannot save a video file larger than 2 GB. Furthermore you ca

OpenCV Tutorials &mdash;&mdash; Image Moments

图像矩 Moments moments(InputArray array, bool binaryImage=false ) Parameters: array – Raster image (single-channel, 8-bit or floating-point 2D array) or an array ( or ) of 2D points (Point or Point2f ). binaryImage – If it is true, all non-zero image pi

OpenCV Tutorials &mdash;&mdash; Hough Circle Transform

Hough 圆变换 和 Hough 直线变换原理相同,只是参数空间不同 : In the line detection case, a line was defined by two parameters . In the circle case, we need three parameters to define a circle: where define the center position (gree point) and is the radius, which allows us

OpenCV Tutorials &mdash;&mdash; Sobel Derivatives

图像边缘 -- 像素灰度值变换剧烈的点 You can easily notice that in an edge, the pixel intensity changes in a notorious way. A good way to expresschanges is by using derivatives. A high change in gradient indicates a major change in the image.   To be more graphical,

OpenCV Tutorials &mdash;&mdash; Making your own linear filters

kernel A kernel is essentially a fixed size array of numerical coefficeints along with an anchor point in that array, which is tipically located at the center. The value of the convolution is calculated in the following way: 1,Place the kernel anchor