Preface
Skin Detection uses skin color information thresholds for detection. However, these thresholds generally use space such as HSV or ycrcb instead of RGB space. Skin Detection uses a statistical method to collect a large amount of skin information, and then you can obtain the threshold of a certain component of the color space. By using this threshold, you can initially split the skin. Of course, opencv also comes with a skin detection operator, which is included in the class cvadaptiveskindetector. This section describes the performance of the skin detection algorithm that comes with opencv.
Development Environment: opencv2.4.3 + qtcreator2.5.1
Lab Basics
Opencv comes with reference to algorithms. Some netizens say an adaptive real-time skin Detector Based on Hue thresholding: A Comparison on two motion tracking methods. For details, see reference 1. I have read a little bit about the algorithm flow of this paper and the source code of the cvadaptiveskindetector class. The implementation of this source code is similar to that in this article, which mainly combines skin threshold segmentation and motion detection. The flowchart of this algorithm is as follows:
We can see that the skin detection algorithm is performed in the HSV space. The HSV space model is shown as follows:
H indicates the color, that is, the most distinctive place of different colors. See the arc in the model. s indicates the saturation, that is, the purity of white light, see the radius in the model. The larger the value of S indicates the more saturated the color, that is, the more pure the corresponding color. The V indicates the brightness. See the vertical line in the model. The larger the value of V indicates the brighter the corresponding point.
Let's take a look at two important functions in the cvadaptiveskindetector class:
Cvadaptiveskindetector (int
Samplingdivider = 1, int morphingmethod = morphing_method_none );
This function is a class constructor. Parameter 1 indicates the sample sampling interval. The default value is 1, indicating that no downsampling is performed. Parameter 2 indicates the graphics operation mode, this allows you to perform graphic operations on images detected by skin. There are three possible values. For morphing_method_erode, only one corrosion operation is performed. For morphing_method_erode_erode, two consecutive corrosion operations are performed. For morphing_method_erode_dilate, it indicates that the first corrosion operation is performed, and then the second expansion operation is performed.
Virtual void
Process (iplimage * inputbgrimage, iplimage * outputhuemask );
This function is the core function of Skin Detection. Parameter 1 is the input image for skin detection, and parameter 2 is the mask image for the output skin. If the value is 1, it indicates that the pixel is the skin. Otherwise, when it is 0, it indicates that the pixel is not the skin. Note that this function only has the C version of opencv, because the cvadaptiveskindetector class is placed in the contrib directory of the opencv source code, which indicates a relatively new but immature algorithm, therefore, the C ++ version of opencv is not provided for the time being. Therefore, the image data types of parameters 1 and 2 are both iplimage. If you want to use mat, you must first perform a small conversion. For details, refer to the Code section.
C/C ++ knowledge point summary:
After reading the source code of the cvadaptiveskindetector class, we found that in the class design, a class can be designed internally. For example, cvadaptiveskindetector embeds a histogram class.
Lab results
The following figure shows the skin color detection (mainly the detection of the Shot ):
After the detection, the items are as follows:
It can be seen that the detection effect is normal, because many backgrounds are taken into account.
Experiment code and comments:
Main. cpp:
# Include <opencv2/CORE/core. HPP> # include <opencv2/highgui. HPP> # include <opencv2/contrib. HPP> # include <opencv2/imgproc. HPP> # include <iostream> using namespace STD; using namespace CV; MAT input_image; MAT output_mask; MAT output_image; iplimage * src_image; iplimage * dst_image; void main () {videocapture cam (0); If (! Cam. isopened () return; namedwindow ("input image"); namedwindow ("output mask"); namedwindow ("output image"); cvadaptiveskindetector skin_detector (1, cvadaptiveskindetector :: morphing_method_none); While (true) {cam> input_image; src_image = & input_image.operator iplimage (); If (dst_image = NULL) dst_image = cvcreateimage (cvsize (src_image-> width, src_image-> height), priority, 1); // you must first allocate a memory skin_detector.process (src_image, dst_image); output_mask = 255 * MAT (dst_image); input_image.copyto (output_image, output_mask); imshow ("input image", input_image); imshow ("output mask", output_mask); imshow ("output image", output_image); output_image.setto (0 ); if (27 = waitkey (30) return ;}
Experiment summary:
Skin Detection is affected by the background, light, skin color, and so on. Therefore, it is difficult to make the effect better. This algorithm of opencv is also very effective.
References:
Opencv-Based Skin Detection
An adaptive real-time skin Detector Based on Hue thresholding: A Comparison on two motion tracking methods