Created
June 21, 2011 00:25
-
-
Save ts-3156/1036954 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "StdAfx.h" | |
#include "FaceDetecter.h" | |
#include "opencv2/opencv.hpp" | |
FaceDetecter::FaceDetecter(void) | |
{ | |
} | |
FaceDetecter::~FaceDetecter(void) | |
{ | |
} | |
void FaceDetecter::detect(void) | |
{ | |
int i; | |
IplImage *src_img = 0, *src_gray = 0; | |
// const char *cascade_name = "data/haarcascade_frontalface_default.xml"; | |
// const char *cascade_name = "data/haarcascade_mcs_nose.xml"; | |
const char *cascade_name = "data/haarcascade_frontalface_alt2.xml"; | |
CvHaarClassifierCascade *cascade = 0; | |
CvMemStorage *storage = 0; | |
CvSeq *faces; | |
static CvScalar colors[] = { | |
{{0, 0, 255}}, {{0, 128, 255}}, | |
{{0, 255, 255}}, {{0, 255, 0}}, | |
{{255, 128, 0}}, {{255, 255, 0}}, | |
{{255, 0, 0}}, {{255, 0, 255}} | |
}; | |
// 画像を読み込む | |
src_img = cvLoadImage("images/purikura.jpg", CV_LOAD_IMAGE_COLOR); | |
if (src_img == NULL){ | |
printf("file is not found.\n"); | |
return; | |
} | |
src_gray = cvCreateImage (cvGetSize(src_img), IPL_DEPTH_8U, 1); | |
// ブーストされた分類器のカスケードを読み込む | |
cascade = (CvHaarClassifierCascade *) cvLoad(cascade_name, 0, 0, 0); | |
// メモリを確保し,読み込んだ画像のグレースケール化,ヒストグラムの均一化を行う | |
storage = cvCreateMemStorage (0); | |
cvClearMemStorage (storage); | |
cvCvtColor (src_img, src_gray, CV_BGR2GRAY); | |
cvEqualizeHist (src_gray, src_gray); | |
// 物体(顔)検出 | |
faces = cvHaarDetectObjects (src_gray, cascade, storage, 1.11, 4, 0, cvSize (40, 40)); | |
// 検出された全ての顔位置に,円を描画する | |
for (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); | |
center.y = cvRound (r->y + r->height * 0.5); | |
radius = cvRound ((r->width + r->height) * 0.25); | |
cvCircle (src_img, center, radius, colors[i % 8], 3, 8, 0); | |
} | |
// 画像を表示,キーが押されたときに終了 | |
cvNamedWindow ("Face Detection", CV_WINDOW_AUTOSIZE); | |
cvShowImage ("Face Detection", src_img); | |
cvWaitKey (0); | |
cvDestroyWindow ("Face Detection"); | |
cvReleaseImage (&src_img); | |
cvReleaseImage (&src_gray); | |
cvReleaseMemStorage (&storage); | |
} | |
// ↓↓↓実際は別のファイル | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
FaceDetecter faceDetecter(); | |
faceDetecter.detect(); // ← 「Error:式にはクラス型が必要です」とかいうエラー | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment