androidopencvcomputer-visionhsv

Which is color space for HSV Skin detector openCV android?


I am newbie in computer vision. I make a skin detector on Android based on http://www.pyimagesearch.com/2014/08/18/skin-detection-step-step-example-using-python-opencv/

But I am having problem in choosing color space. https://i.sstatic.net/slWOU.jpg (Sr, I can't post image without 10 reputations)

I tried a lot of spaces on the Internet and this website but those're wrong.
This is my code:

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {    
    Mat hsv = new Mat();
    //H,S,V area
    Scalar lower = new Scalar(0, 0.28*255, 0);
    Scalar upper = new Scalar(25, 0.68*255, 255);
    //Skin detector HSV-based
    Imgproc.cvtColor(inputFrame.rgba(), hsv, Imgproc.COLOR_RGBA2RGB);
    Imgproc.cvtColor(hsv, hsv, Imgproc.COLOR_RGB2HSV_FULL);
    Mat result = new Mat(hsv.rows(), hsv.cols(), CvType.CV_8U);
    Core.inRange(hsv, lower, upper, result);
    // Perform and decrease noise
    Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(2, 2));
    Imgproc.erode(result, result, kernel);
    Imgproc.dilate(result, result, kernel);
    Imgproc.GaussianBlur(result, result, new Size(3,3), 0);
    // Output
    Imgproc.cvtColor(result, result, Imgproc.COLOR_GRAY2RGB);
    Imgproc.cvtColor(result, result, Imgproc.COLOR_RGB2RGBA);
    return result;
}

Update 1: I try to get skin space with face detection example:

        for (int i = 0; i < facesArray.length; i++) {
        Core.rectangle(mRgba, facesArray[i].tl(), facesArray[i].br(),
                FACE_RECT_COLOR, 3);        
        Mat hsv = new Mat(inputFrame.rgba(), facesArray[i]);
        Imgproc.cvtColor(hsv, hsv, Imgproc.COLOR_RGB2HSV_FULL);
        List<Mat> hsv_channel = new ArrayList<Mat>();
        Core.split(hsv, hsv_channel);
        Log.w("Color: ", "H: " + hsv_channel.get(0) + "S: " + hsv_channel.get(1) + "V: " + hsv_channel.get(2));
    }

And output:
H: Mat [ 261*261*CV_8UC1, isCont=true, isSubmat=false, nativeObj=0xffffffffb8b15658, dataAddr=0xffffffffb983d5f0 ]
S: Mat [ 261*261*CV_8UC1, isCont=true, isSubmat=false, nativeObj=0xffffffffb8b15698, dataAddr=0xffffffffb984e030 ]
V: Mat [ 261*261*CV_8UC1, isCont=true, isSubmat=false, nativeObj=0xffffffffb8b156d8, dataAddr=0xffffffffb985ea60 ]

How can I apply these to my code?


Solution

  • Those HSV color ranges should be ok for skin detection.

    I've used this image for testing: http://www.dance-classes.ca/photos/faces/faces_2004W_comp1.jpg enter image description here

    found on https://fashionisrael.wordpress.com/2010/03/08/fashion-battle-royale-castro-vs-hm/

    and this code (C++ reduced to hsv conversion and inRange testing:

    int main()
    {
        cv::Mat input = cv::imread("../inputData/faces2.jpg");
    
        cv::Mat hsv;
        cv::cvtColor(input,hsv,CV_BGR2HSV);
    
        double scaleSatLower = 0.28;
        double scaleSatUpper = 0.68;
        //double scaleSatLower = 0.18; // maybe better
        //double scaleSatLower = 0.08; // maybe too much
        //double scaleSatUpper = 0.78;
        cv::Scalar lower = cv::Scalar(0, scaleSatLower*255, 0);
        cv::Scalar upper = cv::Scalar(25, scaleSatUpper*255, 255);
    
        cv::Mat result;
        cv::inRange(hsv, lower, upper, result);
        cv::imshow("result", result);
    
    
        cv::imshow("input",input);
        cv::imwrite("../outputData/HSV_skin.png", result);
        cv::waitKey(0);
        return 0;
    }
    

    giving me this result:

    enter image description here

    Which is ok for a simple skin detector.

    Can you verify the result for this test image with your code?