码迷,mamicode.com
首页 > 其他好文 > 详细

opencv 利用Haar 人脸识别

时间:2015-01-11 16:06:19      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

 

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

    // 载入图像
    const char *pstrImageName = "D:\\721.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;
}

 

opencv 利用Haar 人脸识别

标签:

原文地址:http://www.cnblogs.com/qscqesze/p/4216560.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!