C#中的Floyd Steinberg算法实现

时间:2014-11-18 20:50:20

标签: c# algorithm visual-studio-2012

我试图将Floyd-Steinberg算法ic#实现为抖动的24位图像到8位但我有异常错误System.IndexOutOfRangeException(索引超出了数组的边界。)行:g1 [x - 1,y + 1] + = err_g * 3/16;有没有人告诉我我犯了什么错误,我应该改变什么? r1表示旧像素 r2 - 新像素

{

            Color Pix;
           int[,] r1 = new int[bmp.Width, bmp.Height];
           int[,] r2 = new int[bmp.Width, bmp.Height];
           int[,] g1 = new int[bmp.Width, bmp.Height];
           int[,] g2 = new int[bmp.Width, bmp.Height];
           int[,] b1 = new int[bmp.Width, bmp.Height];
           int[,] b2 = new int[bmp.Width, bmp.Height];




            for (int y = 0; y < bmp.Height; y++)
            {


                for (int x = 0; x < bmp.Width; x++)
                {
                    Pix = bmp.GetPixel(x, y);
                    r1[x, y] = (Pix.R + Pix.G + Pix.B);
                    g1[x, y] = (Pix.R + Pix.G + Pix.B);
                    b1[x, y] = (Pix.R + Pix.G + Pix.B);               
                }
            }

            int err_r;
            int err_g;
            int err_b;

            for (int y = 0; y < bmp.Height; y++)
            {


                for (int x = 0; x < bmp.Width; x++)
                {

                    if (g1[x, y] < 128)
                    {
                        g2[x, y] = 0;

                    }
                    else
                    {
                        g2[x, y] = 255;

                    }
                    if (r1[x, y] < 128)
                    {
                        r2[x, y] = 0;
                                       }
                    else
                    {
                        r2[x, y] = 255;
                                       }
                    if (b1[x, y] < 128)
                    {
                        b2[x, y] = 0;

                    }
                    else
                    {
                        b2[x, y] = 255;

                    }

                    err_r = r1[x,y] - r2[x,y];
                    err_g = g1[x,y] - g2[x,y];
                    err_b = b1[x,y] - b2[x,y];



                    if (x < bmp.Width - 1)
                        r1[x + 1, y] += err_r * 7 / 16;
                        g1[x + 1, y] += err_g * 7/ 16;
                        b1[x + 1, y] += err_b * 7/ 16;
                    if (y < bmp.Height - 1)
                        r1[x, y + 1] += err_r * 5 / 16;
                        g1[x, y + 1] += err_g * 5 / 16;
                        b1[x, y + 1] += err_b * 5 / 16;
                    if (x < bmp.Width - 1 && y < bmp.Height - 1)
                        r1[x + 1, y + 1] += err_r / 16;
                        g1[x + 1, y + 1] += err_g / 16;
                        b1[x + 1, y + 1] += err_b / 16;
                    if (x > 0 && y < bmp.Height - 1)
                        r1[x - 1, y + 1] += err_r * 3 / 16;
                        g1[x - 1, y + 1] += err_g * 3 / 16;
                        b1[x - 1, y + 1] += err_b * 3/ 16;



                }
            }

            for (int y = 0; y < bmp.Height; y++)
            {


                for (int x = 0; x < bmp.Width; x++)
                {
                    bmp.SetPixel(x, y, Color.FromArgb(r2[x, y], g2[x, y], b1[x, y]));

                }
            }

1 个答案:

答案 0 :(得分:5)

我想你忘了在你的ifs之后加上“{}”:

if (x < bmp.Width - 1)
{
...
}
 if (y < bmp.Height - 1)
{
...
}
...