Opencv learning --- enable the camera to detect the profile picture

Source: Internet
Author: User

Opencv has a cascade classifier for detecting various parts of the human body, which is in the sources/data/haarcascades folder of opencv.

Here you should choose a file that can detect the human Avatar and the cascade classifier of the eye.

They are:

Detect Avatar Haarcascade_frontalface_alt.xml or haarcascade_frontalface_alt2.xml
Eye Detection Haarcascade_eye.xml or haarcascade_eye_tree_eyeglasses.xml

 

 

The function used for detection is:

cvHaarDetectObjects(CvArr* image,            CvHaarClassifierCascade *cascade,            cvMemStorage* storage,            double scale_factor=1.1,                int min_neighors=0,            int flags=0,            CvSize min_size=cvSize(0,0),                    CvSize max_size=cvSize(0,0)                                );                        

 

The cascade Haar classifier Cascade internal identification form storage is used to store the memory area of the detected sequence of candidate target rectangular boxes. You can use cvcreatestorage (INT block_size) to create the buffer memory. When block_size = 0 is set, 64 K buffer space memory blocks are allocated by default. In the two successive scans, scale_factor searches for the proportional coefficient of the window. For example, 1.1 means to expand the search window by 10% min_neighbors to form the minimum number of adjacent rectangles of the detection target (default:-1 ). If the number of small rectangles that make up the detection target and smaller than the min_neighbors-1 are excluded. If min_neighbors is 0, the function returns all rectangular boxes of candidates to be checked without any operation. This setting value is generally used in a user-defined combination of detection results. Flags operation method. The only operation method that can be defined currently is cv_haar_do_canny_pruning. If this parameter is set, the function uses the Canny edge detector to exclude image areas with few or many edges, because such areas generally do not include the target to be checked. This method is used in Face Detection by setting the threshold, and thus the detection speed is improved. Min_size: minimum size of the detection window. By default, it is set as the sample size used for Classifier Training (the default size in Face Detection is ~ 20 × 20 ).
1 # DEFINE _ crt_secure_no_warnings 2 # include <iostream> 3 # include "CV. H "4 # include" highgui. H "5 using namespace CV; 6 using namespace STD; 7 8 iplimage * dectimg (iplimage * getimg ); 9 // write the file paths of these two files clearly or copy them to the project folder 10 // check the human eye 11 // static char * cascade_name = "haarcascade_eye_tree_eyeglasses.xml "; 12 // face detection 13 static char * cascade_name = "haarcascade_frontalface_alt2.xml"; 14 15 cvhaarclassifiercascade * C Ascade = 0; 16 int main () 17 {18 // use cvload () to load the cascade classiercascade file 19 cascade = (cvhaarclassifiercascade *) cvload (cascade_name ); 20 21 // open the camera and read the personal video 22 cvcapture * capture = cvcreatecameracapture (cv_cap_any); 23 // define the iplimage pointer and initialize it. 24 iplimage * GetFile = cvqueryframe (capture); 25 // name window 26 cvnamedwindow ("result "); 27 // read frame 28 while (GetFile = cvqueryframe (capture) cyclically ))! = NULL) 29 {30 // after each image frame is read, the detection function 31 iplimage * Picture = dectimg (GetFile); 32 33 cvshowimage ("result", picture) is displayed ); 34 // The interval is 15 us. Wait for the key to be entered. If the key is ESC (the key value of this key is 27), exit. 35 if (cvwaitkey (15) = 27) break; 36} 37 cvwaitkey (); 38 // release image pointer 39 cvreleaseimage (& GetFile ); 40 // release the pointer to read the video frame 41 cvreleasecapture (& capture); 42 // close all windows 43 cvdestroyallwindows (); 44 return 0; 45} 46 47 // detect the target image in the image and use a rectangle to circle the target 48 iplimage * dectimg (iplimage * getimg) 49 {50 // clone image 51 iplimage * IMG = cvcloneimage (getimg); 52 double scale = 1.2; 53 // zoom out image, increase the detection speed by 54 iplimage * smallimg = cvcreateimage (cvsize (IMG-> width/scale, IMG-> height/scale), IMG-> depth, IMG-> nchannels ); 55 cvresize (IMG, smallimg); 56 // create a single-channel image 57 iplimage * grayimg = cvcreateimage (cvgetsize (smallimg), smallimg-> depth, 1 ); 58 // convert to grayscale image 59 cvcvtcolor (smallimg, grayimg, cv_rgb2gray); 60 // create cache 61 cvmemstorage * storage = 0; 62 storage = cvcreatememstorage (0 ); 63 // balance images 64 cvequalizehist (grayimg, grayimg); 65 cvclearmemstorage (storage); 66 // detect images, the rectangle 67 cvseq * objects = cvhaardetectobjects (grayimg, cascade, storage, scale, 3, 0, cvsize (30, 30); 68 if (objects-> total> 0) in the target region will be obtained) 69 {70/* 71 for (INT I = 0; I <objects-> total; I ++) 72 {73 cvpoint center; 74 int radius; 75 cvrect * rect = (cvrect *) cvgetseqelem (objects, I); 76 center. X = (rect-> X + rect-> width/2) * scale; 77 center. y = (rect-> Y + rect-> height/2) * scale78; 79 radius = cvround (rect-> width + rect-> height) * 0.26 * scale ); 80 cvcircle (getimg, center, radius, cvscalar (14, 45, 73), 4); 81} 82 */83 for (INT I = 0; I <objects-> total; I ++) 84 {85 cvrect * rect = (cvrect *) cvgetseqelem (objects, I); 86 cvrectangle (getimg, 87 cvpoint (cvround (rect-> X * scale), cvround (rect-> y * scale )), 88 cvpoint (cvround (1.2 * (rect-> X + rect-> width), 89 cvround (1.2 * (rect-> Y + rect-> height ))), 90 cvscalar (155,), 5); 91} 92} 93 else 94 return NULL; // if no target image is detected, return a null pointer 95 return getimg; 96}

 

Opencv learning --- enable the camera to detect the profile picture

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.