Hannah Solomons
Hannah Solomons

Reputation: 7

ArgumentException Error in c# code

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));
            }
        }

Upvotes: 0

Views: 239

Answers (1)

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101701

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

Upvotes: 1

Related Questions