Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        /// <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;
        }
Ejemplo n.º 3
0
        public Complex[] getData1i()
        {
            // 求共轭
            for (int i = 0; i < count; i++)
            {
                double im = -x[i].Imaginary;
                x[i].Imaginary = im;
            }

            fft2 = new FFT2();
            fft2.setData1(x, power);
            fd1i = fft2.getData1();

            for (int i = 0; i < count; i++)
            {
                double re = fd1i[i].Real;
                double im = -fd1i[i].Imaginary;
                fd1i[i].Imaginary = im / count;
                fd1i[i].Real = re / count;
            }
            return fd1i;
        }