1

See this related question.

I want to obtain the same outcome using AForge.net framework. The output should match the following:

enter image description here

The output seems to be not coming as expected:

Why is the output different in AForge.net?

.

Source Code

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        Bitmap image = (Bitmap)Bitmap.FromFile(@"StandardImage\\lena.png");
        Bitmap conv = new Bitmap(image.Width, image.Height, image.PixelFormat);

        ComplexImage cImage = ComplexImage.FromBitmap(image);
        cImage.ForwardFourierTransform();

        ComplexImage cKernel = ComplexImage.FromBitmap(image);
        cImage.ForwardFourierTransform();

        ComplexImage convOut = ComplexImage.FromBitmap(conv);
        convOut.ForwardFourierTransform();

        for (int y = 0; y < cImage.Height; y++)
        {
            for (int x = 0; x < cImage.Width; x++)
            {
                convOut.Data[x, y] = cImage.Data[x, y] * cKernel.Data[x, y];
            }
        }

        convOut.BackwardFourierTransform();

        Bitmap bbbb = convOut.ToBitmap();

        pictureBox1.Image = bbbb;

    }
}
user366312
  • 16,949
  • 65
  • 235
  • 452

2 Answers2

2

The main problem is

    ComplexImage cKernel = ComplexImage.FromBitmap(image);
    //cImage.ForwardFourierTransform(); //<--- This line should be FFT of cKernel
    cKernel.ForwardFourierTransform();

This would solve the problem you mentioned in the resulting image, but if you want to get an image similar to the bottom right image you need to do some normalization to increase the intensity of pixels.


update: The bottom right image is actually a Fourier image so I think we should remove the BFF.

//convOut.BackwardFourierTransform();

Amir Ehsani
  • 136
  • 1
  • 4
  • Thanks. How is going there in Tehran with US sanction? – user366312 Aug 13 '18 at 11:37
  • 1
    Life as usual! And by usual I mean usual crazy things happening everyday, economic crisis, exchange rate drop and so on! – Amir Ehsani Aug 13 '18 at 12:41
  • I am sorry to say this, but I am not being able to normalize the output image. – user366312 Aug 13 '18 at 19:22
  • Well, the normalization is usually a bit annoying. I updated the answer to add a simple function to set intensity multiplied by a magic number 150 and a sample result. I think the difference between the bottom right image and the sample result is due to data loss in converting Fourier to RGB, it would be more rational if we could convert it to double matrix instead of RGB bitmap. – Amir Ehsani Aug 14 '18 at 05:41
  • 1
    `The bottom right image is actually a Fourier image` --- which is actually not. – user366312 Aug 14 '18 at 08:05
  • You're right. I tried to do same using Scipy and worked like a charm, but following AForge code results is something weird: ComplexImage.FromBitmap((Bitmap)Bitmap.FromFile("lena_gray.gif")).ToBitmap().Save("out.gif"); – Amir Ehsani Aug 14 '18 at 11:57
0

It is seems like you are not using a gaussian kernel with Aforge, Anyway, the library has a method for convolution with gaussian:

int w=4,h=11;
GaussianBlur filter = new GaussianBlur( w, h );
// apply the filter
filter.ApplyInPlace( image );

Try it and the output should be the same as the others.

Dr.Haimovitz
  • 1,568
  • 12
  • 16