当前位置: 首页>>代码示例>>C#>>正文


C# Bitmap.SetPixelGray方法代码示例

本文整理汇总了C#中System.Drawing.Bitmap.SetPixelGray方法的典型用法代码示例。如果您正苦于以下问题:C# Bitmap.SetPixelGray方法的具体用法?C# Bitmap.SetPixelGray怎么用?C# Bitmap.SetPixelGray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Drawing.Bitmap的用法示例。


在下文中一共展示了Bitmap.SetPixelGray方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AverageFilter

        /// <summary>
        /// 均值滤波算法
        /// </summary>
        public Bitmap AverageFilter()
        {
            Bitmap retVal = new Bitmap(image.Width, image.Height);
            for (int y = 1; y < image.Height - 1; y++)
            {
                for (int x = 1; x < image.Width - 1; x++)
                {
                    int avr = 0, sum = 0;
                    for (int i = -1; i <= 1; i++)
                    {
                        for (int j = -1; j <= 1; j++)
                        {
                            sum += image.GetPixelGray(x + i, y + j);
                        }
                    }

                    avr = (int)(sum / 9.0F);

                    retVal.SetPixelGray(x, y, avr);
                }
            }

            return retVal;
        }
开发者ID:RyuAsuka,项目名称:DigitalImageProcessing,代码行数:27,代码来源:Enhancement.cs

示例2: FrequencyFilter

        /// <summary>
        /// 频域滤波算法
        /// </summary>
        /// <param name="_image">要施加滤波算法的图像</param>
        /// <param name="type">滤波类型</param>
        /// <param name="d0"></param>
        /// <returns></returns>
        public Bitmap FrequencyFilter(Bitmap _image, FilterType type, int d0)
        {
            Bitmap output = new Bitmap(_image);
            int iw = image.Width;
            int ih = image.Height;
            double[] outpix = new double[iw * ih];
            Complex[] complex;

            for (int y = 0; y < ih; y++)
            {
                for (int x = 0; x < iw; x++)
                {
                    outpix[x + y * iw] = image.GetPixelGray(x, y);
                }
            }

            FFT2 fft2 = new FFT2();
            fft2.setData2(iw, ih, outpix);
            complex = fft2.getFFT2();

            Complex bc = new Complex();

            for (int v = 0; v < ih; v++)
            {
                for (int u = 0; u < iw; u++)
                {
                    double dd = Math.Sqrt(u * u + v * v);
                    //double dd = u * u + v * v;

                    switch (type)
                    {
                        case FilterType.IdealLowPass:
                            if (dd <= d0) bc = new Complex(1);
                            else bc = new Complex(0);
                            //bc = new Complex(1 / (1 + 0.4142 * dd / (d0 * d0)));
                            break;
                        case FilterType.IdealHighPass:
                            if (dd <= d0) bc = new Complex(0);
                            else bc = new Complex(1);
                            //bc = new Complex(1 / (1 + 0.4142 * (d0 * d0)/dd));
                            break;
                        case FilterType.GaussianLowPass:
                            bc = new Complex(Math.Exp(-(dd * dd) / (2 * d0 * d0)));
                            //bc = new Complex(Math.Exp(-0.5*Math.Log(2)*dd/(d0*d0)));
                            break;
                        case FilterType.GaussianHighPass:
                            bc = new Complex(Math.Exp(-(dd * dd) / (2 * d0 * d0)));
                            //bc = new Complex(Math.Exp(-0.5 * Math.Log(2) * dd / (d0 * d0)));
                            bc = new Complex(1) - bc;
                            break;
                        default:
                            break;
                    }
                    complex[u + v * iw] = complex[u + v * iw] * bc;
                }
            }

            //FFT2 ifft2 = new FFT2();
            fft2.setData2i(iw, ih, complex);
            outpix = fft2.getPixels2i();

            double max = outpix[0];
            for (int y = 0; y < ih; y++)
            {
                for (int x = 0; x < iw; x++)
                {
                    if (max < outpix[x + y * iw])
                        max = outpix[x + y * iw];
                }
            }

            for (int y = 0; y < ih; y++)
            {
                for (int x = 0; x < iw; x++)
                {
                    int r = (int)(outpix[x + y * iw] * 255 / max);
                    output.SetPixelGray(x, y, r);
                }
            }

            return output;
        }
开发者ID:RyuAsuka,项目名称:DigitalImageProcessing,代码行数:89,代码来源:Enhancement.cs

示例3: Laplace

        /// <summary>
        /// 拉普拉斯锐化算法
        /// </summary>
        public Bitmap Laplace()
        {
            Bitmap retVal = new Bitmap(image.Width, image.Height);
            for (int y = 1; y < image.Height - 1; y++)
            {
                for (int x = 1; x < image.Width - 1; x++)
                {
                    int value = image.GetPixelGray(x - 1, y - 1) +
                                image.GetPixelGray(x, y - 1) +
                                image.GetPixelGray(x + 1, y - 1) +
                                image.GetPixelGray(x - 1, y) +
                                image.GetPixelGray(x + 1, y) +
                                image.GetPixelGray(x - 1, y + 1) +
                                image.GetPixelGray(x, y + 1) +
                                image.GetPixelGray(x + 1, y + 1) -
                                8 * image.GetPixelGray(x, y);

                    value = image.GetPixelGray(x, y) - value;
                    if (value > 255) value = 255;
                    if (value < 0) value = 0;

                    retVal.SetPixelGray(x, y, value);
                }
            }

            return retVal;
        }
开发者ID:RyuAsuka,项目名称:DigitalImageProcessing,代码行数:30,代码来源:Enhancement.cs

示例4: MedianFilter

        /// <summary>
        /// 中值滤波算法
        /// </summary>
        public Bitmap MedianFilter()
        {
            Bitmap retVal = new Bitmap(image.Width, image.Height);
            int[] values = new int[9];

            for (int y = 1; y < image.Height - 1; y++)
            {
                for (int x = 1; x < image.Width - 1; x++)
                {
                    int median = 0;
                    values[0] = image.GetPixelGray(x - 1, y - 1);
                    values[1] = image.GetPixelGray(x, y - 1);
                    values[2] = image.GetPixelGray(x + 1, y - 1);
                    values[3] = image.GetPixelGray(x - 1, y);
                    values[4] = image.GetPixelGray(x, y);
                    values[5] = image.GetPixelGray(x + 1, y);
                    values[6] = image.GetPixelGray(x - 1, y + 1);
                    values[7] = image.GetPixelGray(x, y + 1);
                    values[8] = image.GetPixelGray(x + 1, y + 1);

                    IEnumerable<int> query = values.OrderBy(v => v);

                    median = query.ElementAt(4);

                    retVal.SetPixelGray(x, y, median);
                }
            }
            return retVal;
        }
开发者ID:RyuAsuka,项目名称:DigitalImageProcessing,代码行数:32,代码来源:Enhancement.cs

示例5: MenuSpectra_Click

        private void MenuSpectra_Click(object sender, EventArgs e)
        {
            if (!CheckImage()) return;
            Bitmap bm = new Bitmap(originImage.Image);
            int iw = bm.Width;
            int ih = bm.Height;

            if (iw == 256 && ih == 256)
            {
                double[] iPix = new double[iw * ih];
                double[] oMod = new double[iw * ih];

                for (int y = 0; y < ih; y++)
                {
                    for (int x = 0; x < iw; x++)
                    {
                        iPix[x+y*iw] = bm.GetPixelGray(x, y);
                    }
                }

                FFT2 fft2 = new FFT2();
                fft2.setData2(iw, ih, iPix);
                fftData = fft2.getFFT2();

                // 生成频谱图像
                int u, v;
                for (int y = 0; y < ih; y++)
                {
                    for (int x = 0; x < iw; x++)
                    {
                        double tem = fftData[x+y*iw].Real * fftData[x+y*iw].Real +
                                    fftData[x+y*iw].Imaginary * fftData[x+y*iw].Imaginary;
                        tem = Math.Sqrt(tem) / 100;
                        if (tem > 255) tem = 255;

                        if (x < iw / 2) u = x + iw / 2;
                        else u = x - iw / 2;
                        if (y < ih / 2) v = y + ih / 2;
                        else v = y - ih / 2;

                        oMod[u+v*iw] = tem;
                    }
                }

                for (int y = 0; y < ih; y++)
                {
                    for (int x = 0; x < iw; x++)
                    {
                        int r = (int)oMod[x+y*iw];
                        try {
                            bm.SetPixelGray(x, y, r);
                        }
                        catch(Exception ex)
                        {
                            MessageBox.Show(ex.Message, "查看频谱", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }

                afterImage.Image = bm;
            }
            else
            {
                MessageBox.Show("仅适用于256*256图像!", "傅里叶变换", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
开发者ID:RyuAsuka,项目名称:DigitalImageProcessing,代码行数:66,代码来源:MainForm.cs


注:本文中的System.Drawing.Bitmap.SetPixelGray方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。