C# 灰度与彩色FFT

C# 灰度与彩色FFT,c#,fft,C#,Fft,以下代码将彩色图像转换为灰度,然后计算其FFT: private void button1_Click(object sender, EventArgs e) { Bitmap source = pictureBox1.Image as Bitmap; Bitmap gray = Grayscale.ToGrayscale(source); Complex[,] cpxImage = ImageDataConverter.ToC

以下代码将彩色图像转换为灰度,然后计算其FFT:

    private void button1_Click(object sender, EventArgs e)
    {
        Bitmap source = pictureBox1.Image as Bitmap;

        Bitmap gray = Grayscale.ToGrayscale(source);

        Complex[,] cpxImage = ImageDataConverter.ToComplex(gray);

        Complex[,] fftCpxImage = FourierTransform.ForwardFFT(cpxImage);

        Complex[,] shiftedFftCpxImage = FourierShifter.ShiftFft(fftCpxImage);

        Bitmap mag = FourierPlot.FftMagnitudePlot(shiftedFftCpxImage, PixelFormat.Format8bppIndexed);
        Bitmap phase = FourierPlot.FftPhasePlot(shiftedFftCpxImage, PixelFormat.Format8bppIndexed);

        pictureBox2.Image = gray;

        pictureBox3.Image =  mag;

        pictureBox4.Image = phase;
    }
下面的代码将彩色图像分割为三个通道,计算它们的FFT并将它们合并在一起,以形成FFT的RGB图像

    private void button2_Click(object sender, EventArgs e)
    {
        Bitmap source = pictureBox1.Image as Bitmap;

        int [,,] array3d = ImageDataConverter.ToInteger3d(source);

        int[,] red = ArrayTools<int>.Split(array3d, 0);
        int[,] green = ArrayTools<int>.Split(array3d, 1);
        int[,] blue = ArrayTools<int>.Split(array3d, 2);

        Complex [,] cpxRed = ImageDataConverter.ToComplex(red);
        Complex [,] cpxGreen = ImageDataConverter.ToComplex(green);
        Complex [,] cpxBlue = ImageDataConverter.ToComplex(blue);

        Complex[,] fftCpxRed = FourierTransform.ForwardFFT(cpxRed);
        Complex[,] fftCpxGreen = FourierTransform.ForwardFFT(cpxGreen);
        Complex[,] fftCpxBlue = FourierTransform.ForwardFFT(cpxBlue);

        Complex[,] shiftedFftCpxRed = FourierShifter.ShiftFft(fftCpxRed);
        Complex[,] shiftedFftCpxGreen = FourierShifter.ShiftFft(fftCpxGreen);
        Complex[,] shiftedFftCpxBlue = FourierShifter.ShiftFft(fftCpxBlue);

        int[,] fftRed = ImageDataConverter.ToIntegerMagnitude(shiftedFftCpxRed);
        int[,] fftGreen = ImageDataConverter.ToIntegerMagnitude(shiftedFftCpxGreen);
        int[,] fftBlue = ImageDataConverter.ToIntegerMagnitude(shiftedFftCpxBlue);

        int [,,] dest = ArrayTools<int>.Merge(fftRed, fftGreen, fftBlue);

        Bitmap mag = ImageDataConverter.ToBitmap3d(dest, PixelFormat.Format8bppIndexed);
        Grayscale.SetPalette(mag);

        pictureBox3.Image = mag;
    }

我认为这是一个问题

    public static int[,] ToIntegerMagnitude(Complex[,] image)
    {
        int Width = image.GetLength(0);
        int Height = image.GetLength(1);

        int[,] integer = new int[Width, Height];


        for (int j = 0; j <= Height - 1; j++)
        {
            for (int i = 0; i <= Width - 1; i++)
            {
                integer[i, j] = ((int)image[i, j].Magnitude);
            }
        }

        return integer;
    }
publicstaticint[,]到integermagnity(复数[,]图像)
{
int Width=image.GetLength(0);
int Height=image.GetLength(1);
整数=新整数[宽度,高度];
对于(int j=0;j

这个评论解决了我的问题

    private void button2_Click(object sender, EventArgs e)
    {
        Bitmap source = (Bitmap)(pictureBox1.Image as Bitmap).Clone();

        int[, ,] array3d = ImageDataConverter.ToInteger3d(source);

        int[,] red = ArrayTools<int>.Split(array3d, 0);
        int[,] green = ArrayTools<int>.Split(array3d, 1);
        int[,] blue = ArrayTools<int>.Split(array3d, 2);

        Complex[,] cpxRed = ImageDataConverter.ToComplex(red);
        Complex[,] cpxGreen = ImageDataConverter.ToComplex(green);
        Complex[,] cpxBlue = ImageDataConverter.ToComplex(blue);

        Complex[,] fftCpxRed = FourierTransform.ForwardFFT(cpxRed);
        Complex[,] fftCpxGreen = FourierTransform.ForwardFFT(cpxGreen);
        Complex[,] fftCpxBlue = FourierTransform.ForwardFFT(cpxBlue);

        Complex[,] shiftedFftCpxRed = FourierShifter.ShiftFft(fftCpxRed);
        Complex[,] shiftedFftCpxGreen = FourierShifter.ShiftFft(fftCpxGreen);
        Complex[,] shiftedFftCpxBlue = FourierShifter.ShiftFft(fftCpxBlue);

        int[,] normRed = FourierNormalizer.Normalize(shiftedFftCpxRed, NormalizeType.Magnitude);
        int[,] normGreen = FourierNormalizer.Normalize(shiftedFftCpxGreen, NormalizeType.Magnitude);
        int[,] normBlue = FourierNormalizer.Normalize(shiftedFftCpxBlue, NormalizeType.Magnitude);

        Bitmap mag = ImageDataConverter.ToBitmap3d(normRed, normGreen, normBlue, PixelFormat.Format8bppIndexed);
        //Grayscale.SetPalette(mag);
        //Bitmap mag = FourierPlot.FftMagnitudePlot(shiftedFftCpxRed, PixelFormat.Format8bppIndexed);
        Bitmap phase = FourierPlot.FftPhasePlot(shiftedFftCpxRed, PixelFormat.Format8bppIndexed);

        pictureBox2.Image = mag;
        pictureBox3.Image = mag;
        pictureBox4.Image = phase;
    }
private void按钮2\u单击(对象发送者,事件参数e)
{
位图源=(位图)(pictureBox1.Image作为位图)。克隆();
int[,]array3d=ImageDataConverter.ToInteger3d(源);
int[,]red=ArrayTools.Split(array3d,0);
int[,]绿色=ArrayTools.Split(array3d,1);
int[,]blue=ArrayTools.Split(array3d,2);
Complex[,]cpxRed=ImageDataConverter.ToComplex(红色);
Complex[,]cpxGreen=ImageDataConverter.ToComplex(绿色);
Complex[,]cpxBlue=ImageDataConverter.ToComplex(蓝色);
复数[,]fftCpxRed=fourier变换.前向fft(cpxRed);
复数[,]fftCpxGreen=FourierTransform.ForwardFFT(cpxGreen);
复数[,]fftCpxBlue=fourier变换.前向fft(cpxBlue);
复数[,]移位fftCpxRed=fourier移位器.ShiftFft(fftCpxRed);
复数[,]移位的fftCpxGreen=fouriershift.ShiftFft(fftCpxGreen);
复数[,]移位fftCpxBlue=fourier移位器.ShiftFft(fftCpxBlue);
int[,]normRed=FourierNormalizer.Normalize(shiftedFftCpxRed,NormalizeType.magnity);
int[,]normGreen=FourierNormalizer.Normalize(shiftedFftCpxGreen,NormalizeType.magnity);
int[,]normBlue=FourierNormalizer.Normalize(shiftedFftCpxBlue,NormalizeType.magnity);
位图mag=ImageDataConverter.ToBitmap3d(normRed、normregreen、normrue、PixelFormat.format8bpindexed);
//灰度设置调色板(mag);
//位图mag=FourierPlot.FftMagnitudePlot(移位FFTCPXRED,像素格式.Format8Bppined);
位图相位=FourierPlot.FftPhasePlot(移位FFTCPXRED,像素格式.Format8BPindexed);
pictureBox2.Image=mag;
pictureBox3.Image=mag;
pictureBox4.图像=相位;
}

您可能不想将大小转换为那样的整数。您需要先缩放大小。您不能使用
FourierPlot.FftMagnitudePlot()吗
要将每个通道的结果转换为整数位图,然后合并这些位图?@CrisLuengo,是的,我可以。但是,为什么这个特定的代码不起作用?以及,我该如何使它起作用?
FourierPlot.FftMagnitudePlot
似乎对输入值进行了对数拉伸。在转换为整数时,可能不会这样做所有值的亮度都太低,看不见。@CrisLuengo,包含了
FftMagnitudePlot
的代码。的确。我会将3个通道通过
FourierNormalizer.Normalize
,然后将它们组合成一个彩色位图。很高兴我能提供帮助!:)
public static class FourierPlot
{
    public static Bitmap FftMagnitudePlot(Complex[,] fftImage, PixelFormat pixelFormat)
    {
        int[,] FourierMagnitudeNormalizedInteger = FourierNormalizer.Normalize(fftImage, NormalizeType.Magnitude);

        Bitmap color = ImageDataConverter.ToBitmap2d(FourierMagnitudeNormalizedInteger, pixelFormat);

        Grayscale.SetPalette(color);

        return color;
    }

    public static Bitmap FftPhasePlot(Complex[,] fftImage, PixelFormat pixelFormat)
    {
        int[,] FourierPhaseNormalizedInteger = FourierNormalizer.Normalize(fftImage, NormalizeType.Phase);

        Bitmap color = ImageDataConverter.ToBitmap2d(FourierPhaseNormalizedInteger, pixelFormat);

        Grayscale.SetPalette(color);

        return color;
    }
}

public partial class FourierNormalizer
{
    public static int[,] Normalize(Complex[,] Output, NormalizeType normalizeType)
    {
        int Width = Output.GetLength(0);
        int Height = Output.GetLength(1);

        double[,] FourierDouble = new double[Width, Height];
        double[,] FourierLogDouble = new double[Width, Height];
        int[,] FourierNormalizedInteger = new int[Width, Height];

        double max = 0;

        if (normalizeType == NormalizeType.Magnitude)
        {
            for (int i = 0; i <= Width - 1; i++)
            {
                for (int j = 0; j <= Height - 1; j++)
                {
                    FourierDouble[i, j] = Output[i, j].Magnitude;
                    FourierLogDouble[i, j] = (double)Math.Log(1 + FourierDouble[i, j]);
                }
            }

            max = FourierLogDouble[0, 0];
        }
        else
        {
            for (int i = 0; i <= Width - 1; i++)
            {
                for (int j = 0; j <= Height - 1; j++)
                {
                    FourierDouble[i, j] = Output[i, j].Phase;
                    FourierLogDouble[i, j] = (double)Math.Log(1 + Math.Abs(FourierDouble[i, j]));
                }
            }

            FourierLogDouble[0, 0] = 0;
            max = FourierLogDouble[1, 1];
        }

        for (int i = 0; i <= Width - 1; i++)
        {
            for (int j = 0; j <= Height - 1; j++)
            {
                if (FourierLogDouble[i, j] > max)
                {
                    max = FourierLogDouble[i, j];
                }
            }
        }

        for (int i = 0; i <= Width - 1; i++)
        {
            for (int j = 0; j <= Height - 1; j++)
            {
                FourierLogDouble[i, j] = FourierLogDouble[i, j] / max;
            }
        }

        if (normalizeType == NormalizeType.Magnitude)
        {
            for (int i = 0; i <= Width - 1; i++)
            {
                for (int j = 0; j <= Height - 1; j++)
                {
                    FourierNormalizedInteger[i, j] = (int)(2000 * FourierLogDouble[i, j]);
                }
            }
        }
        else
        {
            for (int i = 0; i <= Width - 1; i++)
            {
                for (int j = 0; j <= Height - 1; j++)
                {
                    FourierNormalizedInteger[i, j] = (int)(255 * FourierLogDouble[i, j]);
                }
            }
        }

        return FourierNormalizedInteger;
    }
}

    public static Bitmap ToBitmap3d(int[, ,] image, PixelFormat pixelFormat)
    {
        int Width = image.GetLength(1);
        int Height = image.GetLength(2);

        Bitmap bmp = new Bitmap(Width, Height, pixelFormat);

        BitmapLocker locker = new BitmapLocker(bmp);
        locker.Lock();

        int [,] red = ArrayTools<int>.Split(image, 0);
        int [,] green = ArrayTools<int>.Split(image, 1);
        int [,] blue = ArrayTools<int>.Split(image, 2);

        for (int y = 0; y < Height; y++)
        {
            for (int x = 0; x < Width; x++)
            {
                int r = red[x,y];
                int g = green[x,y];
                int b = blue[x,y];

                Color clr = Color.FromArgb(r,g,b);

                locker.SetPixel(x, y, clr);
            }
        }

        locker.Unlock();

        return bmp;
    } 
    private void button2_Click(object sender, EventArgs e)
    {
        Bitmap source = (Bitmap)(pictureBox1.Image as Bitmap).Clone();

        int[, ,] array3d = ImageDataConverter.ToInteger3d(source);

        int[,] red = ArrayTools<int>.Split(array3d, 0);
        int[,] green = ArrayTools<int>.Split(array3d, 1);
        int[,] blue = ArrayTools<int>.Split(array3d, 2);

        Complex[,] cpxRed = ImageDataConverter.ToComplex(red);
        Complex[,] cpxGreen = ImageDataConverter.ToComplex(green);
        Complex[,] cpxBlue = ImageDataConverter.ToComplex(blue);

        Complex[,] fftCpxRed = FourierTransform.ForwardFFT(cpxRed);
        Complex[,] fftCpxGreen = FourierTransform.ForwardFFT(cpxGreen);
        Complex[,] fftCpxBlue = FourierTransform.ForwardFFT(cpxBlue);

        Complex[,] shiftedFftCpxRed = FourierShifter.ShiftFft(fftCpxRed);
        Complex[,] shiftedFftCpxGreen = FourierShifter.ShiftFft(fftCpxGreen);
        Complex[,] shiftedFftCpxBlue = FourierShifter.ShiftFft(fftCpxBlue);

        int[,] normRed = FourierNormalizer.Normalize(shiftedFftCpxRed, NormalizeType.Magnitude);
        int[,] normGreen = FourierNormalizer.Normalize(shiftedFftCpxGreen, NormalizeType.Magnitude);
        int[,] normBlue = FourierNormalizer.Normalize(shiftedFftCpxBlue, NormalizeType.Magnitude);

        Bitmap mag = ImageDataConverter.ToBitmap3d(normRed, normGreen, normBlue, PixelFormat.Format8bppIndexed);
        //Grayscale.SetPalette(mag);
        //Bitmap mag = FourierPlot.FftMagnitudePlot(shiftedFftCpxRed, PixelFormat.Format8bppIndexed);
        Bitmap phase = FourierPlot.FftPhasePlot(shiftedFftCpxRed, PixelFormat.Format8bppIndexed);

        pictureBox2.Image = mag;
        pictureBox3.Image = mag;
        pictureBox4.Image = phase;
    }