线性滤波之平滑滤波

传统的滤波方式将滤波分为线性滤波和非线性滤波,这里首先说下线性滤波。

(1)线性滤波的示例3x3的平均滤波器。




案例如下:
public class ImageJAndroid2Activity extends Activity {
     ImageView sourceImage;
     ImageView destinationImage;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        sourceImage=(ImageView) findViewById(R.id.source);
        destinationImage=(ImageView) findViewById(R.id.destination);
    }
    
    public void remove(View v){
        Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.cc);
        int width=bitmap.getWidth();
        int height=bitmap.getHeight();
        int[] pixel=new int[width*height];
        int[] outpixel=new int[width*height];
        bitmap.getPixels(pixel, 0, width, 0, 0, width, height);
        for(int y=0;y<height;y++){
            for(int x=0;x<width;x++){
                int sumred=0;
                int sumgreen=0;
                int sumblue=0;
                int a=pixel[y*width+x]>>24&0xff;;
                for(int i=-1;i<=1;i++){
                    int newi=y+i;
                    if(newi<0 ||newi>=height){
                        newi=y;
                    }
                    for(int j=-1;j<=1;j++){
                        int newj=x+j;
                        if(newj<0 || newj>=width){
                            newj=x;
                        }
                        int red=pixel[newi*width+newj]>>16&0xff;
                        int green=pixel[newi*width+newj]>>8&0xff;
                        int blue=pixel[newi*width+newj]&0xff;
                        
                        sumred+=red;
                        sumgreen+=green;
                        sumblue+=blue;
                        
                    }
                }
            
                int newred=(int)Math.round(sumred/9.0);
                int newgreen=(int)Math.round(sumgreen/9.0);
                int newblue=(int)Math.round(sumblue/9.0);
                outpixel[y*width+x]=a<<24|newred<<16|newgreen<<8|newblue;
                
            }
        }
        Bitmap newBitmap=Bitmap.createBitmap(width, height,Config.RGB_565);
        newBitmap.setPixels(outpixel, 0, width,0,0, width, height);
        destinationImage.setImageBitmap(newBitmap);
    }
}

效果图:


大家应该看得出来效果吧。


(2)3x3的平滑滤波器


采用这种方式得到的平滑的滤波图像
activity:
public class ImageJAndroid2Activity extends Activity {
     ImageView sourceImage;
     ImageView destinationImage;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        sourceImage=(ImageView) findViewById(R.id.source);
        destinationImage=(ImageView) findViewById(R.id.destination);
    }
    
    //平滑滤波器
    public void remove(View v){
        Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.cc);
        int[][] filter={
                {3,5,3},
                {5,8,5},
                {3,5,3}
        };
        int width=bitmap.getWidth();
        int height=bitmap.getHeight();
        int[] pixel=new int[width*height];
        int[] outpixel=new int[width*height];
        bitmap.getPixels(pixel, 0, width, 0, 0, width, height);
        
        for(int y=0;y<height;y++){
            for(int x=0;x<width;x++){
                int sumred=0;
                int sumgreen=0;
                int sumblue=0;
                int a=0;
                for(int i=-1;i<=1;i++){
                    int newi=y+i;
                    if(newi<0||newi>=height){
                        newi=y;
                    }
                    for(int j=-1;j<=1;j++){
                        int newj=x+j;
                        if(newj<0||newj>=width){
                            newj=x;
                        }
                        a=pixel[newi*width+newj]>>24&0xff;
                        int red=pixel[newi*width+newj]>>16&0xff;
                        int green=pixel[newi*width+newj]>>8&0xff;
                        int blue=pixel[newi*width+newj]&0xff;
                        
                        sumred+=filter[i+1][j+1]*red;
                        sumgreen+=filter[i+1][j+1]*green;
                        sumblue+=filter[i+1][j+1]*blue;
                        
                    }
                }
                outpixel[y*width+x]=a<<24|((int)(sumred/40))<<16|((int)(sumgreen/40))<<8|((int)(sumblue/40));
            }
        }
        Bitmap newBitmap=Bitmap.createBitmap(width, height,Config.RGB_565);
        newBitmap.setPixels(outpixel, 0, width, 0, 0, width, height);
        destinationImage.setImageBitmap(newBitmap);
    }

效果图:

大家会觉得平均滤波与平滑滤波的区别不是蛮大,但是在处理精细图片的时候区别还是蛮蛮明显的。
这里所使用到的是3x3矩阵,因为3x3使用的比较多,除此之外你自己还可以定义任意尺寸的滤波器。例如5x5,,21x21



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值