Face Detection Using opencv

Source: Internet
Author: User

How to detect a face in a pair of images involves some very complex computation in computer graphics. If these computation relies on programmers to program themselves, the workload will be quite large. Opencv stands for open computer vision, which refers to open computer vision resource code. It has the following features: it features a unified structure and function definition, powerful image and matrix computing capabilities, convenient and flexible interfaces, and is an ideal tool for secondary development in computer vision, image processing, and pattern recognition. It can run in various versions of Windows or Linux. The source code of opencv is written in C and C ++ and is fully open, so it has good portability.
Visual c ++ 6.0, Microsoft Visual Studio 2003, Borland C ++ builderx, and other environments allow you to conveniently use the libraries provided by opencv for actual development. This program uses Visual C ++ 2005 as the development environment.

1. installation and configuration of opencv in Visual C ++ 2005

1. opencv Installation

First go to the official website of opencv (http://sourceforge.net/projects/opencvlibrary) download opencv and install, the installation process is very simple, as long as according to the installation wizard step by step.

2. perform global settings for Visual C ++ 2005

1) Open visual C ++ 2005 and select the "Tools | options" menu. the dialog box shown in 1 is displayed.

2) Select "projects and solutions (project and solution) | VC ++ directories (VC ++ directory)" in the list box on the left )".

3) In the show directories for drop-down list box, select "library files )".

4) Locate and add "<opencv installation directory> opencvlib" in the library file list box on the right ".

5) Select "include files" in the "show directories for (directory with the following content displayed)" drop-down list box, locate and add the following path in the list box on the right:

<Opencv installation directory> opencvcxcoreinclude

<Opencv installation directory> opencvcvinclude

<Opencv installation directory> opencvcvauxinclude

<Opencv installation directory> opencvmlinclude

<Opencv installation directory> opencvotherlibshighgui

<Opencv installation directory> opencvotherlibscvcaminclude

<Opencv installation directory> opencvmicrosoft platform sdkinclude

6. Click OK to save the configuration.

3. Set the Visual C ++ 2005 project

After creating a project, you need to set the Link Library required by the project. Select the "Project | properties" menu. the dialog box shown in 2 is displayed. In the left-side list box, select "configuration properties" | linker) | input, select "additional dependencies (additional dependencies)" in the list box on the right, and enter "cxcore. lib cv. lib ml. lib highgui. lib cvaux. lib and other required libraries (separated by spaces ).

2. Development of face detection programs

Face detection is a very advanced problem. It is a major direction of Face research. Its task is to search for a given image using certain strategies, to determine whether it contains a face, how many faces are contained, and the position, size, and posture of the contained face. There are many face detection algorithms. Adaboost is one of them. Although the Adaboost algorithm is very complex, it is very easy to use functions and classifiers provided by OpenCV for face detection.

1. Use OpenCV for Face Detection

The facial detection program provides three functions: loading classifiers, loading images to be detected, and detecting and marking. This program uses the target detection class of the "haarcascade_frontalface_alt.xml" file stored in OpenCV. After loading it using the cvLoad function, the program performs forced type conversion. The cvHaarDetectObjects function provided in OpenCV is used to detect objects in an image. This function uses pointers to a target object (such as a face) the trained cascading classifier finds the rectangular area of the target object in the image and returns these areas as a sequence of rectangular boxes. The classifiercascade must be explicitly released after being used. The function used is cvReleaseHaarClassifierCascade. For the prototype of these functions, see the OpenCV manual.

2. Program Implementation

1) create a Visual C ++ MFC project named "FaceDetection" and set the application type to "single document ". Remove unnecessary items from the menu and add a "Face Detection" with the ID "ID_FaceDetected" and generate the message ing function for this menu item.

2) Add the following program code in the "FaceDetectionView. h" header file:

// Jiang linsheng, Nanjing forest Public Security College // facedetectionview. h: cfacedetectionview class interface # pragma once # include "CV. H "# include" highgui. H "class cfacedetectionview: Public cview {protected: // create cfacedetectionview (); declare_dyncreate (cfacedetectionview) only from serialization // attribute public: Comment * getdocument () const; Comment * cascade; // feature classification cvmemstorage * storage; void detect_and_draw (iplimage * IMG); iplimage * SRC; // loaded image

 

3) Add the following program code to the "facedetectionview. cpp" file:

// Facedetectionview. CPP: Implementation of the cfacedetectionview class # include "stdafx. H "# include" facedetection. H "# include" facedetectiondoc. H "# include" facedetectionview. H "# include <string> # ifdef _ debug # define new debug_new # endif // values (cfacedetectionview, cview) values (cfacedetectionview, cview) on_command (values, & cfacedetectionview :: onfacedetected) end_message_ma P () // cfacedetectionview const char * cascade_name = "regular"; // classifier name // cfacedetectionview void cfacedetectionview: onfacedetected () // RESPONSE event in the face detection menu {// todo: add the command processing code cstring filename here; // open the file dialog window cfiledialog opendlg (true, null, null, ofn_hidereadonly | ofn_overwriteprompt | ofn_nochangedir, l "image file format. JPG file format (*. JPG) | *. JPG | (*. BMP) | *. BMP | ", null); // The dialog from the file. Open the image if (opendlg. domodal () in the window ()! = Idok) return; // get the file name filename = opendlg. getpathname (); // necessary type conversion STD: String tempname = (lpcstr) cstringa (filename); const char * TMP = tempname. c_str (); // open the file. If the file fails, if (src = cvloadimage (TMP, cv_load_image_anycolor) = 0) return; // load (classifier Cascade) training database cascade = (cvhaarclassifiercascade *) cvload (cascade_name, 0, 0, 0); // if the load fails, an error message is displayed and exit if (cascade) {storage = cvcreatemstorage (0); cvnamedwindow ("Face Detection", cv_window_autosize); // create a window // analyze and display the result if the image exists; otherwise, exit the program if (SRC) detect_and_draw (SRC ); // call the cvreleaseimage (& SRC); cvreleasemstorage (& storage) ;}else {&, nbsp; afxmessagebox (L "cannot load classifier. Please confirm and try again! ");} Response (& cascade);} void cfacedetectionview: detect_and_draw (iplimage * IMG) // face detection and marking event {static cvscalar color [] = {0, 0, 255}; // used to set the face color double scale = 1.3; iplimage * gray = cvcreateimage (cvsize (IMG-> width, IMG-> height), 8, 1); iplimage * small_img = cvcreateimage (cvsize (cvround (IMG-> width/scale), cvround (IMG-> height/scale), 8, 1); int I; cvcvtcolor (IMG, gray, Cv_bgr2gray); cvresize (Gray, small_img, cv_inter_linear); cvequalizehist (small_img, small_img); cvclearmemstorage (storage); If (cascade) {// face detection cvseq * faces = cvhaardetectobjects (small_img, cascade, storage, 1.1, 2, 0, cvsize (30, 30); for (I = 0; I <(faces? Faces-> total: 0); I ++) {cvrect * r = (cvrect *) cvgetseqelem (faces, I); cvpoint center; int radius; center. X = cvround (R-> X + R-> width * 0.5) * scale); Center. y = cvround (R-> Y + R-> height * 0.5) * scale); radius = cvround (R-> width + R-> height) * 0.25 * scale); cvcircle (IMG, center, radius, color [0], 3, 8, 0) ;}} cvshowimage ("Face Detection", IMG ); cvreleaseimage (& gray); cvreleaseimage (& small_img );}

Note that the classifier file should be placed in the program directory when the program runs. If the generated EXE file is run, the classifier file and the EXE file should be placed in the same directory.

3. program running result

Run the program and select the Face Detection menu. In the pop-up dialog box, select the image file to be detected, and the program will mark the detected face in a circle, as shown in 3. This program can successfully detect most faces. However, due to illumination, occlusion, and skew, some faces cannot be correctly detected. In addition, some non-faces have certain facial features, it is also regarded as a face. These are the parts that need to be improved in this program.

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.