Advertisement Jump to content


This topic is now archived and is closed to further replies.


Q. 2D image Filtering algorithm/Theory

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Had a superquick lesson today about filtering techniques, and the guy showed some code. I''m looking for image filtering algorithms, and the theory behind them ... how contrast/sharpening/blur works, histograms of a picture, the theory behind them, and maybe some cpp code ... most stuff I have found so far is just ... messy, overcommented, and goes quickly over into superadvanced high-tech stuff ... and reading mathlab code at that level is just hard ... when I''m a medium newb c++ guy ... Maby stuff that would be relevant towards "photoshop" like stuff ( simple ofcourse ) ... and maybe to ad effects into movies ... I''m talking easy stuff ... simple motion blur maybe .. thx

Share this post

Link to post
Share on other sites
Most stuff uses a simple array that is applied to every pixel in the image. I believe Photoshop has something like that (I use PSP7; I''m not too sure about Photoshop''s effects).

You can easily do a variety of things by playing around with individual pixels, if that''s the kind of image processing you want to do. This is, obviously, way too slow for use in a game though; you''d make use of other techniques for that.

For instance, to blur, you could cross your entire screen and average the current pixels and its neighbours (for instance, p(x,y)+p(x-1,y)+p(x+1,y)+p(x,y-1)+p(x,y+1) / 5).

Share this post

Link to post
Share on other sites
I found this quite a nice intro, not much in the way of code but goes over the fundamental maths behind image processing.

[edited by - JuNC on March 5, 2004 7:45:40 AM]

Share this post

Link to post
Share on other sites
Don''t worry about games ... I haven''t thought of using them there, it''s just for playing aroud simply. Learn.

Nice link ... I found a similar page, but not that good. I could also have a use of code''s and stuff.

I have noticed that they apply different math techniques to image prosessing. My teatcher used a floting point from 0 to 1 (black/White), I also see some useing bytevalues, and other forms ...

The little code I was presented looked like this:

Create a greyscale pic:
float r, g, b, w;
for( int x=0; x < width - 1; x++ )
for( int y=0; y < height - 1; y++ )
GetRGB(x,y,r,g,b) // r,g,b get the pixel value from x,y
w = ( r + b + g ) /3;
PutRGB(x,y,w,w,w) // put the value back to a new pic

This was done in a lua programming interface, so don''t put to much into the code...

Then there was contrast/brightnes ... and blur .. but didn''t got the code (fast guy)

Share this post

Link to post
Share on other sites
There''s that, too: the simpler ones that allow you to play around with the color.

Take the pixel, extract the R, G and B values, then play with ''em.

For greyscale, you''d just set R, G and B to "(R + G + B) / 3".
For a red tint, you''d set G and B to 0.
For a blue tint, you''d set R and G to 0.

It''s a pretty limited technique though, and you''ll soon find yourself branching out to the neighbouring pixels for guidance.

Share this post

Link to post
Share on other sites
If I want to dick around with algorithms without worrying about precision I usually use LTI-lib with floating point image buffers (open source library with lots of nice helper routines, only problem is that template instanciation creates a truly garganteum static library ... 80 MB, but your program will only include a very small percentage of that of course). Working with bytes is for when you have the need for speed, but the limited dynamic range and wrap around of numbers distracts if you are just trying to look at what a filter does.

For grey scale you are better off with the Y from the YUV color space ... if you take the unweighted average of the components then colors with lots of blue will look far too bright.

Contrast and brightness are overloaded terms, but for the simplest filters contrast simply scales the luminance (Y) of pixels and brightness offsets it. Histogram equalization can also increase contrast, but it isnt really suited to video (or at least you cant apply it seperately to each frame).

Simple sharpening and blurring is indeed done with convolution as RuneLancer implied, take a look at for an explanation on unsharp masking and gaussian blur. (The other link described them too, but it didnt give the convolution masks ... and looking at them gives you a far more intuitive understanding of what the filter does.)

The biggest problem with using convolution/linear-filters is the way edges are handled. With linear filters sharpening will induce ringing near edges, and blur will blur across edges (which you dont always want, for instance if you want to simplify the structure of the image you will usually want to retain strong edges in their original form). Also denoising images contaminated with multiplicative/impulsive noise is impossible to do well with linear filters for instance.

There is NOTHING simple about motion blur, at least not if you want to do it halfway decently.

[edited by - PinkyAndThaBrain on March 6, 2004 3:36:15 AM]

[edited by - PinkyAndThaBrain on March 6, 2004 3:37:14 AM]

Share this post

Link to post
Share on other sites
Of course you can alter an image in numerous way, and one could say that the only limit is your imagination. However, some often used basic methodology can still found:

1.) Kernel filters. In this you store a 3x3 or a 5x5 matrix of values, and for each pixel grab it''s neighbouring pixels multiplied by the filter matrix''s corresponding values. The filter sometimes comes with a factor (by which you multiplie/divide the final result, although this factor isn''t necessarily needed...) and a bias-value (which you add to the final result).

2.) Distortion effects. Here each pixel in the new image is set to be another pixel in the old image. Assume, for example, "twisting" the image: here, for each new pixel, you grab another pixel in the old image by rotating the coordinate of the pixel by some nice amount (for example, the distance to the midpoint of the image). A little formula might explain it better:

newImage[x,y] = oldImage[ AlterCoordinates(x,y) ]

(notice that AlterCoordinates returns floating point coordinates: you should apply interpolation to find the pixel value in the old image)

3.) Then you can, for example construct a version of algorithm (2.) where you take a (possibly weighted) integral of pixels in the old image to construct a new pixel (this is a very "general" technique of course: even blurs can be done using it). It''s also a very abstract technique, so implementation issues vary largely (eg. doing a 1d vs. 2d integral). If you want speed, there are often simpler techniques available for certain effects, possibly with a little error term though.

- Mikko Kauppila

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!