1、选择一张高斯噪声比较明显的图片。高斯噪声是指它的概率密度函数服从高斯分布(即正态分布)的一类噪声。如果一个噪声,它的幅度分布服从高斯分布,而它的功率谱密度又是均匀分布的,则称它为高斯白噪声。
2、代码。生成模版半径分别是3、5和7的图片。
private static final String File_Path = "G:\\xiaojie-java-test\\img\\";
public static void main(String[] args) {
String gaussSourcePath = File_Path + "滤波\\高斯滤波\\高斯噪声.jpg";
String gaussTargetPath = File_Path + "滤波\\高斯滤波\\均值滤波_";
ImageService.averageFilter(gaussSourcePath, gaussTargetPath, ImageUtils.Gray_Type_Default, 3);//均值过滤
ImageService.averageFilter(gaussSourcePath, gaussTargetPath, ImageUtils.Gray_Type_Default, 5);//均值过滤
ImageService.averageFilter(gaussSourcePath, gaussTargetPath, ImageUtils.Gray_Type_Default, 7);//均值过滤
}
package com.zxj.reptile.utils.image;
import com.zxj.reptile.utils.number.ArrayUtils;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
public class ImageService {
/**
* 均值过滤
*
* @param sourcePath 原图像
* @param targetPath 目标图像
* @param filterLength 过滤的长度
*/
public static void averageFilter(String sourcePath, String targetPath, int grayType, int filterLength) {
try {
//获取原图像对象,并获取原图像的二维数组
BufferedImage image = ImageIO.read(new File(sourcePath));
int[][] imgArrays = ImageUtils.getTwoDimension(image);
//生成新图像的二维数组
imgArrays = ImageUtils.getGrayImg(imgArrays, grayType);//均值过滤
int[][] newImgArrays = ImageUtils.getAverageFilter(imgArrays, filterLength);//二值化
//生成新图片对象,填充像素
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
ImageUtils.setTwoDimension(newImage, newImgArrays, ImageUtils.Channel_Type_1);
//生成图片文件
ImageIO.write(newImage, "JPEG", new File(targetPath + filterLength + ".jpg"));
Thread.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.zxj.reptile.utils.image;
import java.awt.image.BufferedImage;
public class ImageUtils {
public static int[][] getAverageFilter(int[][] imgArrays, int filterLength) {
final int imgHeight = imgArrays.length;
final int imgWidth = imgArrays[0].length;
//模版半径
int[][] newImgArrays = new int[imgHeight][imgWidth];
int filterRadius = (filterLength - 1) / 2;
//均值过滤
for (int h = 0; h < imgHeight; h++) {//图片第几行
for (int w = 0; w < imgWidth; w++) {//图片第几列
int count = 0;
int sum = 0;
for (int templateH = -filterRadius; templateH <= filterRadius; templateH++) {//模版第几行
int rowIndex = h + templateH;
if (rowIndex < 0 || rowIndex > imgHeight - 1) {
continue;
}
for (int templateW = -filterRadius; templateW < filterRadius; templateW++) {//模版第几列
int columnIndex = w + templateW;
if (columnIndex < 0 || columnIndex > imgWidth - 1) {
continue;
}
sum += imgArrays[rowIndex][columnIndex];
count++;
}
}
newImgArrays[h][w] = sum / count;
}
}
return newImgArrays;
}
//灰度处理的方法
public static final byte Gray_Type_Default = 0;//默认加权法
public static final byte Gray_Type_Min = 1;//最大值法
public static final byte Gray_Type_Max = 2;//最小值法
public static final byte Gray_Type_Average = 3;//平均值法
public static final byte Gray_Type_Weight = 4;//加权法
public static final byte Gray_Type_Red = 5;//红色值法
public static final byte Gray_Type_Green = 6;//绿色值法
public static final byte Gray_Type_Blue = 7;//蓝色值法
//生成的图片是几通道的
public static final byte Channel_Type_Default = 0;//默认三通道
public static final byte Channel_Type_1 = 1;//单通道
public static final byte Channel_Type_3 = 3;//三通道
/**
* 灰度化处理
*
* @param imgArrays 图像二维数组
* @param grayType 灰度化方法
*/
public static int[][] getGrayImg(int[][] imgArrays, int grayType) throws Exception {
final int imgHeight = imgArrays.length;
final int imgWidth = imgArrays[0].length;
int[][] newImgArrays = new int[imgHeight][imgWidth];
for (int h = 0; h < imgHeight; h++) {
for (int w = 0; w < imgWidth; w++) {
final int[] grb = getRgb(imgArrays[h][w]);
newImgArrays[h][w] = getGray(grb, grayType);
}
}
return newImgArrays;
}
/**
* 通过像素值,返回r、g、b颜色通道的值
*
* @param pixel 像素值
*/
public static int[] getRgb(int pixel) {
int[] rgb = new int[3];
rgb[0] = (pixel >> 16) & 0xff;
rgb[1] = (pixel >> 8) & 0xff;
rgb[2] = pixel & 0xff;
return rgb;
}
/**
* 根据不同的灰度化方法,返回灰度值
*
* @param rgb r、g、b颜色通道的值
* @param grayType 不同灰度处理的方法
*/
public static int getGray(int[] rgb, int grayType) throws Exception {
if (grayType == Gray_Type_Average) {
return (rgb[0] + rgb[1] + rgb[2]) / 3; //rgb之和除以3
} else if (grayType == Gray_Type_Weight || grayType == Gray_Type_Default) {
return (int) (0.3 * rgb[0] + 0.59 * rgb[1] + 0.11 * rgb[2]);
} else if (grayType == Gray_Type_Red) {
return rgb[0];//取红色值
} else if (grayType == Gray_Type_Green) {
return rgb[1];//取绿色值
} else if (grayType == Gray_Type_Blue) {
return rgb[2];//取蓝色值
}
//比较三个数的大小
int gray = rgb[0];
for (int i = 1; i < rgb.length; i++) {
if (grayType == Gray_Type_Min) {
if (gray > rgb[i]) {
gray = rgb[i];//取最小值
}
} else if (grayType == Gray_Type_Max) {
if (gray < rgb[i]) {
gray = rgb[i];//取最大值
}
} else {
throw new Exception("grayType出错");
}
}
return gray;
}
/**
* 获取二维像素
*
* @param image BufferedImage图像对象
*/
public static int[][] getTwoDimension(BufferedImage image) {
final int imgWidth = image.getWidth();
final int imgHeight = image.getHeight();
int[][] imgArrays = new int[imgHeight][imgWidth];
for (int i = 0; i < imgHeight; i++) {
for (int j = 0; j < imgWidth; j++) {
imgArrays[i][j] = image.getRGB(j, i);
}
}
return imgArrays;
}
/**
* 将二维像素填充到图像中
*
* @param image BufferedImage图像对象
* @param imgArrays 二维像素
* @param channelType 单通道还是三通道
*/
public static void setTwoDimension(BufferedImage image, int[][] imgArrays, int channelType) throws Exception {
final int imgWidth = image.getWidth();
final int imgHeight = image.getHeight();
for (int i = 0; i < imgHeight; i++) {
for (int j = 0; j < imgWidth; j++) {
if (channelType == Channel_Type_1) {
image.setRGB(j, i, (byte) imgArrays[i][j]);
} else if (channelType == Channel_Type_3 || channelType == Channel_Type_Default) {
image.setRGB(j, i, imgArrays[i][j]);
} else {
throw new Exception("channelType错误");
}
}
}
}
}
3、结果。半径越长,图像越模糊。
4、结论。
均值滤波采用线性的方法,平均整个窗口范围内的像素值,均值滤波本身存在着固有的缺陷。它不能很好地保护图像细节,在图像去噪的同时也破坏了图像的细节部分,从而使图像变得模糊。均值滤波只适合处理高斯噪声的图片,而对椒盐噪声的处理则十分的不理想。当模版的长度越长,则图像就会越加的模糊。