传统的滤波方式将滤波分为线性滤波和非线性滤波,这里首先说下线性滤波。
(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