首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

图片增添高斯噪声

2013-03-19 
图片添加高斯噪声 这是添加高斯噪声的代码,有没有大神看看为什么这个代码只能给部分图像加噪声private voi

图片添加高斯噪声
 这是添加高斯噪声的代码,有没有大神看看为什么这个代码只能给部分图像加噪声图片增添高斯噪声
private void btngauss_Click(object sender, EventArgs e)
        {
            if (CurBitmap != null)
            {
                gaussnoise gnoise = new gaussnoise();
                if (gnoise.ShowDialog() == DialogResult.OK)
                {

                    Rectangle rect = new Rectangle(0, 0, CurBitmap.Width, CurBitmap.Height);
                    System.Drawing.Imaging.BitmapData bmpData = CurBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, CurBitmap.PixelFormat);
                    IntPtr ptr = bmpData.Scan0;
                    int bytes = CurBitmap.Width * CurBitmap.Height;
                    byte[] grayvalues = new byte[bytes];
                    System.Runtime.InteropServices.Marshal.Copy(ptr, grayvalues, 0, bytes);
                    double temp = 0;
                    Random r1, r2;
                    double v1, v2;
                    r1 = new Random(unchecked((int)DateTime.Now.Ticks));
                    r2 = new Random(unchecked((int)DateTime.Now.Ticks));
                    for (int i = 0; i < bytes; i++)
                    {
                        do
                        {
                            v1 = r1.NextDouble();
                        }
                        while (v1 <= 0.00000000001);


                        v2 = r2.NextDouble();
                        temp = Math.Sqrt(-2 * Math.Log(v1)) * Math.Cos(2 * Math.PI * v2) * gnoise.GetSigma + gnoise.GetMean;
                        //temp = (1 / gnoise.GetSigma * Math.Sqrt(2 * Math.Sqrt(2 * Math.PI)) * Math.Pow(Math.E, - (Math.Pow(v1 - gnoise.GetMean, 2) / (2 * gnoise.GetSigma * gnoise.GetSigma))));
                        temp = temp + grayvalues[i];
                        if (temp > 255)
                        {
                            grayvalues[i] = 255;

                        }
                        else if (temp < 0)
                        {
                            grayvalues[i] = 0;
                        }
                        else
                            grayvalues[i] = Convert.ToByte(temp);
                    }
                    System.Runtime.InteropServices.Marshal.Copy(grayvalues, 0, ptr, bytes);
                    CurBitmap.UnlockBits(bmpData);
                }
                gnoise.Close();
                Invalidate();
            }

[解决办法]
int bytes = CurBitmap.Width * CurBitmap.Height;
长度不对,标准一个像素点包含RGB三个字节,所以应该是


int bytes = CurBitmap.Width * CurBitmap.Height*3;

热点排行