ArgumentException c#代码中的错误

时间:2014-01-08 19:07:38

标签: c# image image-processing

我在检测picureBox1中加载的图像中的肤色时遇到问题,我在下面的文章中实现了这些方程式。

我需要你的建议让我的代码正常工作。

由于

代码:

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

1 个答案:

答案 0 :(得分:1)

看来你的gmm值大于255

请参阅documentation

  

ArgumentException alpha,red,green或blue小于0或更大   比255。

检查你的gmm值并确保它有一个有效值