线性滤波之线性卷积

线性卷积的概念:

http://blog.csdn.net/cszhangchao/article/details/8852363

这里采用的卷积算子是:

float[][]  h={

  {0.075,0.125,0.075},

  {0.125,0.2,0.125},

  {0.075,0.125,0.075}

};

线性卷积的步骤是

(1):将卷积算子围绕中心旋转180度。

(2)滑动卷积算子使其中心位于图像像素上

(2)加权和计算像素之和赋值给中心的像素点。


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);
        //卷积算子
        float[] filter={
                0.075f,0.125f,0.075f,
                0.125f,0.2f,0.125f,
                0.075f,0.125f,0.075f
        };
        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);
        
        //卷积算子旋转180
        for(int i=0;i<filter.length;i++){
              float t=filter[i];
              filter[i]=filter[filter.length-1-i];
              filter[filter.length-1-i]=t;
              
        }
        
        //计算卷积
        for(int y=0;y<height;y++){
            for(int x=0;x<width;x++){
                  int a=pixel[y*width+x]>>24&0xff;;
                  int sumred=0;
                  int sumgreen=0;
                  int sumblue=0;
                  int index=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;
                          }
                        
                          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[index]*red;
                          sumgreen+=filter[index]*green;
                          sumblue+=filter[index]*blue;
                          index++;
                      }
                      
                  }
                outpixel[y*width+x]=a<<24|sumred<<16|sumgreen<<8|sumblue;
            }
        }
        Bitmap newBitmap=Bitmap.createBitmap(width, height, Config.RGB_565);
        newBitmap.setPixels(outpixel, 0, width, 0, 0, width, height);
        destinationImage.setImageBitmap(newBitmap);
    }


图像效果:

由于使用的卷积算子的特殊原因旋转180度之后,该卷积算子没有变。所以得到的图像效果跟前面的平滑滤波的效果是一样。这里只是介绍线性卷积滤波的使用方式。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值