Smoothing and anti-aliasing

Aliasing arises if you want to show a structure that varies rapidly within the distance between pixels. Then you will see artefacts that are quite different. We can use this to get new strange images such as I showed in the post “Wallpaper 2.0“. But often we prefer to get rid of aliasing by smoothing the image, as in “fractals in high resolution“. Here I will discuss this in more detail and present a more efficient method. Its code will be part of one of the next posts.

You can see a similar effect in films of wheels with many spokes. When they begin to accelerate you see first that their rotational speed increases as expected. But at very high speeds they seem to become slower even though they still accelerate. Then they even apparently stand still, if they rotate fast enough to exchange spokes at the same place within the time of one image frame.

Lets look at an image showing ripples, that become narrower going from the left to the right. Its length is 600 pixels and the height 100 pixels. The red, green and blue color channels have intensities going from 0 to 255 and i is the horizontal and j the vertical position of a pixel. For the color components I used the equations

red(i,j)=128*(1+cos(2*π*i*(101-j)/600)),

green(i,j)=128 and

blue(i,j)= 255*sq(cos(π*i*j/600)).

We expect that this gives broad bands of color at the left, which become narrower going to the right. At the right we have very fine bands and naïvely one would expect that the oscillations of the cosine functions average out. This would result in red=green=blue=125 and give a grey color. But simply looking up the colors for the pixels at their coordinates (i,j) gives strong aliasing. The image at the right thus looks the same as at the left:

antiAliasOhne

Color ripples that become narrower going from the left to the right. Their width seems to increase again at the right side because of aliasing.

At the right the cosine functions simply make one extra oscillation going from one pixel to the next in vertical direction. But this has no effect on the function values and thus the ripples reappear.

We can decrease aliasing by sampling the image at smaller distances. Thus we get an image of higher resolution with less aliasing. Then we have to decimate the resolution to fit it to the real pixel size. This is the general idea. Here we sample at half the distance between the pixels and use different methods to get the pixel colors.

As in “fractals in high resolution” we can average the colors of four points around the pixel at (i,j). Then

pixel(i,j)=(point(i+¼,j+¼)+point(i-¼,j+¼)+point(i+¼,j-¼)+point(i-¼,j-¼))/4

and much of the aliasing vanishes

antiAliasBlock

Averaging using four points around each pixels partially removes aliasing.

We can slightly improve on this with the same computational cost. The grid of double resolution is now centered on the pixels and we use a weighted averaging

pixel(i,j)=(4*point(i,j)+2*point(i+½,j)+2*point(i-½,j)+2*point(i,j+½)+2*point(i,j-½)

+point(i+½,j+½)+point(i-½,j+½)+point(i+½,j-½)+point(i-½,j-½))/16.

Now aliasing is further decreased:

antiAliasTent

Improved anti-aliasing with a weighted average over 9 points.

Clearly, this can improve images. But aliasing is not fully eliminated, only postponed. Decreasing further the period of the oscillations by an factor of two we get strong aliasing on the sampling grid, which cannot be eliminated by averaging. You see this in an image with a width of 1200 pixels (here shown at reduced scale, click on it to see it full-scale):

tentAliasLarge

Aliasing reappears in spite of the averaging if the oscillation frequency is doubled. Click on this image to see it full-scale.

This entry was posted in Extra, programming and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s