블로그 이미지
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-30 04:52

Recent Post

Recent Comment

Recent Trackback

Archive

2015. 4. 16. 17:22 Computer Vision/OpenCV



개발환경


사용툴       : Visual Studio 2013

라이브러리 : openCV library 2.4.10

프로젝트    : Visual C++ console application

개발날짜    : 2015-04-16


출처 및 참고 : OpenCV Tutorial C++

                     OpenCV Document - Eroding and Dilating





void FaceDetection::SkinColor_FaceDetection(const char* filename)

{

        CvCapture* capture;

 

        capture = cvCaptureFromFile(filename);

 

        assert(capture != NULL);

 

        IplImage* bgr_frame = cvQueryFrame(capture);

 

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

                             (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT));

 

        cvNamedWindow("CV_Window", 0);

        cvNamedWindow("CV_Window_th", 0);

        cvResizeWindow("CV_Window", 2048, 1024);

        cvResizeWindow("CV_Window_th", 2048, 1024);

 

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

 

        int iLowH = 170;

        int iHighH = 179;

 

        int iLowS = 150;

        int iHighS = 255;

 

        int iLowV = 60;

        int iHighV = 255;

 

        Mat imgTmp;

        imgTmp = cvarrToMat(bgr_frame);

 

        //Create a black image with the size as the camera output

        Mat imgLines = Mat::zeros(imgTmp.size(), CV_8UC3);;

 

        namedWindow("Control", 0); //create a window called "Control"

        cvResizeWindow("Control", 400, 300);

 

        //Create trackbars in "Control" window

        createTrackbar("LowH", "Control", &iLowH, 179); //Hue (0 - 179)

        createTrackbar("HighH", "Control", &iHighH, 179);

 

        createTrackbar("LowS", "Control", &iLowS, 255); //Saturation (0 - 255)

        createTrackbar("HighS", "Control", &iHighS, 255);

 

        createTrackbar("LowV", "Control", &iLowV, 255);//Value (0 - 255)

        createTrackbar("HighV", "Control", &iHighV, 255);

 

        int iLastX = -1;

        int iLastY = -1;

       

        //while (true)

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

        {

               Mat imgOriginal;

               Mat imgHSV;

               Mat imgThresholded;

 

               imgOriginal = cvarrToMat(bgr_frame); // read a new frame from video

              

               cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV


               //Threshold the image

               inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); 

 

               //morphological opening (remove small objects from the foreground)

               erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));

               dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));

 

               //morphological closing (fill small holes in the foreground)

               dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));

               erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));

 

               imshow("CV_Window_th", imgThresholded); //show the thresholded image

               imshow("CV_Window", imgOriginal); //show the original image

 

               //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop

               if (waitKey(30) == 27) 

               {

                       cout << "esc key is pressed by user" << endl;

                       break;

               }

        }

}





결과화면


HSV 영역을 좀 넓게 잡았더니 피부색 뿐만 아니라 비슷한 영역도 검출함.

control window에서 좀더 조절하면 오류범위를 좁힐 수 있을 듯 함.



posted by Kanais