线性卷积的概念:
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度之后,该卷积算子没有变。所以得到的图像效果跟前面的平滑滤波的效果是一样。这里只是介绍线性卷积滤波的使用方式。