高斯二维滤波

高斯二维滤波的步骤:

1:生成高斯二维算子

2:利用第一步中生成的高斯算子完成卷积

3:得到图像处理前后最大像素值之比rate(这里采用的是之间red,green,blue三通道的最大值进行比较)

4:完成归一化操作,返回处理后的像素


activity的代码:

public class ImageJAndroid9Activity extends Activity {
    ImageView sourceImage;
    ImageView destinationImage;
    float[][] gaositwo;
    private Handler handler=new Handler(){

        @Override
        public void handleMessage(Message msg) {
            if(msg.what==1){
                destinationImage.setImageBitmap((Bitmap)msg.obj);
            }
            super.handleMessage(msg);
        }
        
    };
    @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){
        gaositwo=get2DKernalData(1,1);
        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 a=pixel[y*width+x]>>24&0xff;
                int sumred=0;
                int sumgreen=0;
                int sumblue=0;
                for(int i=-1;i<=1;i++){
                    for(int j=-1;j<=1;j++){
                         int newi=y+i;
                         if(newi<0 || newi>=height){
                             newi=y;
                         }
                        
                         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*gaositwo[i+1][j+1];
                         sumgreen+=green*gaositwo[i+1][j+1];
                         sumblue+=blue*gaositwo[i+1][i+1];   
                    }
                }
                outpixel[y*width+x]=a<<24|sumred<<16|sumgreen<<8|sumblue;
            }
        }
        
        //计算图像处理前后最大像素值peak的rate,只是用red通道
        
        int[] temppixel=new int[width*height];
        int[] tempoutpixel=new int[width*height]; //是否能直接赋值
        for(int y=0;y<height;y++){
            for(int x=0;x<width;x++){
                temppixel[y*width+x]=pixel[y*width+x]&0xffffff;
                tempoutpixel[y*width+x]=outpixel[y*width+x]&0xffffff;
            }
        }
        Arrays.sort(temppixel);
        Arrays.sort(tempoutpixel);
        float rate=(float)temppixel[pixel.length-1]/tempoutpixel[outpixel.length-1];
        //归一化操作
        for(int y=0;y<height;y++){
            for(int x=0;x<width;x++){
                int a=outpixel[y*width+x];
                int red=outpixel[y*width+x]>>16&0xff;
                int green=outpixel[y*width+x]>>8&0xff;
                int blue=outpixel[y*width+x]&0xff;
                red=(int)(red*rate);
                green=(int)(green*rate);
                blue=(int)(blue*rate);
               // Log.i("msg","rate之后的值"+red+","+green+","+blue);
                outpixel[y*width+x]=a<<24|clamp(red)<<16|clamp(green)<<8|clamp(blue);
            }
        }
        
        Bitmap newBitmap=Bitmap.createBitmap(width, height,Config.RGB_565);
        newBitmap.setPixels(outpixel, 0, width, 0, 0, width, height);
        Message message=Message.obtain();
        message.what=1;
        message.obj=newBitmap;
        handler.sendMessage(message);
    }
    
    //生成高斯二维算子
    public float[][] get2DKernalData(int n, float sigma) {  
        int size = 2*n +1;  
        float sigma22 = 2*sigma*sigma;  
        float sigma22PI = (float)Math.PI * sigma22;  
        float[][] kernalData = new float[size][size];  
        int row = 0;  
        for(int i=-n; i<=n; i++) {  
            int column = 0;  
            for(int j=-n; j<=n; j++) {  
                float xDistance = i*i;  
                float yDistance = j*j;  
                kernalData[row][column] = (float)Math.exp(-(xDistance + yDistance)/sigma22)/sigma22PI;  
                column++;  
            }  
            row++;  
        }  
        return kernalData;  
    }  
    
    public int clamp(int a){
        return a<0?0:(a>255?255:a);
    }
}


最后得到的处理效果为:


参考文章:http://blog.csdn.net/jia20003/article/details/7234741

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值