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

📄 violajones.cpp

📁 这是个人脸识别程序
💻 CPP
字号:
// $violajones.cpp 1.5 milbo$ interface to OpenCV Viola Jones face detector.//// TODO We re-open the Img in fViolaJonesFindFace, rather than using the// passed-in Img parameter.  This wastes a bit of time and could  be fixed.//-----------------------------------------------------------------------------// This program 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.//// A copy of the GNU General Public License is available at// http://www.r-project.org/Licenses///-----------------------------------------------------------------------------#include "all.hpp"#include "cv.h"#include "highgui.h"// accurate but slow: these values are recommended in opencvref_cv.htmstatic const double SCALE_FACTOR   = 1.1;static const int    MIN_NEIGHBORS  = 3;static const int    DETECTOR_FLAGS = 0;static const char *CASCADE_NAME = "haarcascade_frontalface_alt2.xml";static CvHaarClassifierCascade *pgCascade;static CvMemStorage *pgStorage;//-----------------------------------------------------------------------------// specifying a min face width helps reduce nbr of false positivesstatic int nMinFaceWidth (int ImageWidth){return ImageWidth / 4;}//-----------------------------------------------------------------------------void CloseViolaJones (void){if (pgCascade)    {    cvReleaseHaarClassifierCascade(&pgCascade);    cvReleaseMemStorage(&pgStorage);    pgCascade = NULL;    }}//-----------------------------------------------------------------------------static void OpenViolaJones (const char sDataDir[]){char sCascadePath[SLEN];sprintf(sCascadePath, "%s/%s", sDataDir, CASCADE_NAME);pgCascade = (CvHaarClassifierCascade*)cvLoad(sCascadePath, 0, 0, 0);if(!pgCascade)    Err("Can't load %s", sCascadePath);pgStorage = cvCreateMemStorage(0);if (!pgStorage)    Err("Out of memory (cvCreateMemStorage failed)");}//-----------------------------------------------------------------------------// Returns with face position in Shape.// Face is a "ViolaJones global detector shape", with NBR_VIOLA_JONES_POINTS points.// See DETECTOR_ defs in landmarks.hpp for point names.// If it can't find a face (with two eyes), we exit with an error message.bool fViolaJonesFindFace(SHAPE &Shape,       // out        Image &Img,                         // in: TODO not actually used        const char sImageFile[],            // in        const char sDataDir[])              // in{if (!pgCascade) // first time?    OpenViolaJones(sDataDir);cvClearMemStorage(pgStorage);Shape.dimClear(NBR_VIOLA_JONES_POINTS, 2);IplImage* pImage = cvLoadImage(sImageFile, 1);if (!pImage)    Err("Can't load %s", sImageFile);IplImage* pWork = cvCreateImage(cvSize(pImage->width, pImage->height), 8, 1);cvCvtColor(pImage, pWork, CV_BGR2GRAY);cvResize(pWork, pWork, CV_INTER_LINEAR);cvEqualizeHist(pWork, pWork);CvSeq* pFaces = cvHaarDetectObjects(pWork, pgCascade, pgStorage,                    SCALE_FACTOR, MIN_NEIGHBORS, DETECTOR_FLAGS,                    cvSize(nMinFaceWidth(pImage->width), nMinFaceWidth(pImage->width)));cvReleaseImage(&pWork);if (pFaces == NULL)     // should never happen    {    cvReleaseImage(&pImage);    Err("cvHaarDetectObjects failed");    }int iSelectedFace = 0;// get most central facedouble MaxOffset = 1e10;    // max abs dist from center of face to center of imagefor (int iFace = 0; iFace < pFaces->total; iFace++)    {    CvRect* r = (CvRect*)cvGetSeqElem(pFaces, iFace);    double Offset = ABS(r->x + r->width/2.0 - pImage->width/2.0);    if (Offset < MaxOffset)        {        MaxOffset = Offset;        iSelectedFace = iFace;        }    }if (pFaces->total < 1)	{    lprintf("\nViola Jones detector found no faces in %s\n", sImageFile);	return false;	}// Write the global detector shape into Shape.// We must convert the Viola Jones shape coords to our internal shape coords.CvRect* r = (CvRect*)cvGetSeqElem(pFaces, iSelectedFace);Shape(DETECTOR_TopLeft, VX)  = r->x - pImage->width/2.0;Shape(DETECTOR_TopLeft, VY)  = pImage->height/2.0 - r->y;Shape(DETECTOR_BotRight, VX) = Shape(DETECTOR_TopLeft, VX) + r->width;Shape(DETECTOR_BotRight, VY) = Shape(DETECTOR_TopLeft, VY) - r->height;cvReleaseImage(&pImage);return true;}

⌨️ 快捷键说明

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