개발환경
사용툴 : 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에서 좀더 조절하면 오류범위를 좁힐 수 있을 듯 함.
'Computer Vision > OpenCV' 카테고리의 다른 글
[OpenCV] Image Resize (0) | 2015.04.21 |
---|---|
[OpenCV] 자주쓰는 기능들 모음 (2) | 2015.04.20 |
[OpenCV] Mat 데이터 픽셀에 접근하는 방법 (0) | 2015.04.17 |
[OpenCV] IplImage 데이터 구조에서 픽셀값에 접근하기 (0) | 2015.04.17 |
[openCV] the library is compiled without gpu support (0) | 2015.04.15 |