高斯二维滤波
2014-09-16 20:53
211 查看
高斯二维滤波的步骤:
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
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
相关文章推荐
- 图像滤波之二维高斯核
- 图像滤波之二维高斯核
- c#实现图像图像卷积与滤波——高斯平滑
- Java+opencv3.2.0之高斯滤波
- MATLAB绘制二维高斯曲面
- 分别使用二维统计滤波对椒盐噪声和高斯噪声进行滤波
- 【OpenCV】邻域滤波:方框、高斯、中值、双边滤波
- 【OpenCV】5种图像滤波辨析:方框、均值、高斯、中值、双边
- 二维高斯正态分布函数(转)
- [学习opencv]高斯、中值、均值、双边滤波
- 数值分析Matlab二维正态(高斯)分布以及协方差矩阵
- [学习opencv]高斯、中值、均值、双边滤波
- 高斯滤波核的大小与sigma(标准差)之间的关系
- opencv学习(二十三)之方框、均值、高斯、中值、双边滤波综合比较
- canny边缘提取之二 高斯图像滤波(将matlab的imfilter函数转换为C++函数)
- matlab laplacian of gaussian(拉普拉斯高斯) 图像滤波
- 高斯(Guassian)模糊 图像滤波 基本原理介绍
- 高斯图像滤波原理及其编程离散化实现方法
- 域滤波:方框、高斯、中值、双边滤波
- opencv源码解析之(一):高斯滤波GaussianBlur()