Thursday, March 29, 2012

Revisiting Lens Effect / Bokeh - Details

Hi
just finished doing another test with this technique, and even test it on my old card... and it rocks, can't wait to inject it to the engine (its still a render monkey tester).
anyway, someone asked for some details about this technique, so here is the post that i hope will clear few things.

let me start with example with steps:
* we have image A
* we want new image B (blurred)
* so we apply blur filter on A and get B
the tricky part is the blur filter.

if we want to apply simple blur, lets say we take 4 samples, so we will sample four locations around our current pixel and do some average to get our new pixel, this is the basic of blurring. in this example we say the kernel/radius size is 1, because we sample at distance of one pixel from our current pixel.

if we want to control the radius and make it more then 1 pixel, we could manually change our samples locations, but a better way would be to set our samples as directions/vectors from our current pixel to the sample location we would like to sample, so we could scale it and sample at larger distance from our current pixel.

some numbers to show what i mean:
if we have an image with 3x3 pixels, and we are at pixel (1,1), our absolute samples locations will be:
(0,0), (2,0) - top
(0,2), (2,2) - bottom
but when we convert these to directions, we get:
(-1,-1), (1,-1) - top
(-1,1), (1,1) - bottom

NOTE: these directions are simply added to our current pixel location to get the right sample location.
the directions format is better since it allows as to scale them and sample at larger distance and its keeps the code more generic.
keep in mind that these directions will be good for radius 1 or 2, larger values results in artifacts dues to not enough samples around our pixel.
to use large values, you want more samples, but more samples means more gpu power, so you want to find nice balance between quality and speed.

anyway, how could we get shape like blurs? like circles, squares, pentagon etc..
the same idea we did in the simple case, we create a set of directions that creates or match the shape we want and apply it, we need to use more samples so our shape will maintain its shape.

one thing you should know is that when you sample and gather pixels almost all the time you will apply some kind of weight on them so each pixel will have its contribution (you may not want that every pixel will have the same contribution...).

here is a new video with better quality that shows this technique in action.
the test uses 2 inputs to emulate simple depth of field effect:
* first input is color image of a really nice car :)
* second is CoC image that represent the circle of confusion which in turn will change our radius when we blur (created in photoshop to apply dof on specific areas in the image)



No comments: