Basic Learning notes: opencv (1): An Example of facedetect in opencv

Source: Internet
Author: User

A mainstream face detection method is like Haar + embedding sting, which is also used in opencv. This method can be applied to the detection of rigid objects, provided that the cascade classifier (for example, the Haar-like feature) is trained. Once the training data is completed, the cascadeclassifier class in opencv is called directly, you can use several simple member functions to complete the detection function. Therefore, it is very easy to use. The following is an example of facedetect in samples in opencv.

Of course, the source example takes into account the camera, video, and image situations, and there are many error handling expressions. The code is omitted here, because it does not seem very concise. Otherwise, you also need to enter it through the command line, for example:

 

There are many items to be entered. If an error occurs in the input, the following information is displayed:

 

For convenience, the code is concise. The modified code and comments are as follows:

1 // face_detect.cpp: defines the entry point of the console application.
2 //
3
4 # include "stdafx. H"
5
6 # include "opencv2/objdetect. HPP"
7 # include "opencv2/highgui. HPP"
8 # include "opencv2/imgproc. HPP"
9 # include "opencv2/ml. HPP"
10
11 # include <iostream>
12 # include <stdio. h>
13
14 using namespace STD;
15 using namespace CV;
16
17 void detectanddraw (MAT & IMG,
18 cascadeclassifier & cascade, cascadeclassifier & nestedcascade,
19 double scale );
20
21 string cascadename = "./haarcascade_frontalface_alt2.xml"; // face training data
22 // string nestedcascadename = "./haarcascade_eye_tree_eyeglasses.xml"; // human eye training data
23 string nestedcascadename = "./haarcascade_eye.xml"; // human eye training data
24
25 int main (INT argc, const char ** argv)
26 {
27 mat image;
28 cascadeclassifier cascade, nestedcascade; // create a cascading classifier object
29 double scale = 1.3;
30
31 // image = imread ("lena.jpg", 1); // read the Lena image
32 image = imread ("lele_with_hands.png", 1 );
33 namedwindow ("result", 1); // After opencv2.0, The namedwindow function will automatically destroy the window.
34
35 if (! Cascade. Load (cascadename) // load the cascade classifier from the specified file directory
36 {
37 cerr <"error: cocould not load classifier Cascade" <Endl;
38 return 0;
39}
40
41 if (! Nestedcascade. Load (nestedcascadename ))
42 {
43 cerr <"Warning: cocould not load classifier Cascade for nested objects" <Endl;
44 return 0;
45}
46
47 If (! Image. Empty () // read image data cannot be blank
48 {
49 detectanddraw (image, cascade, nestedcascade, scale );
50 waitkey (0 );
51}
52
53 return 0;
54}
55
56 void detectanddraw (MAT & IMG,
57 cascadeclassifier & cascade, cascadeclassifier & nestedcascade,
58 double scale)
59 {
60 int I = 0;
61 double T = 0;
62 vector <rect> faces;
63 const static scalar colors [] = {cv_rgb (0,0, 255 ),
64 cv_rgb (0,128,255 ),
65 cv_rgb (0,255,255 ),
66. cv_rgb (0,255, 0 ),
67 cv_rgb (255,128, 0 ),
68 cv_rgb (255,255, 0 ),
69 cv_rgb (255, 0, 0 ),
70 cv_rgb (255,)}; // different colors are used to represent different faces.
71
72 mat gray, smallimg (cvround (IMG. Rows/scale), cvround (IMG. Cols/scale), cv_8uc1); // scale down the image to speed up Detection
73
74 cvtcolor (IMG, gray, cv_bgr2gray); // because Haar-like features are used, they are all based on grayscale images. here we need to convert them to grayscale images.
75 resize (Gray, smallimg, smallimg. Size (), 0, 0, inter_linear); // scale down the size to 1/scale and use linear interpolation
76 equalizehist (smallimg, smallimg); // histogram balancing
77
78 t = (double) cvgettickcount (); // used to calculate the algorithm execution time
79

// Detect faces
// In the detectmultiscale function, smallimg indicates that the input image to be detected is smallimg, faces indicates the detected face target sequence, and 1.1 indicates
// The image size decreases by 1.1. 2 indicates that each target must be detected at least three times before it is a real target (because the surrounding pixels and different windows are large ).
// The face can be detected for all the smaller values). cv_haar_scale_image indicates that the image is scaled instead of the zoom classifier. The size (30, 30) is the target.
// Minimum maximum size
84 cascade. detectmultiscale (smallimg, faces,
85 1.1, 2, 0
86 // | cv_haar_find_biggest_object
87 // | cv_haar_do_rough_search
88 | cv_haar_scale_image
89,
90 size (30, 30 ));
91
92 t = (double) cvgettickcount ()-T; // subtract the algorithm execution time
93 printf ("detection time = % G Ms \ n", t/(double) cvgettickfrequency () * 1000 .));
94 for (vector <rect >:: const_iterator r = faces. Begin (); R! = Faces. End (); R ++, I ++)
95 {
96 mat smallimgroi;
97 vector <rect> nestedobjects;
98 point center;
99 scalar color = colors [I % 8];
100 int radius;
101 center. x = cvround (R-> X + R-> width * 0.5) * scale); // restore to the original size
102 center. Y = cvround (R-> Y + R-> height * 0.5) * scale );
103 radius = cvround (R-> width + R-> height) * 0.25 * scale );
104 circle (IMG, center, radius, color, 3, 8, 0 );
105
106 // detects the human eye and draws the human eye on each face map
107 If (nestedcascade. Empty ())
108 continue;
109 smallimgroi = smallimg (* R );
110
111 // same as the function above
112 nestedcascade. detectmultiscale (smallimgroi, nestedobjects,
113 1.1, 2, 0
114 // | cv_haar_find_biggest_object
115 // | cv_haar_do_rough_search
116 // | cv_haar_do_canny_pruning
117 | cv_haar_scale_image
118,
119 size (30, 30 ));
120 for (vector <rect>: const_iterator Nr = nestedobjects. Begin (); NR! = Nestedobjects. End (); NR ++)
121 {
122 center. x = cvround (R-> X + nR-> width * 0.5) * scale );
123 center. Y = cvround (R-> Y + nR-> height * 0.5) * scale );
124 radius = cvround (NR-> width + nR-> height) * 0.25 * scale );
125 circle (IMG, center, radius, color, 3, 8, 0); // draw the eyes, which is the same as the image of the corresponding face.
126}
127}
128 CV: imshow ("result", IMG );
129}

 

The following figure shows how to run the Lena diagram:

 

The following figure shows how to run a multi-person graph:

 

It can be seen that in the multi-person diagram, not everyone's face can be detected. The face, especially on the image boundary, that is, the partially blocked face, cannot be detected. In addition, when the human eyes are not particularly positive, they cannot be detected completely, and they do not know the specific reason. The self-contained in opencv is because the human eye database training is not completely good. Because the training of such data is indeed very troublesome.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.