图像处理之应用卷积一实现噪声消去

2024-06-12 22:48

本文主要是介绍图像处理之应用卷积一实现噪声消去,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 - created by gloomyfish

讨论如何使用卷积作为数学工具来处理图像,实现图像的滤波,其方法包含以下几种,均值

滤波,中值滤波,最大最小值滤波,关于什么是卷积以及理解卷积在图像处理中作用参见这

里–http://blog.csdn.net/jia20003/article/details/7038938

 

均值滤波:

均值滤波,是图像处理中最常用的手段,从频率域观点来看均值滤波是一种低通滤波器,高

频信号将会去掉,因此可以帮助消除图像尖锐噪声,实现图像平滑,模糊等功能。理想的均

值滤波是用每个像素和它周围像素计算出来的平均值替换图像中每个像素。采样Kernel数

据通常是3X3的矩阵,如下表示:


从左到右从上到下计算图像中的每个像素,最终得到处理后的图像。均值滤波可以加上两个

参数,即迭代次数,Kernel数据大小。一个相同的Kernel,但是多次迭代就会效果越来越好。

同样,迭代次数相同,Kernel矩阵越大,均值滤波的效果就越明显。

 

中值滤波

中值滤波也是消除图像噪声最常见的手段之一,特别是消除椒盐噪声,中值滤波的效果要比

均值滤波更好。中值滤波是跟均值滤波唯一不同是,不是用均值来替换中心每个像素,而是

将周围像素和中心像素排序以后,取中值,一个3X3大小的中值滤波如下:

 

最大最小值滤波

最大最小值滤波是一种比较保守的图像处理手段,与中值滤波类似,首先要排序周围像素和

中心像素值,然后将中心像素值与最小和最大像素值比较,如果比最小值小,则替换中心像

素为最小值,如果中心像素比最大值大,则替换中心像素为最大值。一个Kernel矩阵为3X3的最大最小值滤波如下:

 

 原图如下:



分别实现中值和均值滤波以后效果如下:


代码就不解释了,原理已经解释得很清楚了,全部算法源代码都是基于Java

特别说明一点的是,均值滤波对于高斯噪声的效果比较好,中值滤波对于椒盐噪声的效果比较好

想必大家从上面效果比较中也可以看到一点端倪。因为我选择的噪声图片是椒盐噪声的,哈哈


自己读吧,不解释了,有问题的可以问,源代码如下:

package com.process.blur.study;import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Arrays;public class SmoothFilter extends AbstractBufferedImageOp {public final static int MEAN_FILTER_TYPE = 1;public final static int MEADIAN_FILTER_TYPE = 2;public final static int MIN_MAX_FILTER_TYPE = 4;private int repeats = 3; // default 1private int kernel_size = 3; // default 3private int type = 1; // default mean typepublic int getRepeat() {return repeats;}public void setRepeat(int repeat) {this.repeats = repeat;}public int getKernelSize() {return kernel_size;}public void setKernelSize(int kernelSize) {this.kernel_size = kernelSize;}public int getType() {return type;}public void setType(int type) {this.type = type;}@Overridepublic BufferedImage filter(BufferedImage src, BufferedImage dest) {int width = src.getWidth();int height = src.getHeight();if ( dest == null )dest = createCompatibleDestImage( src, null );int[] inPixels = new int[width*height];int[] outPixels = new int[width*height];getRGB( src, 0, 0, width, height, inPixels );// pick up one filter from here!!!if(this.type == MEAN_FILTER_TYPE) {for(int i=0; i<repeats; i++) {performMeanFilter(width, height, inPixels, outPixels);System.arraycopy(outPixels, 0, inPixels, 0, inPixels.length);}} else if(this.type == MEADIAN_FILTER_TYPE) {performMedianFilter(width, height, inPixels, outPixels);} else if(this.type == MIN_MAX_FILTER_TYPE) {performMinMaxFilter(width, height, inPixels, outPixels);}// return resultsetRGB( dest, 0, 0, width, height, outPixels );return dest;}/***  <p> perform convolution filter </p>* * @param width* @param height* @param inPixels* @param outPixels*/public void performMeanFilter(int width, int height, int[] inPixels, int[] outPixels) {int rows2 = kernel_size/2;int cols2 = kernel_size/2;int index = 0;int index2 = 0;float total = kernel_size * kernel_size;for (int y = 0; y < height; y++) {for (int x = 0; x < width; x++) {float r = 0, g = 0, b = 0, a = 0;for (int row = -rows2; row <= rows2; row++) {int rowoffset = y + row;if(rowoffset < 0 || rowoffset >=height) {rowoffset = y;}//System.out.println("rowoffset == " + rowoffset);for(int col = -cols2; col <= cols2; col++) {int coloffset = col + x;if(coloffset < 0 || coloffset >= width) {coloffset = x;}index2 = rowoffset * width + coloffset;int rgb = inPixels[index2];a += ((rgb >> 24) & 0xff);r += ((rgb >> 16) & 0xff);g += ((rgb >> 8) & 0xff);b += (rgb & 0xff);}}int ia = 0xff;int ir = clamp((int)(r/total));int ig = clamp((int)(g/total));int ib = clamp((int)(b/total));outPixels[index++] = (ia << 24) | (ir << 16) | (ig << 8) | ib;}}}/***  <p> perform median filter </p>* * @param width* @param height* @param src* @param inPixels* @param outPixels*/public void performMedianFilter(int width, int height, int[] inPixels, int[] outPixels) {int rows2 = kernel_size/2;int cols2 = kernel_size/2;int index = 0;int index2 = 0;float total = kernel_size * kernel_size;int[] matrix = new int[(int)total];for (int y = 0; y < height; y++) {for (int x = 0; x < width; x++) {int count = 0;for (int row = -rows2; row <= rows2; row++) {int rowoffset = y + row;if(rowoffset < 0 || rowoffset >=height) {rowoffset = y;}for(int col = -cols2; col <= cols2; col++) {int coloffset = col + x;if(coloffset < 0 || coloffset >= width) {coloffset = x;}index2 = rowoffset * width + coloffset;int rgb = inPixels[index2];matrix[count] = rgb;count++; }}Arrays.sort(matrix);int ia = 0xff;int ir = ((matrix[count/2] >> 16) & 0xff);int ig = ((matrix[count/2] >> 8) & 0xff);int ib = (matrix[count/2] & 0xff);outPixels[index++] = (ia << 24) | (ir << 16) | (ig << 8) | ib;}}}/*** <p> perform min/max pixel filter </p>* * @param width* @param height* @param src* @param inPixels* @param outPixels*/public void performMinMaxFilter(int width, int height, int[] inPixels, int[] outPixels) {int rows2 = kernel_size/2;int cols2 = kernel_size/2;int index = 0;int index2 = 0;float total = kernel_size * kernel_size;int[] matrix = new int[(int)total];for (int y = 0; y < height; y++) {for (int x = 0; x < width; x++) {int count = 0;for (int row = -rows2; row <= rows2; row++) {int rowoffset = y + row;if(rowoffset < 0 || rowoffset >=height) {rowoffset = y;}for(int col = -cols2; col <= cols2; col++) {int coloffset = col + x;if(coloffset < 0 || coloffset >= width) {coloffset = x;}index2 = rowoffset * width + coloffset;int rgb = inPixels[index2];matrix[count] = rgb;count++; }}int ia = 0xff;int oldPixel = matrix[count/2];int targetRGB = findNewPixel(matrix, oldPixel);int ir = ((targetRGB >> 16) & 0xff);int ig = ((targetRGB >> 8) & 0xff);int ib = (targetRGB & 0xff);outPixels[index++] = (ia << 24) | (ir << 16) | (ig << 8) | ib;}}}private int findNewPixel(int[] matrix, int oldPixel) {ArrayList<Integer> list = new ArrayList<Integer>();for(int i=0; i<matrix.length; i++) {if(matrix[i] == oldPixel)continue;list.add(matrix[i]);}int[] filterData = new int[list.size()];int index = 0;for(Integer rgb : list) {filterData[index++] = rgb;}Arrays.sort(filterData);if(filterData.length == 0)return oldPixel;return (oldPixel > filterData[0]) ? filterData[0] : (oldPixel < filterData[filterData.length -1])? filterData[filterData.length -1] : oldPixel;}public static int clamp(int c) {if (c < 0)return 0;if (c > 255)return 255;return c;}}


这篇关于图像处理之应用卷积一实现噪声消去的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1055557

相关文章

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

利用python实现对excel文件进行加密

《利用python实现对excel文件进行加密》由于文件内容的私密性,需要对Excel文件进行加密,保护文件以免给第三方看到,本文将以Python语言为例,和大家讲讲如何对Excel文件进行加密,感兴... 目录前言方法一:使用pywin32库(仅限Windows)方法二:使用msoffcrypto-too