Box Filtering

7 October 2007, 02:57 AM

*image sample*and the

*filter kernel*are multiplied to get the

*filtering result*. The filter kernel is like a description of how the filtering is going to happen, it actually defines the type of filtering. The power of box filtering is one can write a general image filter that can do sharpen, emboss, edge-detect, smooth, motion-blur, etcetera. Provided approriate filter kernel is used.

Now that I probably had wet your appetite let us see further the coolness of box filtering and its filter kernel. A filter kernel defines filtering type, but what exactly is it? Think of it as a fixed size small box or window larger than a pixel. Imagine that it slides over the sample image through all positions. While doing so, it constantly calculates the average of what it sees through its window.

The minimum standard size of a filter kernel is 3x3, as shown in above diagram. Due to the rule that a filter kernel must fit within the boundary of sampling image, no filtering will be applied on all four sides of the image in question. With special treatment, it can be done, but what is more important than making the basic work first? Enough talk, lets get to the implementation asap!

publicint[] BoxFiltering(int[] pixels,intwidth,intheight,float[] kernel) {int[] temp =newint[width*height] ;floatdenominator = 0.0f ;floatred, green, blue ;intired, igreen, iblue, indexOffset, rgb ;int[] indices = { -(width + 1), -width, -(width - 1), -1, 0, +1, width - 1, width, width + 1 } ;for(inti=0;i<kernel.length;i++) denominator += kernel[i] ;if(denominator==0.0f) denominator = 1.0f ;for(inti=1;i<height-1;i++) {for(intj=1;j<width-1;j++) { red = green = blue = 0.0f ; indexOffset = (i*width)+j ;for(intk=0;k<kernel.length;k++) { rgb = pixels[indexOffset+indices[k]] ; red += ((rgb & 0xff0000)>>16)*kernel[k] ; green += ((rgb & 0xff00)>>8)*kernel[k] ; blue += (rgb & 0xff)*kernel[k] ; } ired = (int)(red / denominator) ; igreen = (int)(green / denominator) ; iblue = (int)(blue / denominator) ;if(ired>0xff) ired = 0xff ;elseif(ired<0) ired = 0 ;if(igreen>0xff) igreen = 0xff ;elseif(igreen<0) igreen = 0 ;if(iblue>0xff) iblue = 0xff ;elseif(iblue<0) iblue = 0 ; temp[indexOffset] = 0xff000000 | ((ired<<16) & 0xff0000) | ((igreen<<8) & 0xff00) | (iblue & 0xff) ; } }returntemp ; }

**Java code explanation**

The parameter pixels is an array containing a total of width*height pixel information. The parameter kernel is an array with a fixed length of nine. This is because the implementation assumes a window of size 3x3. The function also assume each pixel is in the form ARGB (alpha, red, green, blue), where blue is the least significant byte. Alpha is the transparency information and should just be left untouched. The array indices is a simple optimization table used to find neighboring pixels. The denominator is the sum of parameter kernel, it will be the denominator when calculating average.

Check images below to see different kind of kernels gives varying results. Sample image is from MMORPG game Lineage II.

**Original unfiltered image.**

**Smoothing**

**Sharpening**

**Raised**

**Motion-blur**

**Edge detection**

See Box Filtering Java applet interactive demo.

Last edited on 29 March 2009

### Comments

~

**John**, 24 April 2010, 11:50 PMHi guest,

If you meant the paper from

http://www.sciencedirect.com/science?_ob=ArticleURL&_udi=B7GXF-4KHBY6B-7&_user=10&_coverDate=09%2F30%2F1981&_rdoc=1&_fmt=high&_orig=search&_sort=d&_docanchor=&view=c&_searchStrId=1309228100&_rerunOrigin=google&_acct=C000050221&_version=1&_urlVersion=0&_userid=10&md5=18deed3e12377661d566a6c338f23223

then I cannot say because I don't have access to it. However the "Box Filtering" concept as in filter kernel is probably the same except for the difference in implementation (maybe other than 3x3?).

If you meant the paper from

http://www.sciencedirect.com/science?_ob=ArticleURL&_udi=B7GXF-4KHBY6B-7&_user=10&_coverDate=09%2F30%2F1981&_rdoc=1&_fmt=high&_orig=search&_sort=d&_docanchor=&view=c&_searchStrId=1309228100&_rerunOrigin=google&_acct=C000050221&_version=1&_urlVersion=0&_userid=10&md5=18deed3e12377661d566a6c338f23223

then I cannot say because I don't have access to it. However the "Box Filtering" concept as in filter kernel is probably the same except for the difference in implementation (maybe other than 3x3?).

~

**P**, 29 January 2012, 12:58 AMHi, you use Lineage2 art image. Very impressive.

Can I translate this blog into Korean and post my blog? I will let you know the link and give you credit. :)

Can I translate this blog into Korean and post my blog? I will let you know the link and give you credit. :)

~

**John**, 30 January 2012, 11:40 PMHi P,

Sure why not!

:)

Sure why not!

:)

~

**P**, 31 January 2012, 06:55 PMI posted the blog at http://parkpd.egloos.com/3800302

Thanks for your interesting blog. :)

Thanks for your interesting blog. :)

~

**J**, 2 March 2013, 06:29 AMhey can i have a pdf for McDonnells book or other articles for box filtering or even an implementation in C or such, i just need the algorithm.. Im working on stereo vision, thanks. :)

~

**Johan**, 15 June 2014, 01:12 AMI am unfortunately not into Java. I use C.

Can somebody please explain to me the indices array and its use? The rest of this impressive article I can understand.

Please be so kind as to email to smitjs@vodamail.co.za in case I dont see ny reply.

Can somebody please explain to me the indices array and its use? The rest of this impressive article I can understand.

Please be so kind as to email to smitjs@vodamail.co.za in case I dont see ny reply.

~

**Johan**, 15 June 2014, 02:03 AMSorry, please ignore my ignorant previous post.

I now see that the indices array simply gives one the pixel at (x+1,y-1),(x,y-1),x-1,y-1) etc.

I am amazed that it took so long to realize this.

Well, we cannot be perfect all the time.

Still, the article is most informative, thank you

Johan Smit

I now see that the indices array simply gives one the pixel at (x+1,y-1),(x,y-1),x-1,y-1) etc.

I am amazed that it took so long to realize this.

Well, we cannot be perfect all the time.

Still, the article is most informative, thank you

Johan Smit

~

**Fab**, 29 January 2015, 10:01 AMHi,

May I know the format your pixels are in? Top left to right and top to bottom?

Meaning an image:

1 2 3

4 5 6

7 8 9

would be represented as [1,2,3,4,5,6,7,8]?

Similarly for the kernels we wish to pass in as inputs?

May I know the format your pixels are in? Top left to right and top to bottom?

Meaning an image:

1 2 3

4 5 6

7 8 9

would be represented as [1,2,3,4,5,6,7,8]?

Similarly for the kernels we wish to pass in as inputs?

Post a comment~

guest, 17 April 2010, 10:46 PMComputer Graphics and Image Processing, Volume 17, Issue 1, Pages 65-70

M. McDonnell".