블로그 이미지
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 31

Notice

05-12 19:33

Recent Post

Recent Comment

Recent Trackback

Archive

2015. 4. 9. 11:34 Computer Vision/Face Detection



개발환경


사용툴       : Visual Studio 2013

라이브러리 : openCV library 2.4.11

프로젝트    : Visual C++ console application

개발날짜    : 2015-04-09



출처 : Tilltue의 공부방


동영상 얼굴인식 부분http://kanais2.tistory.com/5


cvGet2D 함수 설명 참고 : http://everyone-has-a-blog.tistory.com/92

cvRectangle 함수 설명 참고 : 아몰라님 티스토리

OPENCV Library



Tilltue님의 소스에서 부분적으로 수정 및 추가함.




void FaceDetection::Mosaic_process(IplImage* image, Rect face)

{

     int nCount = 0;

     int mb = 9;

     int w_point = 0, h_point = 0;

     int y_startpoint = 0, x_startpoint = 0;

     double R = 0, G= 0, B = 0;

 

     for (int i = 0 ; i < face.height / mb ; i++) {

             for (int j = 0 ; j < face.width / mb ; j++) {

                   nCount = 0;

                   B = 0; G = 0; R = 0;

 

                    x_startpoint = face.x + (j * mb);

                    y_startpoint = face.y + (i * mb);

                    for (int mb_y = y_startpoint ; mb_y < y_startpoint + mb; mb_y++) {

                          for (int mb_x = x_startpoint ; mb_x < x_startpoint + mb; mb_x++) {

                               CvScalar color;

                                w_point = mb_x;

                                h_point = mb_y;

 

                                if (mb_x >= image->width) {

                                     w_point = image->width - 1;

                                 }

                                 if (mb_y >= image->height){

                                     h_point = image->height - 1;

                                  }

                                  color = cvGet2D(image, h_point, w_point);

                                  B += color.val[0];

                                  G += color.val[1];

                                  R += color.val[2];

                                  nCount++;

                             }

                     }

 

                     //평균을구함

                     B /= nCount;

                     G /= nCount;

                     R /= nCount;

                     CvScalar color;

                     color.val[0] = B;

                     color.val[1] = G;

                     color.val[2] = R;

                     //cvRectangle(image, cvPoint(x_startpoint, y_startpoint), cvPoint(x_startpoint + mb, y_startpoint + mb), cvScalar(0, 0, 0, 0), 1, 8, 0);

                     cvRectangle(image, cvPoint(x_startpoint, y_startpoint), cvPoint(x_startpoint + mb, y_startpoint + mb), color, CV_FILLED, 8, 0);

        }

    }

}





결과화면


- 녹색 테두리되어있는 것만 얼굴인식 후 모자이크 처리된 것임.

- 나머지 모자이크 처리는 제대로 얼굴인식이 되지 않아 임의로 수정한 것.

- 기본 Haar Cascade Classifier 를 사용했다보니 옆얼굴이나 기운얼굴이나 안경낀 얼굴은제대로 인식하지 못함.



posted by Kanais
2015. 4. 8. 11:30 Computer Vision/Face Detection


개발환경


사용툴       : Visual Studio 2013

라이브러리 : openCV library 2.4.11

프로젝트    : Visual C++ console application

개발날짜    : 2015-04-08



참고 : stackoverflow -OpenCV saving video to file




void FaceDetection::Haar_FaceDetection(const char* filename)

{

     int cnt = 0;

     //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_alt.xml");

 

     CvCapture* capture;

 

     capture = cvCaptureFromFile(filename);

     //capture = cvCaptureFromFile("face.jpg");

 

     assert(capture != NULL);

 

     IplImage* bgr_frame;

 

   CvSize size = cvSize((int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH), 

                                 (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT));

 

     //cvNamedWindow("Webcam", CV_WINDOW_AUTOSIZE);

 

  CvVideoWriter *writer = cvCreateVideoWriter("vp_01003.avi", CV_FOURCC('D', 'I', 'V', 'X'), 15, size);

 

     while ((bgr_frame = cvQueryFrame(capture)) != NULL)

     {

           ////////////////////////////////////////////////////////////////////////////////////////////

           // face detection and process  

           ////////////////////////////////////////////////////////////////////////////////////////////

            cnt++;

            //if (cnt == 100) { break; }

 

            Mat f2;

            Mat frame;

            //waitKey(10);

            frame = cvarrToMat(bgr_frame);

            cvtColor(frame, f2, CV_BGR2GRAY);

 

            equalizeHist(f2, f2);

 

            std::vector<Rect> faces;

   face_cascade.detectMultiScale(f2, faces, 1.1, 2, CV_HAAR_SCALE_IMAGE | CV_HAAR_DO_CANNY_PRUNING, Size(0, 0), Size(100, 100));

 

           //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(frame, pt1, pt2, cvScalar(0, 255, 0, 0), 2, 8, 0);

                 printf("face detection! size : %d - %d\n", faces.size(), i);

             }

             /////////////////////////////////////////////////////////////////////////////////////////

 

             cvWriteFrame(writer, bgr_frame);

             cvShowImage("CV_Window", bgr_frame);

             char c = cvWaitKey(1);

             if (c == 27) break;

             printf("A frame processing : %d\n", cnt);

         }

 

        cvReleaseVideoWriter(&writer);

        cvReleaseCapture(&capture);

        cvDestroyWindow("CV_Window");

}




결과화면



posted by Kanais
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