“Alright! This is where we start having some fun! The concept behind the Haar Cascade and how it is used in the real world is nothing short of amazing. So what is it?”
“Haar Cascade is a machine learning object detection algorithm used to identify objects in an image or video and based on the concept of features proposed by Paul Viola and Michael Jones in their paper "Rapid Object Detection using a Boosted Cascade of Simple Features" in 2001.“
Haar 级联分类器是一种机器学习算法,最初由 Paul Viola 和 Michael Jones 在 2001 年的论文《Rapid Object Detection using a Boosted Cascade of Simple Features》中提出。该算法能够通过特征来识别图像或视频中的物体。
“It is a machine learning based approach where a cascade function is trained from a lot of positive[a] and negative[a] images. It is then used to detect objects in other images.”
“Lets take face detection as an example. Initially, the algorithm needs a lot of positive images of faces and negative images without faces to train the classifier. Then we need to extract features from it.”
“First step is to collect the Haar Features. A Haar feature considers adjacent rectangular regions at a specific location in a detection window, sums up the pixel intensities in each region and calculates the difference between these sums.”
“But among all these features we calculated, most of them are irrelevant. For example, consider the image below. Top row shows two good features. The first feature selected seems to focus on the property that the region of the eyes is often darker than the region of the nose and cheeks. The second feature selected relies on the property that the eyes are darker than the bridge of the nose. But the same windows applying on cheeks or any other place is irrelevant.”
“So how do we select the best features out of 160000+ features? This is accomplished using a concept called Adaboost which both selects the best features and trains the classifiers that use them. This algorithm constructs a “strong” classifier as a linear combination of weighted simple “weak” classifiers. The process is as follows.”
“During the detection phase, a window of the target size is moved over the input image, and for each subsection of the image and Haar features are calculated. You can see this in action in the video below. This difference is then compared to a learned threshold that separates non-objects from objects. Because each Haar feature is only a "weak classifier" (its detection quality is slightly better than random guessing) a large number of Haar features are necessary to describe an object with sufficient accuracy and are therefore organized into cascade classifiers to form a strong classifier.”
在检测阶段,一个特定的窗口在输入图像上移动,并在每个位置上计算 Haar 特征。下面的视频演示了这一过程。计算结果将与阈值进行比较以区分是否为检测目标,这个阈值是通过学习得到的。由于每个 Haar 特征只是一个“弱分类器”(弱分类器的检测质量仅比随即猜测好一点儿),我们需要大量的 Haar 特征才能够对目标进行精确的检测,因此这些弱分类器被级联成了强分类器。
“The cascade classifier[b] consists of a collection of stages, where each stage is an ensemble of weak learners. The weak learners are simple classifiers called decision stumps. Each stage is trained using a technique called boosting. Boosting provides the ability to train a highly accurate classifier by taking a weighted average of the decisions made by the weak learners.”
“Each stage of the classifier labels the region defined by the current location of the sliding window as either positive or negative. Positive indicates that an object was found and negative indicates no objects were found. If the label is negative, the classification of this region is complete, and the detector slides the window to the next location. If the label is positive, the classifier passes the region to the next stage. The detector reports an object found at the current window location when the final stage classifies the region as positive.”
“The stages are designed to reject negative samples as fast as possible. The assumption is that the vast majority of windows do not contain the object of interest. Conversely, true positives are rare and worth taking the time to verify.
* A true positive occurs when a positive sample is correctly classified.
* A false positive occurs when a negative sample is mistakenly classified as positive.
* A false negative occurs when a positive sample is mistakenly classified as negative.”
“To work well, each stage in the cascade must have a low false negative rate. If a stage incorrectly labels an object as negative, the classification stops, and you cannot correct the mistake. However, each stage can have a high false positive rate. Even if the detector incorrectly labels a nonobject as positive, you can correct the mistake in subsequent stages. Adding more stages reduces the overall false positive rate, but it also reduces the overall true positive rate.”
“Cascade classifier training requires a set of positive samples and a set of negative images. You must provide a set of positive images with regions of interest specified to be used as positive samples. You can use the Image Labeler to label objects of interest with bounding boxes. The Image Labeler outputs a table to use for positive samples. You also must provide a set of negative images from which the function generates negative samples automatically. To achieve acceptable detector accuracy, set the number of stages, feature type, and other function parameters.“
* Notice how the algorithm moves the window systematically over the image, applying the Haar features as it is trying to detect the face. This is depicted by the green rectangles.
* Notice underneath the red boundary square, we see the classifier executing stages quickly discarding window frames that are clearly not a match (stages 1-25)
* To the right of the stage we see the how well it performed in identifying the face.
* Notice as it gets closer and closer to identifying the face, the number of stages increases into the 20s. (around the 1 minute mark). This demonstrates the cascading effect where the early stages are discarding the input as it has identified them as irrelevant. As it gets closer to finding a face it pays closer attention.”
“Let me know if you have any questions or have any comments below.
I want to make sure I got this post right. It will be critical that you understand this before we go into the next section where we will implement a full Custom Object Haar Cascade detector.”
“I don't know about you, but I find the best way to understand something is by doing it. Conceptually we now have an idea for how the machine learning Haar Cascade object detection works. Now lets build a real world custom Object Detector, train it, and see it in action. I have a really cool example for us! Click on the button below.”
按钮的连接错了,所以我实在不知道作者所说的 cool example 是哪一个,不过作者网站确实有一些很酷的例子,感兴趣的朋友可以点击连接进入[作者网站](http://www.willberger.org/category/ai/),也可以自行练习一些例子,毕竟理解事物的最好方法就是实践。
2. Docs, OpenCV. “Face Detection Using Haar Cascades.” OpenCV: Face Detection Using Haar Cascades, 4 Aug. 2017, docs.opencv.org/3.3.0/d7/d8b/tutorial_py_face_detection.html.
A Haar-like feature considers adjacent rectangular regions at a specific location in a detection window, sums up the pixel intensities in each region and calculates the difference between these sums.
This difference is then used to categorize subsections of an image. For example, let us say we have an image database with human faces. It is a common observation that among all faces the region of the eyes is darker than the region of the cheeks. Therefore a common Haar feature for face detection is a set of two adjacent rectangles that lie above the eye and the cheek region. The position of these rectangles is defined relative to a detection window that acts like a bounding box to the target object (the face in this case).
An integral image is summed-area table is a data structure and algorithm for quickly and efficiently generating the sum of values in a rectangular subset of a grid. To understand this look at image 1 and image 2. Image 1 is the source table, Image 2 is the summation table. Notice in Image 2 row 1, col 2 value 33 is sum of Image 1 row 1 (col 1 + col 2).
Problems in machine learning often suffer from the curse of dimensionality — each sample may consist of a huge number of potential features (for instance, there can be 162,336 Haar features, as used by the Viola–Jones object detection framework, in a 24×24 pixel image window), and evaluating every feature can reduce not only the speed of classifier training and execution, but in fact reduce predictive power, per the Hughes Effect. Unlike neural networks and SVMs, the AdaBoost training process selects only those features known to improve the predictive power of the model, reducing dimensionality and potentially improving execution time as irrelevant features need not be computed.
2. [What’s the Difference Between Haar-Feature Classifiers and Convolutional Neural Networks?](https://towardsdatascience.com/whats-the-difference-between-haar-feature-classifiers-and-convolutional-neural-networks-ce6828343aeb)
3. [Computer Vision — Detecting objects using Haar Cascade Classifier](https://towardsdatascience.com/computer-vision-detecting-objects-using-haar-cascade-classifier-4585472829a9)
4. [Deep Learning Haar Cascade Explained](http://www.willberger.org/cascade-haar-explained/)