Box Filtering

 转自:http://tech-algorithm.com/articles/boxfiltering/

Box filtering is basically an average-of-surrounding-pixel kind of image filtering. It is actually a convolution filter which is a commonly used mathematical operation for image filtering. A convolution filters provide a method of multiplying two arrays to produce a third one. In box filtering, image sample and the filter kernel are multiplied to get thefiltering 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!

public int[] BoxFiltering(
        int[] pixels,int width, int height, float[] kernel) {
    int[] temp = new int[width*height] ;
    float denominator = 0.0f ;
    float red, green, blue ;
    int ired, igreen, iblue, indexOffset, rgb ;
    int[] indices = {
        -(width + 1),  -width,     -(width - 1), 
        -1,                0,           +1, 
        width - 1,      width,      width + 1
    } ;
    for (int i=0;i<kernel.length;i++)
        denominator += kernel[i] ;
    if (denominator==0.0f) denominator = 1.0f ;
    for (int i=1;i<height-1;i++) {
        for (int j=1;j<width-1;j++) {
            red = green = blue = 0.0f ;
            indexOffset = (i*width)+j ;
            for (int k=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 ;
                else if (ired<0) ired = 0 ;
            if (igreen>0xff) igreen = 0xff ;
                else if (igreen<0) igreen = 0 ;
            if (iblue>0xff) iblue = 0xff ;
                else if (iblue<0) iblue = 0 ;            
            temp[indexOffset] = 0xff000000 | ((ired<<16) & 0xff0000) | 
                    ((igreen<<8) & 0xff00) | (iblue & 0xff) ;
        }
    }
    return temp ;
}


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.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值