频域图像卷积 [英] Image convolution in frequency domain

查看:280
本文介绍了频域图像卷积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在频域中将Lena自己​​卷入其中。



我编写了以下应用程序来实现频域中两个图像的卷积。我遵循的步骤如下:



  1. 将Lena转换为复数矩阵。

  2. 应用FFT以获得复杂矩阵。

  3. 逐个元素地乘以两个复杂矩阵(如果这是卷积的定义)。

  4. 将IFFT应用于乘法结果。


输出似乎没有达到预期:





这里可以看到两个问题:




  • 输出只包含一个黑色背景,其中心只有一个点。

  • 原始图像在执行卷积后失真。





注意。 FFT和I-FFT与相同的库完美配合。





注意事项-2。



然后你应该注意到一些尖锐的白色/黑色边缘。这些是由输出值中的包装引起的。为避免这种情况,您可能希望在逆变换后重新调整输出以适应可用的比例,例如:

  double maxAmp = 0.0; 
for(int i = 0; i< imageWidth; i ++)
{
for(int j = 0; j< imageHeight; j ++)
{
maxAmp = Math.Max(maxAmp,convolve [i,j] .Magnitude);
}
}
double scale = 255.0 / maxAmp;
for(int i = 0; i< imageWidth; i ++)
{
for(int j = 0; j< imageHeight; j ++)
{
convolve [i,j] = new Complex(convolve [i,j] .Real * scale,convolve [i,j] .Imaginary * scale);
maxAmp = Math.Max(maxAmp,convolve [i,j] .Magnitude);
}
}

这应该会给出更合理的输出:





但是,这仍然没有在您的书中描述。此时我们有一个2D循环卷积。要获得2D线性卷积,您需要确保图像都填充到维度的总和

  Bitmap lena = inputImagePictureBox.Image as Bitmap; 
Bitmap mask = paddedMaskPictureBox.Image as Bitmap;

位图paddedLena = ImagePadder.Pad(lena,lena.Width + mask.Width,lena.Height + mask.Height);
Bitmap paddedMask = ImagePadder.Pad(mask,lena.Width + mask.Width,lena.Height + mask.Height);

复杂[,] cLena = ImageDataConverter.ToComplex(paddedLena);
Complex [,] cPaddedMask = ImageDataConverter.ToComplex(paddedMask);

复杂[,] cConvolved = Convolution.Convolve(cLena,cPaddedMask);

当您调整填充时,您可能希望将填充颜色更改为黑色,否则您的填充将本身在两个图像之间引入了很大的相关性:

 公共类ImagePadder 
{
公共静态位图Pad(Bitmap maskImage,int newWidth,int newHeight)
{
...
Grayscale.Fill(resizedImage,Color.Black);

现在你应该得到以下信息:





我们越来越接近,但自相关结果的峰值不在中心,这是因为你在正向变换中 FourierShifter.FFTShift 但是在逆变换中不调用相应的 FourierShifter.RemoveFFTShift 。如果我们调整那些(在 ForwardFFT 中删除​​ FFTShift ,或添加 RemoveFFTShift in InverseFFT ),然后我们终于得到:




I want to Convolve Lena with itself in the Frequency Domain. Here is an excerpt from a book. which suggests how should the output of the convolution be:

I have written the following application to achieve the Convolution of two images in the frequency domain. The steps I followed are as follows:

  1. Convert Lena into a matrix of complex numbers.
  2. Apply FFT to obtain a complex matrix.
  3. Multiply two complex matrices element by element (if that is the definition of Convolution).
  4. Apply IFFT to the result of the multiplication.

The output seems to be not coming as expected:

Two issues are visible here:

  • The output only contains a black background with only one dot at its center.
  • The original image gets distorted after the the execution of convolution.

.

Note. FFT and I-FFT are working perfectly with the same libraries.

Note-2. There is a thread in SO that seems to be discussing the same topic.

.

Source Code:

public static class Convolution
{
    public static Complex[,] Convolve(Complex[,]image, Complex[,]mask)
    {
        Complex[,] convolve = null;

        int imageWidth = image.GetLength(0);
        int imageHeight = image.GetLength(1);

        int maskWidth = mask.GetLength(0);
        int maskeHeight = mask.GetLength(1);

        if (imageWidth == maskWidth && imageHeight == maskeHeight)
        {
            FourierTransform ftForImage = new FourierTransform(image); ftForImage.ForwardFFT();
            FourierTransform ftForMask = new FourierTransform(mask); ftForMask.ForwardFFT();

            Complex[,] fftImage = ftForImage.FourierTransformedImageComplex;                
            Complex[,] fftKernel = ftForMask.FourierTransformedImageComplex;

            Complex[,] fftConvolved = new Complex[imageWidth, imageHeight];

            for (int i = 0; i < imageWidth; i++)
            {
                for (int j = 0; j < imageHeight; j++)
                {
                    fftConvolved[i, j] = fftImage[i, j] * fftKernel[i, j];
                }
            }

            FourierTransform ftForConv = new FourierTransform();
            ftForConv.InverseFFT(fftConvolved);
            convolve = ftForConv.GrayscaleImageComplex;

            //convolve = fftConvolved;
        }
        else
        {
            throw new Exception("padding needed");
        }

        return convolve;
    }
}

    private void convolveButton_Click(object sender, EventArgs e)
    {
        Bitmap lena = inputImagePictureBox.Image as Bitmap;
        Bitmap paddedMask = paddedMaskPictureBox.Image as Bitmap;

        Complex[,] cLena = ImageDataConverter.ToComplex(lena);
        Complex[,] cPaddedMask = ImageDataConverter.ToComplex(paddedMask);

        Complex[,] cConvolved = Convolution.Convolve(cLena, cPaddedMask);

        Bitmap convolved = ImageDataConverter.ToBitmap(cConvolved);

        convolvedImagePictureBox.Image = convolved;
    }

解决方案

There is a difference in how you call InverseFFT between the working FFT->IFFT application, and the broken Convolution application. In the latter case you do not pass explicitly the Width and Height parameters (which you are supposed to get from the input image):

public void InverseFFT(Complex[,] fftImage)
{
    if (FourierTransformedImageComplex == null)
    {
       FourierTransformedImageComplex = fftImage;
    }

    GrayscaleImageComplex = FourierFunction.FFT2D(FourierTransformedImageComplex, Width, Height, -1);

    GrayscaleImageInteger = ImageDataConverter.ToInteger(GrayscaleImageComplex);
    InputImageBitmap = ImageDataConverter.ToBitmap(GrayscaleImageInteger);
}

As a result both Width and Height are 0 and the code skips over most of the inverse 2D transformation. Initializing those parameters should give you something which is at least not all black.

    if (FourierTransformedImageComplex == null)
    {
        FourierTransformedImageComplex = fftImage;
        Width = fftImage.GetLength(0);
        Height = fftImage.GetLength(1);
    }

Then you should notice some sharp white/black edges. Those are caused by wraparounds in the output values. To avoid this, you may want to rescale the output after the inverse transform to fit the available scale with something such as:

double maxAmp = 0.0;
for (int i = 0; i < imageWidth; i++)
{
    for (int j = 0; j < imageHeight; j++)
    {
        maxAmp = Math.Max(maxAmp, convolve[i, j].Magnitude);
    }
}
double scale = 255.0 / maxAmp;
for (int i = 0; i < imageWidth; i++)
{
    for (int j = 0; j < imageHeight; j++)
    {
        convolve[i, j] = new Complex(convolve[i, j].Real * scale, convolve[i, j].Imaginary * scale);
        maxAmp = Math.Max(maxAmp, convolve[i, j].Magnitude);
    }
}

This should then give the more reasonable output:

However that is still not as depicted in your book. At this point we have a 2D circular convolution. To get a 2D linear convolution, you need to make sure the images are both padded to the sum of the dimensions:

Bitmap lena = inputImagePictureBox.Image as Bitmap;
Bitmap mask = paddedMaskPictureBox.Image as Bitmap;

Bitmap paddedLena = ImagePadder.Pad(lena, lena.Width+ mask.Width, lena.Height+ mask.Height);
Bitmap paddedMask = ImagePadder.Pad(mask, lena.Width+ mask.Width, lena.Height+ mask.Height);

Complex[,] cLena = ImageDataConverter.ToComplex(paddedLena);
Complex[,] cPaddedMask = ImageDataConverter.ToComplex(paddedMask);

Complex[,] cConvolved = Convolution.Convolve(cLena, cPaddedMask);

And as you adjust the padding, you may want to change the padding color to black otherwise your padding will in itself introduce a large correlation between the two images:

public class ImagePadder
{
    public static Bitmap Pad(Bitmap maskImage, int newWidth, int newHeight)
    {
        ...
        Grayscale.Fill(resizedImage, Color.Black);

Now you should be getting the following:

We are getting close, but the peak of the autocorrelation result is not in the center, and that's because you FourierShifter.FFTShift in the forward transform but do not call the corresponding FourierShifter.RemoveFFTShift in the inverse transform. If we adjust those (either remove FFTShift in ForwardFFT, or add RemoveFFTShift in InverseFFT), then we finally get:

这篇关于频域图像卷积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆