opencv人脸检测的helloworld

注意两点

1.图片

2.haarcascade_frontalface_alt.xml在opencv的安装路径

参考

http://blog.csdn.net/morewindows/article/details/8426318

#include <opencv2/opencv.hpp>  
#include <cstdio>  
#include <cstdlib>  
#include <Windows.h>  
using namespace std;
int main()
{
	// 加载Haar特征检测分类器  
	// haarcascade_frontalface_alt.xml系OpenCV自带的分类器 下面是我机器上的文件路径  
	const char *pstrCascadeFileName = "E:/tools/opencv2410/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml";
	CvHaarClassifierCascade *pHaarCascade = NULL;
	pHaarCascade = (CvHaarClassifierCascade*)cvLoad(pstrCascadeFileName);

	// 载入图像  
	const char *pstrImageName = "13.jpg";
	IplImage *pSrcImage = cvLoadImage(pstrImageName, CV_LOAD_IMAGE_UNCHANGED);

	IplImage *pGrayImage = cvCreateImage(cvGetSize(pSrcImage), IPL_DEPTH_8U, 1);
	cvCvtColor(pSrcImage, pGrayImage, CV_BGR2GRAY);

	// 人脸识别与标记  
	if (pHaarCascade != NULL)
	{
		CvScalar FaceCirclecolors[] =
		{
			{ { 0, 0, 255 } },
			{ { 0, 128, 255 } },
			{ { 0, 255, 255 } },
			{ { 0, 255, 0 } },
			{ { 255, 128, 0 } },
			{ { 255, 255, 0 } },
			{ { 255, 0, 0 } },
			{ { 255, 0, 255 } }
		};

		CvMemStorage *pcvMStorage = cvCreateMemStorage(0);
		cvClearMemStorage(pcvMStorage);
		// 识别  
		DWORD dwTimeBegin, dwTimeEnd;
		dwTimeBegin = GetTickCount();
		CvSeq *pcvSeqFaces = cvHaarDetectObjects(pGrayImage, pHaarCascade, pcvMStorage);
		dwTimeEnd = GetTickCount();

		printf("人脸个数: %d   识别用时: %d ms\n", pcvSeqFaces->total, dwTimeEnd - dwTimeBegin);

		// 标记  
		for (int i = 0; i <pcvSeqFaces->total; i++)
		{
			CvRect* r = (CvRect*)cvGetSeqElem(pcvSeqFaces, i);
			CvPoint center;
			int radius;
			center.x = cvRound((r->x + r->width * 0.5));
			center.y = cvRound((r->y + r->height * 0.5));
			radius = cvRound((r->width + r->height) * 0.25);
			cvCircle(pSrcImage, center, radius, FaceCirclecolors[i % 8], 2);
		}
		cvReleaseMemStorage(&pcvMStorage);
	}

	const char *pstrWindowsTitle = "人脸识别";
	cvNamedWindow(pstrWindowsTitle, CV_WINDOW_AUTOSIZE);
	cvShowImage(pstrWindowsTitle, pSrcImage);

	cvWaitKey(0);

	cvDestroyWindow(pstrWindowsTitle);
	cvReleaseImage(&pSrcImage);
	cvReleaseImage(&pGrayImage);
	return 0;
}

注意linkerinput要引入所有lib

opencv_core2410d.lib

opencv_imgproc2410d.lib

opencv_highgui2410d.lib

opencv_calib3d2410d.lib

opencv_contrib2410d.lib

opencv_features2d2410d.lib

opencv_flann2410d.lib

opencv_gpu2410d.lib

opencv_legacy2410d.lib

opencv_ml2410d.lib

opencv_nonfree2410d.lib

opencv_objdetect2410d.lib

opencv_ocl2410d.lib

opencv_photo2410d.lib

opencv_stitching2410d.lib

opencv_superres2410d.lib

opencv_ts2410d.lib

opencv_video2410d.lib

opencv_videostab2410d.lib

相关推荐