After learning theory a bit in part1 now we lear how we can detect face in an image. Later we will extend this to detecting face from web cam and avi file. The code is very simple. We will load a image of a person in IplImage structure. We also need to load haarcascader of frontal face will which enable us to detect the face in image. Opencv 2 provides many haar cascades of face, eye , nose etc which ever you want use. If you have installed openCV v2 in "C:/OpenCV2.0" ,you find it in "C:/OpenCV2.0/data/haarcascades/" Location. Now will need to call "cvHaarDetectObjects" OpenCV Function to detect the faces.
Here is the program -
#include "cv.h" #include "highgui.h" #include#include #ifdef _EiC #define WIN32 #endif static CvMemStorage* storage_face = 0; //Memory Storage to Sore faces static CvHaarClassifierCascade* cascade_face = 0; void detect_and_draw( IplImage* image ); //Haar cascade - if your openc CV is installed at location C:/OpenCV2.0/ const char* cascade_name_face ="C:/OpenCV2.0/data/haarcascades/haarcascade_frontalface_alt.xml"; ///////////////////////////////////////////////////////////////////////////////// int main() { IplImage *image =0; image = cvLoadImage("viv6v.jpg",1); if(!image) { printf("Error loading image\n"); return -1; } cascade_face = (CvHaarClassifierCascade*)cvLoad( cascade_name_face, 0, 0, 0 ); if( !cascade_face ) { printf("ERROR: Could not load classifier of face cascade\n" ); return -1; } storage_face = cvCreateMemStorage(0); cvNamedWindow( "result", 1 ); // Call function to detect and Draw rectagle around face detect_and_draw( image); // Wait for key event. cvWaitKey(0); // release resourses cvReleaseImage( &image ); cvReleaseHaarClassifierCascade(&cascade_face ); cvReleaseMemStorage( &storage_face); cvDestroyWindow("result"); return 0; } //////////////////////////// Function To detect face ////////////////////////// void detect_and_draw( IplImage* img ) { double scale = 2; // create a gray image for the input image IplImage* gray = cvCreateImage( cvSize(img->width,img->height), 8, 1 ); // Scale down the ie. make it small. This will increase the detection speed 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 ); // Equalise contrast by eqalizing histogram of image cvEqualizeHist( small_img, small_img ); cvClearMemStorage( storage_face); if( cascade_face ) { // Detect object defined in Haar cascade. IN our case it is face CvSeq* faces = cvHaarDetectObjects( small_img, cascade_face, storage_face, 1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/, cvSize(30, 30) ); // Draw a rectagle around all detected face for( i = 0; i < (faces ? faces->total : 0); i++ ) { CvRect r = *(CvRect*)cvGetSeqElem( faces, i ); cvRectangle( img, cvPoint(r.x*scale,r.y*scale),cvPoint((r.x+r.width)*scale,(r.y+r.height)*scale),CV_RGB(255,0,0),3,8,0 ); } } cvShowImage( "result", img ); cvReleaseImage( &gray ); cvReleaseImage( &small_img ); } /////////////////////////////////////////////////////////////////////////////////
Output - Image with multiple Faces -
Can I check with you for OpenCV up to how many faces can detect? Kindly email to lavenderziyi@hotmail.
ReplyDeletethank u for the tutorial.
ReplyDeleteI am new to MSVC++ and OpenCV, I have this error, from this code, could u guide me how i should fix it?
my email is, arifgulzar85@gmail.com
I missed the error. Here it is,
ReplyDelete1>FaceDetecct.obj : error LNK2019: unresolved external symbol _cvReleaseHaarClassifierCascade referenced in function _main
1>FaceDetecct.obj : error LNK2019: unresolved external symbol _cvHaarDetectObjects referenced in function "void __cdecl detect_and_draw(struct _IplImage *)" (?detect_and_draw@@YAXPAU_IplImage@@@Z)
1>c:\users\durat\documents\visual studio 2010\Projects\Facce\Debug\Facce.exe : fatal error LNK1120: 2 unresolved externals
Hello Ritesh Ranjan, your work is nice ,you get some different ideas so please visit online itutorial
ReplyDeletethanks for helping me in my project and how can i make my project report and what about report writeing help and Online tutoring, programming help, assignment help, homework help. you can get all on your table from online
ReplyDeletereally a great blog..
ReplyDeleteLearn Free ASP .NET
Yes, I think there is a large element of wishful thinking.
ReplyDeleteEnglish Test Center Pakistan
Very useful programming. Thank you for this sharing.
ReplyDeleteSAP BI
sap gold partner
SAP Implementation
SAP DBA Services
SAP Functional Module
SAP Solution
HANA Technology
good post
ReplyDeleteCruise Dinner in Dubai Marina
Hello
ReplyDeleteInternational Movers
barwa real estate company
ReplyDeleteFantastic blog! Do you have any tips and hints for aspiring writers? I’m planning to start my own website soon but I’m a little lost on everything Water Filter UAE
ReplyDelete