高斯二维滤波的步骤:
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