Friday, July 11, 2008

Gaussian Blur

Ok, since I brought it up, here's the deal with Gaussian blur. First of all, any kind of blur is basically a way of mixing colors together. If we have some black pixels next to some white pixels, and we make the black ones along the edge a little lighter, and the white ones along the edge a little darker ... voila! Blur.

So the basic blur operation is to look at each pixel in turn, and mix in a little color from the surrounding pixels. Typically, we still want the pixel to be mostly its original color, but with some of the surrounding colors mixed in. We can come up with a kind of recipe for doing this .... mix so much of this color, plus so much of that color, stir ... etc. Let's look at a simple example.

Each pixel (except the ones at the edge of the image) has eight neighbors ...


like a tic-tac-toe board. So one way to make a mixing recipe is to figure out a percentage of each neighbor's color to mix into the center pixel's color. Since the left, right, top and bottom neighbors are slightly closer than the diagonal ones, let's take more color from them. So one example recipe could be
In other words, to figure out the new value for the middle pixel, we take 40 per cent of its color, plus 10 percent from each of the pixels above, below, left and right, plus 5 per cent of the diagonal ones.

So, suppose we have an image with a sharp edge where the top is black and the bottom is white, for example:

When we're modifying the black pixels just above the white area, the recipe will look like this:

So, in total, 20 per cent will be white, and 80 per cent will be black. In other words, we change each pixel in the bottom row of black pixels from 100% black to 80% black ... dark gray. Likewise, when we get to the next row, the top row of white pixels, they will go from being 100% white to 80% white ... light gray. The result

contains dark and light gray bands along what was originally a sharp edge. In effect, the edge is blurred. Notice that only the edge is affected. When you apply the recipe to one of the pixels in the middle of the black area, all its neighbors are black too, so the result will be ... black!

Of course, that's a very simple example. In practice, we'd use a recipe, called a kernel, that covers a lot more pixels, so for each pixel we're considering, we'd mix in a little color from pixels 2, 3, 4 or more away. This is what the radius setting in Photoshop's Gaussian blur function is for.

So, why's it called Gaussian? Because this kind of recipe, which could be visualized like this

is modeled on a mathematical function called a Gaussian distribution, named after the mathematician Carl Friedrich Gauss. Gauss was a brilliant guy. The story is that when one of his teacher's tried to challenge him by asking him to add all the numbers from 1 to 100, he thought about it for a moment, invented a quick way to solve this, and responded almost immediately with the answer. Of course, it's 5,050, as you knew, right?

No comments: