# Lens blur in frequency space

This topic is 1898 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey

I've read somewhere about fft bokeh (lens) blur. I'm trying to implement it. I'm totally new to fft and stuff.

I managed to successfully fft an image, I have an 2d array of complex numbers.. when I do a backward fft, I get the same image as before, so it works.

How can I filter it to get a bokeh effect? Edited by MrOMGWTF

##### Share on other sites
In theory you compute the fft of your filter upfront and then do an element wise multiplication of the ffted image and the ffted filter. The result, when transformed back, should be identical to what you would have gotten, if you had just applied the filter without any ffts.
Doing the filtering in the frequency domain (applying the fft and everything) is usually only beneficial if your filter kernel is very big. I am not sure that bokeh effects qualify for this.

##### Share on other sites
It's also important to note that if your blur pattern can be decomposed as

F(x,y) = g(x)*h(y)

you can apply the filter one axis at a time, which is usually much faster than applying a 2D filter. This is particularly relevant because a Gaussian filter fits this pattern.

##### Share on other sites

This is particularly relevant because a Gaussian filter fits this pattern.

Gaussian blur isn't appropriate/correct for bokeh, though.

##### Share on other sites

[quote name='Álvaro' timestamp='1352593684' post='4999769']
This is particularly relevant because a Gaussian filter fits this pattern.

Gaussian blur isn't appropriate/correct for bokeh, though.
[/quote]

Sure. It's still good to remind people of this trick, since we are talking about implementing filters.

##### Share on other sites
As the above poster mentioned you need to convert your kernel to the frequency domain using an FFT, after which you can convolve your kernel with the source image by performing a complex multiply of image * kernel for each pixel. You have to be careful though when using a cyclic FFT (such as the one used in the CUDA FFT library), since it means you need to cyclically shift your kernel.

Honestly though I'm not sure that frequency domain is really so great for DOF. FFT is certainly not free, so you generally need to be using a really wide filter for it to be worth the cost of converting to and from the frequency domain. Plus FFT needs to work on power-of-2 dimensions, so you need to deal with that. However the biggest problem is that for a filter-based DOF to look decent you need the ability to not filter across depth discontinuities, in order to avoid background bleeding onto the foreground and other similar artifacts. This isn't simple to do in the frequency domain. Plus you really want to vary the kernel size per pixel based on the depth, which also isn't simple.

##### Share on other sites

However the biggest problem is that for a filter-based DOF to look decent you need the ability to not filter across depth discontinuities, in order to avoid background bleeding onto the foreground and other similar artifacts. This isn't simple to do in the frequency domain. Plus you really want to vary the kernel size per pixel based on the depth, which also isn't simple.

I heard that it's possible to use a 3-dimensional FFT where the depth is an additional dimension, if you want to use a FFT for your depth of field implementation. But that would probably not be worth it though Edited by CryZe

##### Share on other sites
My understanding is that there are FFT algorithms for sizes that are not powers of 2. I don't know if I ever knew the details, but for instance FFTW can perform FFT on any size.

##### Share on other sites

My understanding is that there are FFT algorithms for sizes that are not powers of 2. I don't know if I ever knew the details, but for instance FFTW can perform FFT on any size.

Indeed there are, but they are slower. FFTW switches algorithms when you use dimensions that aren't a power of 2.

##### Share on other sites

Have you tried anisotropic diffusion? From experience it's fast, has a constant time per pixel variable size blur, and isn't too hard to implement.

You will need to manually add in bokeh shapes.

I've also wanted to try out this adaptive manifold bilateral filter approximation. Think it could work for Dof.