0

I'm facing a problem with detecting skin color in images loaded in picureBox1, I implemented the equations in the article below.

I need your suggestions that make my code working.

Thanks

Code:

        Bitmap bm = (Bitmap)pictureBox1.Image;
        Bitmap bmp = new Bitmap(pictureBox1.Image.Width, pictureBox1.Image.Height);
        Color color = new Color();
        double Cb,Cr,r,g,b,R,G,B; 
        double CbMean = 156.56;
        double CrMean = 117.43;
        double K1 = 160.13 ;
        double K2 = 12.143;
        double K3 = 12.143;
        double K4 = 299.46;
        for (int i = 0; i < pictureBox1.Width; i++)
        {
            for (int j = 0; j < pictureBox1.Height; j++)
            {
                color = bm.GetPixel(i, j);
                R = Convert.ToDouble(color.R);
                G = Convert.ToDouble(color.G);
                B = Convert.ToDouble(color.B);

                r = R / (R+G+B);
                g = G / (R+G+B);
                b = B / (R+G+B);
                Cb = (-0.169 * r - 0.331 * g + 0.500 * b);
                Cr = (0.500 * r - 0.418 * g - 0.082 * b);
                Cb -= CbMean;
                Cr -= CrMean;
                double CbDist = (K1 * Cb) + (K3 * Cr);
                double CrDist = (K2 * Cb) + (K4 * Cr);
                double CbDist1 =(0.5 * Cb) + (0.5 * Cr);
                double CrDist1 = (0.5 * Cb) + (0.5 * Cr);
                double dist = CbDist + CrDist;
                double dist1 = CbDist1 + CrDist1;
                double gmm = Math.Exp(dist * dist1);
                bmp.SetPixel(i, j, Color.FromArgb(255, (int)gmm, (int)gmm, (int)gmm));
            }
        }
7
  • 3
    Can you include more information about the error you're seeing? Commented Jan 8, 2014 at 19:08
  • ArgumentException was handled in the last line of the code (bmb.setpixel...), however, sometimes I get black image only. Commented Jan 8, 2014 at 19:12
  • maybe your gmm value is bigger than 255? Commented Jan 8, 2014 at 19:17
  • Make sure gmm isn't less than 0 or greater than 255, FromARGB will throw an exception if so Commented Jan 8, 2014 at 19:18
  • Run in debug mode and step through the code to see which line is throwing the exception. Commented Jan 8, 2014 at 19:18

1 Answer 1

1

It seems your gmm value is bigger than 255

See the documentation

ArgumentException alpha, red, green, or blue is less than 0 or greater than 255.

Check your gmm's value and make sure it has a valid value

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the answer, but I did exactly what the article says. and GMM value is an equation form the above paper. I don't know what makes the error.
@HannahSolomons ...except that's clearly what the problem is. Just output something if gmm is not between 0 and 255 and I can almost guarantee it will happen.
in some images, the output is shown as BLACK image in pictureBox2, and sometimes it get ArgumentException error in GMM values. My equation is right. I need to know where is my error according to the paper above. I need to draw the likelihood image show also in the paper.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.