数字图像处理--几种图像均值滤波的java实现

    在《数字图像处理》一书中介绍了用于降低图像噪声的均值滤波器,分别有算数均值滤波器、几何均值滤波器、谐波均值滤波器、逆谐波均值滤波器。除了降噪,均值滤波器也可以模糊图像,滤波器大小为3、5、7...2n+1,滤波器越大计算量越大,产生的图像越模糊。这里采用纯java对几种算法进行实现,代码如下:

实现类:

import java.awt.image.BufferedImage;

/**
 * 几种均值滤波算法实现 1、算术均值滤波 2、几何均值滤波 3、谐波均值滤波 4、逆谐波均值滤波
 * 
 * @author admin
 */
public class AverageFilter {
	// singleton
	private static AverageFilter averageFilter = new AverageFilter();

	public static AverageFilter getInstance() {
		return averageFilter;
	}

	private AverageFilter() {
	}

	/**
	 * 算术均值滤波 滤波器大小:param取值3、5、7、9...(2n+1 ) 产生一幅模糊图像
	 * 
	 * @param image
	 */
	public void arithmeticAverageFilter(BufferedImage image, int param) {
		// 创建一个临时图像,为了保证原图边缘参与计算,临时图像比原图大param-1
		BufferedImage tempImage = new BufferedImage(image.getWidth() + param - 1, image.getHeight() + param - 1,
				image.getType());
		// 对图像进行填充,边缘像素采用最近像素填充
		nearFillEdge(tempImage, image, param);
		// 进行卷积运算
		for (int i = (param - 1) / 2; i < tempImage.getWidth() - (param - 1) / 2; i++) {
			for (int j = (param - 1) / 2; j < tempImage.getHeight() - (param - 1) / 2; j++) {
				int r = 0, g = 0, b = 0;
				// 计算滤波器内所有像素,R、G、B各个分量总和
				for (int x = -(param - 1) / 2; x <= (param - 1) / 2; x++) {
					for (int y = -(param - 1) / 2; y <= (param - 1) / 2; y++) {
						int tempRGB = tempImage.getRGB(i + x, j + y);
						int tempR = (tempRGB >> 16) & 0xff;
						int tempG = (tempRGB >> 8) & 0xff;
						int tempB = tempRGB & 0xff;
						r += tempR;
						g += tempG;
						b += tempB;
					}
				}
				// 采用总和除以滤波器内总像素数量得到均值
				r = (int) (r / Math.pow(param, 2));
				g = (int) (g / Math.pow(param, 2));
				b = (int) (b / Math.pow(param, 2));
				int rgb = (255 & 0xff) << 24 | (clamp(r) & 0xff) << 16 | (clamp(g) & 0xff) << 8 | (clamp(b) & 0xff);
				image.setRGB(i - (param - 1) / 2, j - (param - 1) / 2, rgb);
			}
		}
	}

	/**
	 * 几何均值滤波
	 * 
	 * @param image
	 * @param param
	 */
	public void geometryAverageFilter(BufferedImage image, int param) {
		// 创建临时图像
		BufferedImage tempImage = new BufferedImage(image.getWidth() + param - 1, image.getHeight() + param - 1,
				image.getType());
		// 填充边缘
		nearFillEdge(tempImage, image, param);
		// 进行卷积运算
		for (int i = (param - 1) / 2; i < tempImage.getWidth() - (param - 1) / 2; i++) {
			for (int j = (param - 1) / 2; j < tempImage.getHeight() - (param - 1) / 2; j++) {
				double r = 1.0, g = 1.0, b = 1.0;
				for (int x = -(param - 1) / 2; x <= (param - 1) / 2; x++) {
					for (int y = -(param - 1) / 2; y <= (param - 1) / 2; y++) {
						int tempRGB = tempImage.getRGB(i + x, j + y);
						double tempR = (tempRGB >> 16) & 0xff;
						double tempG = (tempRGB >> 8) & 0xff;
						double tempB = tempRGB & 0xff;
						r *= Math.pow(tempR + 1, 1.0 / (param * param));
						g *= Math.pow(tempG + 1, 1.0 / (param * param));
						b *= Math.pow(tempB + 1, 1.0 / (param * param));
					}
				}
				int rgb = (255 & 0xff) << 24 | (clamp((int) r) & 0xff) << 16 | (clamp((int) g) & 0xff) << 8
						| (clamp((int) b) & 0xff);
				image.setRGB(i - (param - 1) / 2, j - (param - 1) / 2, rgb);
			}
		}
	}

	/**
	 * 谐波均值滤波
	 * 
	 * @param image
	 * @param param
	 */
	public void harmonicFilter(BufferedImage image, int param) {
		// 创建temp图像
		BufferedImage tempImage = new BufferedImage(image.getWidth() + param - 1, image.getHeight() + param - 1,
				image.getType());
		// 填充边缘
		nearFillEdge(tempImage, image, param);
		// 进行卷积运算
		for (int i = (param - 1) / 2; i < tempImage.getWidth() - (param - 1) / 2; i++) {
			for (int j = (param - 1) / 2; j < tempImage.getHeight() - (param - 1) / 2; j++) {
				double r = 0, g = 0, b = 0;
				for (int x = -(param - 1) / 2; x <= (param - 1) / 2; x++) {
					for (int y = -(param - 1) / 2; y <= (param - 1) / 2; y++) {
						int tempRGB = tempImage.getRGB(i + x, j + y);
						double tempR = (tempRGB >> 16) & 0xff;
						double tempG = (tempRGB >> 8) & 0xff;
						double tempB = tempRGB & 0xff;
						r += 1 / tempR;
						g += 1 / tempG;
						b += 1 / tempB;
					}
				}
				r = param * param / r;
				g = param * param / g;
				b = param * param / b;
				int rgb = (255 & 0xff) << 24 | (clamp((int) r) & 0xff) << 16 | (clamp((int) g) & 0xff) << 8
						| (clamp((int) b) & 0xff);
				image.setRGB(i - (param - 1) / 2, j - (param - 1) / 2, rgb);
			}
		}
	}

	/**
	 * 逆谐波均值滤波
	 * 
	 * @param image
	 * @param param
	 * @param Q
	 *            当Q=0为算术均值滤波,Q=-1为谐波均值滤波;Q为正,消除胡椒噪声,Q为负,消除盐粒噪声
	 */
	public void reverseHarmonicFilter(BufferedImage image, int param, int Q) {
		// 创建temp图像
		BufferedImage tempImage = new BufferedImage(image.getWidth() + param - 1, image.getHeight() + param - 1,
				image.getType());
		// 填充边缘
		nearFillEdge(tempImage, image, param);
		// 进行卷积运算
		for (int i = (param - 1) / 2; i < tempImage.getWidth() - (param - 1) / 2; i++) {
			for (int j = (param - 1) / 2; j < tempImage.getHeight() - (param - 1) / 2; j++) {
				double r = 0, g = 0, b = 0, r1 = 0, g1 = 0, b1 = 0;
				for (int x = -(param - 1) / 2; x <= (param - 1) / 2; x++) {
					for (int y = -(param - 1) / 2; y <= (param - 1) / 2; y++) {
						int tempRGB = tempImage.getRGB(i + x, j + y);
						double tempR = (tempRGB >> 16) & 0xff;
						double tempG = (tempRGB >> 8) & 0xff;
						double tempB = tempRGB & 0xff;
						r += Math.pow(tempR, Q + 1);
						g += Math.pow(tempG, Q + 1);
						b += Math.pow(tempB, Q + 1);
						r1 += Math.pow(tempR, Q);
						g1 += Math.pow(tempG, Q);
						b1 += Math.pow(tempB, Q);
					}
				}
				r = r / r1;
				g = g / g1;
				b = b / b1;
				int rgb = (255 & 0xff) << 24 | (clamp((int) r) & 0xff) << 16 | (clamp((int) g) & 0xff) << 8
						| (clamp((int) b) & 0xff);
				image.setRGB(i - (param - 1) / 2, j - (param - 1) / 2, rgb);
			}
		}

	}

	// 判断r,g,b值,大于256返回256,小于0则返回0,0到256之间则直接返回原始值
	private int clamp(int rgb) {
		if (rgb > 255)
			return 255;
		if (rgb < 0)
			return 0;
		return rgb;
	}

	// 填充图像边缘空白像素,使用最近像素填充
	private void nearFillEdge(BufferedImage tempImage, BufferedImage image, int param) {
		for (int i = 0; i < tempImage.getWidth(); i++) {
			for (int j = 0; j < tempImage.getHeight(); j++) {
				// 临时图像位置没超过原图第一个位置,左下角
				if (i <= (param - 1) / 2 & j <= (param - 1) / 2) {
					int rgb = image.getRGB(0, 0);
					tempImage.setRGB(i, j, rgb);
				}
				// 临时图像位置超过横坐标最大,小于纵坐标最小,右下角
				if (i >= tempImage.getWidth() - (param - 1) / 2 - 1 & j <= (param - 1) / 2) {
					int rgb = image.getRGB(image.getWidth() - 1, 0);
					tempImage.setRGB(i, j, rgb);
				}
				// 临时图像位置超过纵坐标最大,小于横坐标最小,左上角
				if (j >= tempImage.getHeight() - (param - 1) / 2 - 1 & i <= (param - 1) / 2) {
					int rgb = image.getRGB(0, image.getHeight() - 1);
					tempImage.setRGB(i, j, rgb);
				}
				// 临时图像位置横纵坐标都超过原图最大位置,右上角
				if (i >= tempImage.getWidth() - (param - 1) / 2 - 1
						& j >= tempImage.getHeight() - (param - 1) / 2 - 1) {
					int rgb = image.getRGB(image.getWidth() - 1, image.getHeight() - 1);
					tempImage.setRGB(i, j, rgb);
				}
				// 临时图像位置横坐标大于最小,小于最大,纵坐标小于最小,正下方
				if (i > (param - 1) / 2 & i < tempImage.getWidth() - (param - 1) / 2 - 1 & j <= (param - 1) / 2) {
					int rgb = image.getRGB(i - (param - 1) / 2, 0);
					tempImage.setRGB(i, j, rgb);
				}
				// 临时图像位置横坐标小于最小,纵坐标大于最小,小于最大,左边
				if (j > (param - 1) / 2 & j < tempImage.getHeight() - (param - 1) / 2 - 1 & i <= (param - 1) / 2) {
					int rgb = image.getRGB(0, j - (param - 1) / 2);
					tempImage.setRGB(i, j, rgb);
				}
				// 右边
				if (j > (param - 1) / 2 & j < tempImage.getHeight() - (param - 1) / 2 - 1
						& i >= tempImage.getWidth() - (param - 1) / 2 - 1) {
					int rgb = image.getRGB(image.getWidth() - 1, j - (param - 1) / 2);
					tempImage.setRGB(i, j, rgb);
				}
				// 上方
				if (i > (param - 1) / 2 & i < tempImage.getWidth() - (param - 1) / 2 - 1
						& j >= tempImage.getHeight() - (param - 1) / 2 - 1) {
					int rgb = image.getRGB(i - (param - 1) / 2, image.getHeight() - 1);
					tempImage.setRGB(i, j, rgb);
				}
				// 中间
				if (i > (param - 1) / 2 & i < tempImage.getWidth() - (param - 1) / 2 - 1 & j > (param - 1) / 2
						& j < tempImage.getHeight() - (param - 1) / 2 - 1) {
					int rgb = image.getRGB(i - (param - 1) / 2, j - (param - 1) / 2);
					tempImage.setRGB(i, j, rgb);
				}
			}
		}
	}
}

测试类:

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class test {
	public static void main(String[] args) throws Exception{
		File input = new File("C:/桌面/AverageFilter/1.jpg");
		File output = new File("C:/桌面/AverageFilter/2.jpg");
		BufferedImage image = ImageIO.read(input);
		AverageFilter.getInstance().reverseHarmonicFilter(image, 3, 2);
		ImageIO.write(image, "jpg", output);
	}
}

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TheMatrixs

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值