www.pudn.com > faces.rar > cvfinder.c


/*** 
 **     libface - Library of face recognition and supporting algorithms 
        Copyright (c) 2003 Stefan Farthofer 
 
	This file is part of libface, which is 
 
        free software; you can redistribute it and/or modify 
        it under the terms of the GNU General Public License as published by 
        the Free Software Foundation; either version 2 of the License, or 
        (at your option) any later version. 
 
        This program is distributed in the hope that it will be useful, 
        but WITHOUT ANY WARRANTY; without even the implied warranty of 
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
        GNU General Public License for more details. 
 
        You should have received a copy of the GNU General Public License 
        along with this program; if not, write to the Free Software 
        Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 
	For further information seek us at http://sourceforge.net/projects/openbio/ 
**	or write an email to dimitri.pissarenko@gmx.net or farthofer@chello.at. 
***/ 
 
#include  
#include  
#include  
#include  
#include "frbase.h" 
#include "fr.h" 
 
//this is the original training size of the used cascade, just 
//copied from the examples 
#define ORIG_WIN_SIZE 24 
 
int cvFindFaces(FRimage* image, FRbox** boxes, unsigned int* nrBoxes) { 
	IplImage* iplImg; 
	CvMemStorage* storage; 
	CvHidHaarClassifierCascade* hid_cascade = NULL; 
	CvSize size; 
	CvRect* r; 
	CvSeq* faces; 
	unsigned int i; 
	ILuint img; 
	unsigned char *imgdata; 
 
	//load the default face cascade 
    CvHaarClassifierCascade* cascade = cvLoadHaarClassifierCascade( 
        "", 
        cvSize( ORIG_WIN_SIZE, ORIG_WIN_SIZE )); 
    if( !cascade ) 
		return FR_ERR_OTHER; 
	//create an optimized version and release the plain cascade, this 
	//could be cached for a performance boost 
    hid_cascade = cvCreateHidHaarClassifierCascade( cascade, 0,0,0,1); 
    cvReleaseHaarClassifierCascade( &cascade ); 
 
	//create a storage area for the detection function 
	storage = cvCreateMemStorage(0); 
 
	//now create an ipl image 
	size.height = image->height; 
	size.width = image->width; 
	iplImg = cvCreateImage(size,IPL_DEPTH_8U, 1); 
 
	//convert back to 8bit channel data and get pointer to data 
	frImgGetILImg(&img, image); 
	imgdata = ilGetData(); 
	 
	//fill image with data 
	for (i = 0; i < image->height; i++) { 
		memcpy(iplImg->imageData + (iplImg->widthStep * i),imgdata + i*image->width, image->width); 
	} 
 
	ilDeleteImages(1, &img); 
 
	//now do the detection (copy code from sample) 
 
        faces = cvHaarDetectObjects( iplImg, hid_cascade, storage, 
                                            1.1, 2, CV_HAAR_DO_CANNY_PRUNING ); 
		*nrBoxes=faces->total; 
		if (faces->total>0) 
		{ 
			*boxes = (FRbox*)malloc(sizeof(FRbox)*faces->total); 
			for( i = 0; i < (faces ? faces->total : (unsigned int)0); i++ ) 
			{ 
				r = (CvRect*)cvGetSeqElem( faces, i, 0 ); 
 
				(*boxes)[i].x1=(float)r->x; 
				(*boxes)[i].y1=(float)r->y; 
				(*boxes)[i].x2=(float)r->x; 
				(*boxes)[i].y2=(float)(r->y+r->height); 
				(*boxes)[i].x3=(float)(r->x+r->width); 
				(*boxes)[i].y3=(float)(r->y+r->height); 
				(*boxes)[i].x4=(float)(r->x+r->width); 
				(*boxes)[i].y4=(float)r->y; 
			} 
		} 
 
 
	cvReleaseImage(&iplImg); 
	cvReleaseMemStorage(&storage); 
	 
	return FR_OK; 
}