⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 facedetect.cpp

📁 Face Detection using OpenCV
💻 CPP
字号:
#pragma warning( disable : 4996 )
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>
#include "constants.h"

#ifdef _EiC
#define WIN32
#endif


static CvMemStorage* storage = 0;
static CvHaarClassifierCascade* cascade;

static CvSeq* detect_face(IplImage* image, double scale);

const char* cascade_name = "haarcascade_frontalface_default.xml";

int init_cascade(void)
{
	/*
	 * Reading the haar file from disk.
	 */
    cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
    if( !cascade )
    {
        fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
        fprintf( stderr,
        "Usage: facedetect --cascade=\"<cascade_path>\" [filename|camera_index]\n" );
        return -1;
    }
		
	/*
	 * Initialize storage in order to store the found faces.
	 */
    storage = cvCreateMemStorage(0);
		if(storage == NULL){
        fprintf( stderr, "ERROR: Could not create memory storage.\n" );
        return -1;
		}

	/*
	 * All went well.
	 */
	return 0;
}


CvSeq *analyse_frame(IplImage *frame, double scale)
{
	CvSeq *faces;  // Faces collection.

	/*
	 * Call the detect and draw routine.
	 */
	faces = detect_face(frame, scale);

	/*
	 * In the future the code to extract the detected faces here.
	 */
	return faces;
}


static CvSeq* detect_face(IplImage* img, double scale)
{
	//int i;
	double t; 		/* Timer value */

	/*
	 * Prepare the image and clean the storage.
	 */
	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 );
	cvCvtColor( img, gray, CV_BGR2GRAY );
	cvResize( gray, small_img, CV_INTER_LINEAR );
	cvEqualizeHist( small_img, small_img );
	cvClearMemStorage( storage );

	/*
	 * When a cascade is present do the detection.
	 */

	/*
	 * Do the actual detection using the haar file here.
	 */
	t = (double)cvGetTickCount();
	CvSeq* faces = cvHaarDetectObjects( small_img, cascade, storage,
			1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/,
			cvSize(30, 30) );
	t = (double)cvGetTickCount() - t;
	//printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );
	//printf( "Faces found = %d\n", faces->total );

//#if 0
	/*
	 * Draw ovals around the detected faces.
	 */
	for(int 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, cvScalar(0,0,255), 2, 8, 0 );
	}
//#endif

	/*
	 * Removed, because SDL is doing the showing of the image.
	 */
	cvReleaseImage( &gray );
	cvReleaseImage( &small_img );

	/*
	 * Return faces struct.
	 */
	return faces;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -