View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Lens blur in frequency space

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

### #1MrOMGWTF  Members

Posted 10 November 2012 - 05:32 AM

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, 10 November 2012 - 07:05 AM.

### #2Ohforf sake  Members

Posted 10 November 2012 - 05:16 PM

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.

### #3Álvaro  Members

Posted 10 November 2012 - 06:28 PM

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.

### #4l0calh05t  Members

Posted 10 November 2012 - 07:52 PM

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

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

### #5Álvaro  Members

Posted 10 November 2012 - 08:14 PM

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

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

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

### #6MJP  Moderators

Posted 11 November 2012 - 01:21 AM

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.

### #7CryZe  Members

Posted 11 November 2012 - 07:57 AM

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, 11 November 2012 - 09:18 AM.

### #8Álvaro  Members

Posted 11 November 2012 - 08:20 AM

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.

### #9MJP  Moderators

Posted 11 November 2012 - 01:16 PM

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.

### #10jameszhao00  Members

Posted 11 November 2012 - 03:19 PM

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.