블로그 이미지
Kanais
Researcher & Developer 퍼즐을 완성하려면 퍼즐 조각들을 하나 둘씩 맞춰나가야 한다. 인생의 퍼즐 조각들을 하나 둘씩 맞춰나가다 보면 인생이란 퍼즐도 완성되는 날이 오려나...?

calendar

1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

Notice

04-27 12:00

Recent Post

Recent Comment

Recent Trackback

Archive

2015. 4. 8. 10:02 Computer Vision/Face Detection




개발환경


사용툴       : Visual Studio 2013

라이브러리 : openCV library 2.4.11

프로젝트    : Visual C++ console application

개발날짜    : 2015-04-08



출처 : stackoverflow  - opencv and C++:detect face in image [closed]





int FaceDetection::Haar_FaceDetection_Img(string filename)

{

             // Global variables

             // Copy this file from opencv/data/haarscascades to target folder

             string face_cascade_name = "haarcascade_frontalface_alt.xml";

             CascadeClassifier face_cascade;

             string window_name = "Capture - Face detection";

             // Function main

            

                           // Load the cascade

                           if (!face_cascade.load(face_cascade_name))

                           {

                                        printf("--(!)Error loading\n");

                                        return (-1);

                           };

 

                           // Read the image file

                           Mat frame = imread(filename);

 

                           for (int i = 0; i < 1; i++)

                           {

                                        // Apply the classifier to the frame

                                        if (!frame.empty())

                                        {

                                                     detectAndDisplay(frame, face_cascade, filename);

                                        }

                                        else

                                        {

                                                     printf(" --(!) No captured frame -- Break!");

                                                     break;

                                        }

 

                                        int c = waitKey(10);

 

                                        if (27 == char(c))

                                        {

                                                     break;

                                        }

                           }

                           return 0;

}

// Function detectAndDisplay

void FaceDetection::detectAndDisplay(Mat frame, CascadeClassifier face_cascade, string filename)

{

             std::vector<Rect> faces;

             Mat frame_gray;

             Mat res;

             Mat gray;

             int filenumber = 0; // Number of file to be saved

 

             cvtColor(frame, frame_gray, COLOR_BGR2GRAY);

             equalizeHist(frame_gray, frame_gray);

 

             // Detect faces

             face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));

 

             // Set Region of Interest

             cv::Rect roi_b;

             cv::Rect roi_c;

 

             size_t ic = 0; // ic is index of current element

             int ac = 0; // ac is area of current element

 

             size_t ib = 0; // ib is index of biggest element

             int ab = 0; // ab is area of biggest element

 

             for (ic = 0; ic < faces.size(); ic++) // Iterate through all current elements (detected faces)

 

             {

                   roi_c.x = faces[ic].x;

                   roi_c.y = faces[ic].y;

                   roi_c.width = (faces[ic].width);

                   roi_c.height = (faces[ic].height);

 

                   ac = roi_c.width * roi_c.height; // Get the area of current element (detected face)

 

                   roi_b.x = faces[ib].x;

                   roi_b.y = faces[ib].y;

                   roi_b.width = (faces[ib].width);

                   roi_b.height = (faces[ib].height);

 

                   // Get the area of biggest element, at beginning it is same as "current" element

                   ab = roi_b.width * roi_b.height; 

 

                   if (ac > ab)

                   {

                          ib = ic;

                          roi_b.x = faces[ib].x;

                          roi_b.y = faces[ib].y;

                          roi_b.width = (faces[ib].width);

                          roi_b.height = (faces[ib].height);

                   }

 

                   Point pt1(faces[ic].x, faces[ic].y); // Display detected faces on main window - live stream from camera

                   Point pt2((faces[ic].x + faces[ic].height), (faces[ic].y + faces[ic].width));

                   rectangle(frame, pt1, pt2, Scalar(0, 255, 0), 2, 8, 0);

             }

 

             // Show image

             imshow("original", frame);

} 



결과화면






posted by Kanais