Opencv advanced 1

Face recognition in Opencv is based on Haar feature and +Adaboost cascade classifier to realize face recognition!

To understand this section, we must first understand what is a feature?

Feature is actually the result of pixel operation in a certain area! For example, haar feature is actually sliding in the image with the template listed below, and calculating the sum of pixels covered by white area minus the sum of pixels covered by black area, and the calculated result is haar eigenvalue!

Haar features are generally combined with Adaboost classifier for target recognition!

We need the knowledge of sports machine learning here! Fortunately, Opencv has trained data for us and extracted the features of human faces. There are corresponding xml feature files in the source code of opencv. We can quickly complete the function of face recognition by calling the good API provided by opencv!

The core api is:

Implementation steps:

HSV(Hue, Saturation, Value) is a color space created by A. R. Smith in 1978, also called Hexcone model.

The parameters of color in this model are hue (H), saturation (S) and lightness (V).

Measured from the angle, the range of values is 0 ~ 360, and it is calculated counterclockwise from red, with red being 0, green being 120 and blue being 240. Their complementary colors are yellow 60, cyan 180 and magenta 300 respectively;

Saturation s represents the closeness of the color to the spectral color. A color can be regarded as the result of mixing a spectral color with white. The greater the spectral color ratio, the higher the degree of color approaching spectral color and the higher the color saturation. High saturation, dark and bright color. The white light component of the spectral color is 0, and the saturation reaches the highest. Generally, the value range is 0% ~ 100%, and the larger the value, the more saturated the color is.

Brightness indicates the brightness of the color, and for the color of the light source, the brightness value is related to the brightness of the emitter; For the color of the object, this value is related to the transmittance or reflectivity of the object. Typical values range from 0% (black) to 100% (white).

Conclusion:

Note: In opencv, the values of h, s and v are respectively, instead of [0, 1] and [0,1];

Here we list some color values of hsv space and classify some purple as red in the table.

[Image upload failed ... (image -4f70f7- 1563843266225)]

. jpg)

Note that this can only be a rough judgment. According to our normal thinking, it is impossible to judge whether it is night or day at this critical point in the evening!

In a picture, if the color of an object is solid, then we can easily extract the object through color filtering in a certain range.

Below we have a picture of tennis. The color of tennis is green within a certain range. We can't find any other green pictures in this picture, so we can consider using green to extract them!

By default, the color space of a picture is the BGR color space. If we want to find and extract pure green, we may need to write something like (0,255,0). Suppose we want to represent a certain range of green, it will be very troublesome!

So we consider switching to HSV color space. We can easily know the range of green hue H, and the rest is to frame the saturation H and brightness V of the color!

Implementation steps:

Image binarization is the process of setting the gray value of pixels on the image to 0 or 255, which means that the whole image presents obvious black and white effect.

In digital image processing, binary image occupies a very important position, and the binarization of image greatly reduces the amount of data in the image, thus highlighting the outline of the target.

[image upload failed ... (image-a 31052-1563843266226)]

We use a global value as the threshold. But this is not good in all cases, for example, if the image has different lighting conditions in different areas. In this case, adaptive thresholds can help. Here, the algorithm determines the threshold of the pixel according to the small area around the pixel. Therefore, we obtain different thresholds for different regions of the same image, which provides better results for images with different illumination.

In addition to the above parameters, the method cv.adaptiveThreshold has three input parameters:

AdaptiveMethod determines how to calculate the threshold:

The block size determines the size of the nearby area. Subtract a constant from the average or weighted sum of nearby pixels.

The algorithm proposed by Otsu in Japan, also known as the maximum between-class variance method, is considered to be the best algorithm for threshold selection in image segmentation. The advantage of adopting this algorithm is high execution efficiency!

& ltimg src= "。 /img 2/Otsu . jpg " width = " 500 "/& gt;

If we regard the image as a signal, then the noise is an interference signal. When collecting images, we may introduce image noise due to various interferences. In a computer, an image is a matrix. To add noise to the original image, we only need to add a certain gray level to the pixels.

F(x, y) = I(x, y)+noise

Common noise is salt and pepper noise, why is it called salt and pepper noise? Because the pixels of the image will randomly become dark spots or white spots due to the influence of noise. The "pepper" here is not our common red pepper or green pepper, but an exotic "pepper" (a spice). We know that pepper is black and salt is white, so we have such an image name.

Next, we will generate 10% pepper noise and salt noise:

We should also note that the image matrix type of opencv is uint8, and the values below 0 and above 255 are not truncated, but modulo operation is used. That is 200+60 = 260% and 256 = 4. So we need to convert the original image matrix and noise image matrix into floating-point numbers for addition, and then turn back.