Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV Skin Detection

I've been doing some skin detection but can't get a smooth one. The image below contains the input (left) and output (right) using the code also attached below. Now, the desired output should have been the bottom most image (the one that is smooth on the edges and doesn't have holes within). How do I achieve this output? A sample code on where to start would be of great help.

Input (left) and Incorrect output (right):

enter image description here

Desired output:

enter image description here

Code to generate the Incorect output:

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

int main(){
    Mat src = imread("qq.jpg");
    if (src.empty())
        return -1;
    blur( src, src, Size(3,3) );
    Mat hsv;
    cvtColor(src, hsv, CV_BGR2HSV);
    Mat bw;
    inRange(hsv, Scalar(0, 10, 60), Scalar(20, 150, 255), bw);
    imshow("src", src);
    imshow("dst", bw);
    waitKey(0);
    return 0;
}

Modified Code (after Astor's suggestion): (the problem now is: how do you smoothen the output?)

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

int findBiggestContour(vector<vector<Point> >);

int main(){
    Mat src = imread("qq.jpg");
    if (src.empty())
        return -1;
    blur( src, src, Size(3,3) );

    Mat hsv;
    cvtColor(src, hsv, CV_BGR2HSV);

    Mat bw;
    inRange(hsv, Scalar(0, 10, 60), Scalar(20, 150, 255), bw);
    imshow("src", src);
    imshow("dst", bw);

    Mat canny_output;
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    findContours( bw, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
    int s = findBiggestContour(contours);

    Mat drawing = Mat::zeros( src.size(), CV_8UC1 );
    drawContours( drawing, contours, s, Scalar(255), -1, 8, hierarchy, 0, Point() );

    imshow("drw", drawing);
    waitKey(0);
    return 0;
}

int findBiggestContour(vector<vector<Point> > contours){
    int indexOfBiggestContour = -1;
    int sizeOfBiggestContour = 0;
    for (int i = 0; i < contours.size(); i++){
        if(contours[i].size() > sizeOfBiggestContour){
            sizeOfBiggestContour = contours[i].size();
            indexOfBiggestContour = i;
        }
    }
    return indexOfBiggestContour;
}
like image 620
Og Namdik Avatar asked Oct 19 '12 06:10

Og Namdik


People also ask

What can OpenCV detect?

OpenCV has a bunch of pre-trained classifiers that can be used to identify objects such as trees, number plates, faces, eyes, etc. We can use any of these classifiers to detect the object as per our need.

What is skin detection?

Skin detection is the process of finding skin-colored pixels and regions in an image or a video. This process is typically used as a preprocessing step to find regions that potentially have human faces and limbs in images [2].

Is OpenCV best for image processing?

OpenCV – Open Source Computer Vision. It is one of the most widely used tools for computer vision and image processing tasks.


2 Answers

You should use findContours to detect the biggest contour and after this draw founded contour with fill parameter -1 using method drawContours. Here's useful link: http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/find_contours/find_contours.html

like image 67
ArtemStorozhuk Avatar answered Sep 23 '22 12:09

ArtemStorozhuk


To improve the smoothness of the output, or in other words to reduce the black holes in the detected area try performing morphological operations on the resulting image. Following documentation explains the eroding and dilating functions in opencv. http://docs.opencv.org/doc/tutorials/imgproc/erosion_dilatation/erosion_dilatation.html

like image 24
hbn1991 Avatar answered Sep 22 '22 12:09

hbn1991