人脸识别系统

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include

#include

#include

#include

#include

using namespace std;

using namespace cv;

int main(int argc, const char** argv)

{

//create the cascade classifier object used for the face detection

CascadeClassifier face_cascade;

//use the haarcascade_frontalface_alt.xml library

face_cascade.load("haarcascade_frontalface_alt2.xml");

const char* imagename = "test3.jpeg";

//从文件中读入图像

Mat img = imread(imagename);

//如果读入图像失败

if (img.empty())

{

fprintf(stderr, "Can not load image %s\n", imagename);

return -1;

}

//setup image files used in the process

Mat grayscaleFrame;

//convert captured image to gray scale and equalize

cvtColor(img, grayscaleFrame, CV_BGR2GRAY);

equalizeHist(grayscaleFrame, grayscaleFrame);

//create a vector array to store the face found

std::vector faces;

//find faces and store them in the vector array

face_cascade.detectMultiScale(grayscaleFrame, faces, 1.1, 3, CV_HAAR_FIND_BIGGEST_OBJECT | CV_HAAR_SCALE_IMAGE, Size(30, 30));

//draw a rectangle for all found faces in the vector array on the original image

for (int i = 0; i < faces.size(); i++)

{

Point pt1(faces[i].x + faces[i].width, faces[i].y + faces[i].height);

Point pt2(faces[i].x, faces[i].y);

rectangle(img, pt1, pt2, cvScalar(0, 255, 0, 0), 3, 8, 0);

}

//print the output

imshow("Face Dection, Results", img);

//pause for 33ms

waitKey();

return 0;

}

请注意:成功运行此段代码需要做一些准备工作,比如要将“haarcascade_frontalface_alt2.xml”文件放到工程目录下,而这个文件可以在”C:\OPENCV3.0\opencv\sources\data\haarcascades”找到。

#include

#include

#include

#include

using namespace std;

const char *pcascadeName = "D:\\OpenCV\\opencv\\data\\haarcascades\\haarcascade_frontalface_alt.xml";

const char *pImageName = "image.jpg";

void DetectAndMark();

int main(int argc, const char** argv)

{

DetectAndMark();

return 0;

}

void DetectAndMark()

{

// load the Haar classifier

CvHaarClassifierCascade *pHaarClassCascade;

pHaarClassCascade = (CvHaarClassifierCascade*)cvLoad(pcascadeName);

//load the test image

IplImage *pSrcImage = cvLoadImage(pImageName, CV_LOAD_IMAGE_UNCHANGED);

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

if(pSrcImage == NULL || pGrayImage == NULL)

{

printf("can't load image!\n");

return;

}

cvCvtColor(pSrcImage, pGrayImage, CV_BGR2GRAY);

if (pHaarClassCascade != NULL && pSrcImage != NULL && pGrayImage != NULL)

{

const static CvScalar colors[] =

{

CV_RGB(0,0,255),

CV_RGB(0,128,255),

CV_RGB(0,255,255),

CV_RGB(0,255,0),

CV_RGB(255,128,0),

CV_RGB(255,255,0),

CV_RGB(255,0,0),

相关文档
最新文档