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

实现类:

import java.awt.image.BufferedImage;/*** 几种均值滤波算法实现 1、算术均值滤波 2、几何均值滤波 3、谐波均值滤波 4、逆谐波均值滤波* * @author admin*/
public class AverageFilter {// singletonprivate 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-1BufferedImage 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);}
}

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

  1. C语言数字图像处理---2.3图像空域滤波

    本文主要给大家讲解图像空间域滤波的相关内容,包括空域滤波概念,以及常用的空域滤波算法,并通过C语言编程来实现几种常用空域滤波(均值滤波.中值滤波.最大值滤波.最小值滤波.高斯滤波和统计滤波),帮助初学 ...

  2. C语言数字图像处理---2.5图像频域滤波

    上一小节我们介绍了图像频域变换,本小节将以此为基础,介绍图像频域滤波的相关内容,包含常见高通/低通/带通/带阻/方向滤波等频域滤波方法,同时以C语言编码实现,帮助初学者理解和掌握如何进行图像的频域滤波 ...

  3. Win8 Metro(C#)数字图像处理--2.52图像K均值聚类

    原文:Win8 Metro(C#)数字图像处理--2.52图像K均值聚类  [函数名称] 图像KMeans聚类      KMeansCluster(WriteableBitmap src,int ...

  4. 数字图像处理——12种常用图像处理方法对比

    数字图像处理--12种常用图像处理方法对比 1.图像反转 2.对数变换 3.幂次变换 4.分段函数 5.直方图均衡化 6.直方图规定化 7.直方图匹配 8.线性滤波 9.中值滤波与均值滤波 10.拉普 ...

  5. Win8Metro(C#)数字图像处理--2.3图像反色

    原文:Win8Metro(C#)数字图像处理--2.3图像反色 [函数名称] 图像反色函数ContraryProcess(WriteableBitmap src) [算法说明] 反色公式如下:     ...

  6. Win8 Metro(C#)数字图像处理--3.3图像直方图计算

    原文:Win8 Metro(C#)数字图像处理--3.3图像直方图计算 /// <summary>/// Get the array of histrgram./// </summa ...

  7. Win8Metro(C#)数字图像处理--2.7图像伪彩色

    Win8Metro(C#)数字图像处理--2.7图像伪彩色 原文:Win8Metro(C#)数字图像处理--2.7图像伪彩色  2.7图像伪彩色函数 [函数名称] 图像伪彩色函数PseudoCol ...

  8. Python 数据降噪处理的四种方法——均值滤波、小波变换、奇异值分解、改变binSize

    Python 数据降噪处理的四种方法--均值滤波.小波变换.奇异值分解.改变binSize github主页:https://github.com/Taot-chen 一.均值滤波 1)算法思想 给定 ...

  9. 数字图像处理实验四图像频域增强

    一.实验目的 (1)了解图像增强的目的及意义,加深对图像增强的感性认识,巩固所学的图像增强的理论知识和相关算法. (2)熟练掌握低通.高通.带通.同态滤波器的使用方法,明确不同性质的滤波器对图像的影响 ...

最新文章

  1. 不敢相信,居然用Java写了个“天天酷跑”!
  2. 少儿python编程培训-无锡江阴少儿Python编程培训一对一
  3. python程序如何循环_在Python的一段程序中如何使用多次事件循环详解
  4. (一)Java工程化--Maven基础
  5. js 获取url参数(QueryString)
  6. 云服务器php版本修改,云服务器 更改php版本
  7. oracle 执行顺序 select查询优化
  8. 复旦大学长跑协会申请书前言(初稿)
  9. 【从 0 开始机器学习】逻辑回归识别手写字符!
  10. Java学习:多线程
  11. 安卓手机状态栏 定位服务自动关闭_【科普知识】手机多久关机一次?看完才知道白用那么多年手机了!...
  12. 通过定位position=fixed实现网页内容的固定层效果
  13. Lwip协议详解(基于Lwip 2.1.0)-内存管理
  14. iTunes出现“iTunes不能读取iPhone的内容,请前往iPhone偏好设置的摘要选项卡,然后点击“恢复””的解决办法
  15. 嗨,你真的懂this吗? 1
  16. 计算机更改开机密码快捷方法,win7怎么修改开机密码(最快) win7修改开机密码最便捷的方法...
  17. 明源云客微信抢房技巧_抢房明源系统是什么意思_明源云客抢房软件
  18. 小米台灯、小米插线板 接入Home Assistant平台 让天猫精灵音箱控制
  19. word学习-清除格式+清除链接
  20. R语言逻辑回归logistic regression对付费用户进行预测

热门文章

  1. 软件驱动安装在docker_别为Docker本地实现不支持GPU发愁,解决方案在此!
  2. python变成exe后启动弹出选文件窗口_通过.py脚本执行的.exe文件隐藏控制台窗口...
  3. c 语言中浮点数舍入,浮点数在C中舍入,我不明白为什么
  4. 表单数据自动录入_智能记账系统,公式已设好,可直接录入数据,凭证报表自动生成...
  5. python3 打印目录下所有模块_python3基础12详解模块和包(库)|构建|使用
  6. php date 毫秒_swoole+PHP自动取消订单he还原库存
  7. 大学文科计算机考试大纲,(文科)大学计算机信息技术课程考核大纲(文科)介绍.doc...
  8. 鸿蒙os2.0公测结束了,鸿蒙OS2.0系统公测版发布时间-鸿蒙OS2.0系统公测版适配机型推荐...
  9. success 已正常处理 hide_最新微信小程序授权的详细处理思路(一)
  10. android 立体 流量球,Android自定义View——实现水波纹效果类似剩余流量球