具有FFT问题的高斯模糊 [英] Gaussian Blur with FFT Questions

查看:189
本文介绍了具有FFT问题的高斯模糊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用常规卷积来实现高斯模糊的当前实现。对于小内核来说它足够有效,但是一旦内核的大小变得更大,性能就会受到影响。所以,我正在考虑使用FFT实现卷积。我从未有过与FFT相关的图像处理经验,所以我有几个问题。

I have a current implementation of Gaussian Blur using regular convolution. It is efficient enough for small kernels, but once the kernels size gets a little bigger, the performance takes a hit. So, I am thinking to implement the convolution using FFT. I've never had any experience with FFT related image processing so I have a few questions.


  1. 是基于2D FFT的卷积也可分为两个1D卷积?

  1. Is a 2D FFT based convolution also separable into two 1D convolutions ?


  • 如果为真,它是这样的 - 每行1D FFT,然后每个1D FFT列,然后与2D内核相乘,然后每列的逆变换和每行的逆变换?或者我必须在每次1D FFT变换后乘以1D内核?

现在我明白内核大小应该与图像大小相同(1D时为行)。但它会如何影响边缘?我是否必须用零填充图像边缘?如果是这样,内核大小应该等于填充之前或之后的图像大小?

Now I understand that the kernel size should be the same size as the image (row in case of 1D). But how will it affect the edges? Do I have to pad the image edges with zeros? If so the kernel size should be equal to the image size before or after padding?

此外,这是一个C ++项目,我打算使用kissFFT,因为这是一个商业项目。欢迎您提出更好的选择。谢谢。

Also, this is a C++ project, and I plan on using kissFFT, since this is a commercial project. You are welcome to suggest any better alternatives. Thank you.

编辑:感谢您的回复,但我还有一些问题。

Thanks for the responses, but I have a few more questions.


  1. 我看到输入图像的虚部将全部为零。但是输出虚部也会是零吗?我是否必须将高斯核与实部和虚部相乘?

  1. I see that the imaginary part of the input image will be all zeros. But will the output imaginary part will also be zeros? Do I have to multiply the Gaussian kernel to both real and imaginary parts?

我有相同图像的实例在不同尺度上模糊,即相同图像缩放到不同的大小,并在不同的内核大小模糊。每次缩放图像时都必须执行FFT吗?或者我可以使用相同的FFT吗?

I have instances of the same image to be blurred at different scales, i.e. the same image is scaled to different sizes and blurred at different kernel sizes. Do I have to perform a FFT every time I scale the image or can I use the same FFT?

最后,如果我想要显示FFT,我了解必须将日志过滤器应用于FFT。但我真的迷失在哪个部分应该用于可视化FFT?实部或虚部。

Lastly, If I wanted to visualize the FFT, I understand that a log filter has to be applied to the FFT. But I am really lost on which part should be used to visualize FFT? The real part or the imaginary part.

同样对于尺寸为512x512的图像,实部和虚部的大小是多少。它们的长度是否相同?

Also for an image of size 512x512, what will be the size of real and imaginary parts. Will they be the same length?

再次感谢您的详细回复。

Thank you again for your detailed replies.

推荐答案


  1. 2-D FFT是可分离的,除了你必须乘以2D内核的二维FFT。如果您使用的是kissfft,执行二维FFT的更简单方法是在kissfft包的tools目录中使用 kiss_fftnd 。这将进行多维FFT。

  1. The 2-D FFT is seperable and you are correct in how to perform it except that you must multiply by the 2-D FFT of the 2D kernel. If you are using kissfft, an easier way to perform the 2-D FFT is to just use kiss_fftnd in the tools directory of the kissfft package. This will do multi-dimensional FFTs.

内核大小不必是任何特定大小。如果内核小于图像,则只需在执行二维FFT之前将其填充到图像大小。您还应该对图像边缘进行零填充,因为在频域中通过乘法执行的计算实际上是循环卷积,并且结果在边缘处环绕。

The kernel size does not have to be any particular size. If the kernel is smaller than the image, you just need to zero-pad up to the image size before performing the 2-D FFT. You should also zero pad the image edges since the convoulution being performed by multiplication in the frequency domain is actually circular convolution and results wrap around at the edges.

总结(假设图像尺寸为M x N):

So to summarize (given that the image size is M x N):


  1. 拿出2 -D任何大小的内核(U x V)

  2. 将内核零填充到(M + U-1)x(N + V-1)

  3. 取内核的2-D fft

  4. 将图像零填充到(M + U-1)x(N + V-1)

  5. 采取图像的二维FFT

  6. 通过图像的FFT乘以内核的FFT

  7. 取逆2 -D FFT结果

  8. 修剪边缘的垃圾

  1. come up with a 2-D kernel of any size (U x V)
  2. zero-pad the kernel up to (M+U-1) x (N+V-1)
  3. take the 2-D fft of the kernel
  4. zero-pad the image up to (M+U-1) x (N+V-1)
  5. take the 2-D FFT of the image
  6. multiply FFT of kernel by FFT of image
  7. take inverse 2-D FFT of result
  8. trim off garbage at edges

如果你正在执行相同的操作在不同的图像上多次过滤,你不必每次都执行1-3次。

If you are performing the same filter multiple times on different images, you don't have to perform 1-3 every time.

注意:内核大小必须相比较大,这比直接计算卷积更快。此外,您是否实现了直接卷积,利用了二维高斯滤波器可分离的事实(见此几段进入力学部分)?也就是说,您可以在行和列上执行二维卷积作为一维卷积。我发现这比大多数基于FFT的方法要快,除非内核非常大。

Note: The kernel size will have to be rather large for this to be faster than direct computation of convolution. Also, did you implement your direct convolution taking advantage of the fact that a 2-D gaussian filter is separable (see this a few paragraphs into the "Mechanics" section)? That is, you can perform the 2-D convolution as 1-D convolutions on the rows and then the columns. I have found this to be faster than most FFT-based approaches unless the kernels are quite large.

对编辑的反应


  1. 如果输入是真实的,除极少数情况外,输出仍然很复杂。高斯核的FFT也很复杂,因此乘法必须是复数乘法。当您执行逆FFT时,输出应该是真实的,因为您的输入图像和内核是真实的。输出将以复数数组的形式返回,但虚数组件应为零或非常小(浮点错误),并且可以丢弃。

  1. If the input is real, the output will still be complex except for rare circumstances. The FFT of your gaussian kernel will also be complex, so the multiply must be a complex multiplication. When you perform the inverse FFT, the output should be real since your input image and kernel are real. The output will be returned in a complex array, but the imaginary components should be zero or very small (floating point error) and can be discarded.

如果你使用相同的图像,您可以重复使用图像FFT,但您需要根据最大的内核大小进行零填充。您必须计算所有不同内核的FFT。

If you are using the same image, you can reuse the image FFT, but you will need to zero pad based on your biggest kernel size. You will have to compute the FFTs of all of the different kernels.

对于可视化,应使用复数输出的大小。当较大的组件将它们以线性标度淹没时,对数标度仅有助于可视化输出的较小组件。经常使用 Decibel 比例,并由 20 * log10( abs(x)) 10 * log10(x * x')等效。 ( x 是复杂的fft输出, x' x的复共轭)。

For visualization, the magnitude of the complex output should be used. The log scale just helps to visualize smaller components of the output when larger components would drown them out in a linear scale. The Decibel scale is often used and is given by either 20*log10(abs(x)) or 10*log10(x*x') which are equivalent. (x is the complex fft output and x' is the complex conjugate of x).

FFT的输入和输出大小相同。此外,实部和虚部将具有相同的大小,因为一个实数和一个虚数值构成单个样本。

The input and output of the FFT will be the same size. Also the real and imaginary parts will be the same size since one real and one imaginary value form a single sample.

这篇关于具有FFT问题的高斯模糊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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