Ann  Orlova
Ann Orlova

Reputation: 1348

Skin Detection with Gaussian Mixture Models

I'm doing skin detection algorithm according to this article. There are two models at page 21: Mixture of Gaussian Skin and Non-skin Color Model.

The first model for skin detection works exellent. There are examples:

1)Orginal image:

enter image description here

2) Skin mask

enter image description here

But the non-skin model gives wrong results:

enter image description here

Here is my code:

 ipl_image_wrapper NudityDetector::filterPixelsWithGMM(const float covarinceMatrix[][3], const float meanMatrix[][3], const float weightVector[], const float probValue) const
{   
    ipl_image_wrapper mask = cvCreateImage(cvGetSize(m_image.get()), IPL_DEPTH_8U, 1);

    double probability = 0.0;
    float x[3] = { 0, 0, 0};

     for(int i = 0; i < m_image.get()->height; ++i)
    {
        for(int j = 0; j < m_image.get()->width; ++j)
        {
            if (m_image.get()->nChannels == 3)
            {
                x[0] = (reinterpret_cast<uchar*>(m_image.get()->imageData + i * m_image.get()->widthStep))[j * 3 + 2];
                x[1] = (reinterpret_cast<uchar*>(m_image.get()->imageData + i * m_image.get()->widthStep))[j * 3 + 1];
                x[2] = (reinterpret_cast<uchar*>(m_image.get()->imageData + i * m_image.get()->widthStep))[j * 3];

                double cov_det = 0.0;
                double power = 0.0;

                double A1 = 0.0;
                double A2 = 0.0;
                double A3 = 0.0;

                probability = 0;

                for (int k = 0; k < 16; ++k)
                {
                    cov_det = covarinceMatrix[k][0] * covarinceMatrix[k][1] * covarinceMatrix[k][2];

                    A1 = covarinceMatrix[k][1] * covarinceMatrix[k][2];
                    A2 = covarinceMatrix[k][0] * covarinceMatrix[k][2];
                    A3 = covarinceMatrix[k][0] * covarinceMatrix[k][1];

                    power =(std::pow((x[0] - meanMatrix[k][0]), 2) * A1 +
                            std::pow((x[1] - meanMatrix[k][1]), 2) * A2 +
                            std::pow((x[2] - meanMatrix[k][2]), 2) * A3 ) / (2 * cov_det);

                     probability += 100 * weightVector[k] *std::exp(-power) / (std::pow(2 * M_PI, 3/2) * std::pow(cov_det, 1/2));
                }

                if ( probability < probValue)
                {
                    (reinterpret_cast<uchar*>(mask.get()->imageData + i * mask.get()->widthStep))[j] = 0;
                }
                else
                {
                    (reinterpret_cast<uchar*>(mask.get()->imageData + i * mask.get()->widthStep))[j] = 255;
                }
            }
        }
    }

    cvDilate(mask.get(), mask.get(), NULL, 2);
    cvErode(mask.get(), mask.get(), NULL, 1);

    return mask;
}

ipl_image_wrapper NudityDetector::detectSkinWithGMM(const float probValue) const
{
   //matrices are from article
    ipl_image_wrapper mask = filterPixelsWithGMM(COVARIANCE_SKIN_MATRIX, MEAN_SKIN_MATRIX, SKIN_WEIGHT_VECTOR, probValue);

    return mask;
 }

ipl_image_wrapper NudityDetector::detectNonSkinWithGMM(const float probValue) const
{
    //matrices are from article
   ipl_image_wrapper mask = filterPixelsWithGMM(COVARIANCE_NON_SKIN_MATRIX, MEAN_NON_SKIN_MATRIX, NON_SKIN_WEIGHT_VECTOR, probValue);

   return mask;
 }

What I'm doing wrong? Maybe I misunderstand the meaning of tre article? Or I translated formula wrong in the code?

Thank you in advance!

Upvotes: 3

Views: 3792

Answers (1)

fatihk
fatihk

Reputation: 7919

In fact, there seems to be nothing wrong with the results, non-skin model correctly identifies non-skin regions as 255 and skin regions as 0. You may just need to tune parameter probValue to a lower value to get rid of some false negatives (small non-skin regions)

GMM may not be an effective approach for skin detection and you may employ some edge intensity information as a regularization parameter so that detected regions will not be fragmented.

Upvotes: 2

Related Questions