Click here to Skip to main content
15,894,825 members
Articles / Multimedia / GDI+

Face and Eyes Detection Using OpenCV

Rate me:
Please Sign up or sign in to vote.
3.58/5 (38 votes)
7 Aug 2008CPOL2 min read 977.6K   82.8K   193  
This article demonstrates how to perform human face and eyes detection on images using OpenCV in .NET
// facedetect.h : main header file for the FACEDETECT DLL
//
#pragma warning(disable : 4793)
#pragma once
#include <cv.h>
#pragma managed 

#define scale 1.3
#define MAXOBJECTCOUNT 5
#define MaxValue 32000

#define cascade_name "haarcascade_frontalface_alt.xml"
#pragma  


/////////////////////////////////////////////////////////////////////////////
// CFacedetectApp
// See facedetect.cpp for the implementation of this class
//
#pragma managed 
__gc class ObjectLocator 
{
private:
	int width,height;             //dimensions of the scene bitmap	  
	CvHaarClassifierCascade* cascade;
	CvMemStorage* storage ;		
	int object_location __gc[,];
	//int object_location __gc[MAXOBJECTCOUNT][4];  //stores the locations of detected objects	
	
	/* finds sequence element by its index */
	char* Helper( const CvSeq *seq, int index )
	{
		CvSeqBlock block;
		int count, total = seq->total;

		if( (unsigned)index >= (unsigned)total )
		{
			index += index < 0 ? total : 0;
			index -= index >= total ? total : 0;
			if( (unsigned)index >= (unsigned)total )
				return 0;
		}

		block = *seq->first;
		if( index + index <= total )
		{
			while( index >= (count = block.count) )
			{
				block = *block.next;
				index -= count;
			}
		}
		else
		{
			do
			{
				block = *block.prev;
				total -= block.count;
			}
			while( index < total );
			index -= total;
		}

		return block.data + index * seq->elem_size;
	}

public:		
	void SetFaceROI(int index);
	void UnSetFaceROI();

	void UnInitImageStorage();
	void InitImageStorage(int img_width, int img_height, double img_scale);
	
	void ImagePreProcessForFaceDetection();
		
	void ManagedImagetoUnManagedImage(IplImage* Src);
	//void PrepareSmallImage();		
	int NoOfObjects;            //number of faces detected	
	void GetObjectCoordinates(int oNum, int &tx, int &ty, int &bx, int &by);
	int DetectObjects();
	int InitObjectDetect(char* cascadename);	
	ObjectLocator();
	~ObjectLocator();
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Pakistan Pakistan
BCSE - Software Engineering (2000 - 2004)
Foundation University Institute of Management and Computer Sciences.
Pakistan.

MS - Computer Sciences (2004 - 2005)
Lahore Univeristy of Management Sciences
Pakistan.

Comments and Discussions