图像处理去噪点(中值滤波)java
来源:互联网 发布:网易短信验证源码 编辑:程序博客网 时间:2024/05/23 01:56
简单介绍一下中值滤波:
数字图像的采样或传输在经过传感器或传输通道时经常受到噪声的干扰。为了便利进一步的图像操作,如:边缘检测、图像分割和模式识别等,有必要甚至是必须对受噪图像进行滤波。中值滤波是由Tukey发明的一种非线性信号处理技术,由于其在滤除噪声的同时,能够很好地保护图像的边缘,因此得到了广泛的应用。但通常的中值滤波中的数据排序费时较多,特别在大窗口下,需要进行大量的数据比较工作,不利于图像快速实时的处理。因而有必要研究中值滤波。
代码如下:
package picture;import java.awt.Color;import java.awt.image.BufferedImage;import java.awt.image.ColorModel;import java.io.*;import java.util.Arrays;import java.awt.*;import javax.imageio.*;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import java.lang.Object;class Inco extends JFrame{ JButton[] buttons = new JButton[12]; public Inco() { super("Inco"); setSize(671,367); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); //这里我用了图片的绝对路径,根据你的图片写路径就可以了 ImageIcon ic = new ImageIcon("C:/Users/ddl/Desktop/zzlb4.png"); JLabel label=new JLabel(); label.setIcon(ic); panel.add(label); setContentPane(panel); show(); } }public class prcessing { public static void main(String[] args) throws IOException { medianFiltering(); Inco ik = new Inco(); } public static void medianFiltering() throws IOException { BufferedImage img = ImageIO.read(new File("C:/Users/ddl/Desktop/zzlb4.png")); int w = img.getWidth(); int h = img.getHeight(); int[] pix = new int[w*h]; img.getRGB(0, 0, w, h, pix, 0, w); int newpix[] = medianFiltering(pix, w, h); img.setRGB(0, 0, w, h, newpix, 0, w); BufferedImage outBufferedImage =new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); outBufferedImage.setRGB(0, 0, w, h, newpix, 0, w); ImageIO.write(outBufferedImage, "png", new File("C:/Users/ddl/Desktop/zzlb4.png")); } /** * 中值滤波 * @param pix 像素矩阵数组 * @param w 矩阵的宽 * @param h 矩阵的高 * @return 处理后的数组 */ public static int[] medianFiltering(int pix[], int w, int h) { int newpix[] = new int[w*h]; int[] temp = new int[9]; ColorModel cm = ColorModel.getRGBdefault(); int r=0; for(int y=0; y<h; y++) { for(int x=0; x<w; x++) { if(x!=0 && x!=w-1 && y!=0 && y!=h-1) { //g = median[(x-1,y-1) + f(x,y-1)+ f(x+1,y-1) // + f(x-1,y) + f(x,y) + f(x+1,y) // + f(x-1,y+1) + f(x,y+1) + f(x+1,y+1)] temp[0] = cm.getRed(pix[x-1+(y-1)*w]); temp[1] = cm.getRed(pix[x+(y-1)*w]); temp[2] = cm.getRed(pix[x+1+(y-1)*w]); temp[3] = cm.getRed(pix[x-1+(y)*w]); temp[4] = cm.getRed(pix[x+(y)*w]); temp[5] = cm.getRed(pix[x+1+(y)*w]); temp[6] = cm.getRed(pix[x-1+(y+1)*w]); temp[7] = cm.getRed(pix[x+(y+1)*w]); temp[8] = cm.getRed(pix[x+1+(y+1)*w]); Arrays.sort(temp); r = temp[4]; newpix[y*w+x] = 255<<24 | r<<16 | r<<8 |r; } else { newpix[y*w+x] = pix[y*w+x]; } } } return newpix; } public static int[] snnFiltering(int pix[], int w, int h) { int newpix[] = new int[w*h]; int n = 9; int temp, i1,i2, sum; int[] temp1 = new int[n]; int[] temp2 = new int[n/2]; ColorModel cm = ColorModel.getRGBdefault(); int r=0; for(int y=0; y<h; y++) { for(int x=0; x<w; x++) { if(x!=0 && x!=w-1 && y!=0 && y!=h-1) { sum = 0; temp1[0] = cm.getRed(pix[x-1+(y-1)*w]); temp1[1] = cm.getRed(pix[x+(y-1)*w]); temp1[2] = cm.getRed(pix[x+1+(y-1)*w]); temp1[3] = cm.getRed(pix[x-1+(y)*w]); temp1[4] = cm.getRed(pix[x+(y)*w]); temp1[5] = cm.getRed(pix[x+1+(y)*w]); temp1[6] = cm.getRed(pix[x-1+(y+1)*w]); temp1[7] = cm.getRed(pix[x+(y+1)*w]); temp1[8] = cm.getRed(pix[x+1+(y+1)*w]); for(int k=0; k<n/2; k++) { i1 = Math.abs(temp1[n/2] - temp1[k]); i2 = Math.abs(temp1[n/2] - temp1[n-k-1]); temp2[k] = i1<i2 ? temp1[k] : temp1[n-k-1]; //选择最接近原像素值的一个邻近像素 sum = sum + temp2[k]; } r = sum/(n/2); //System.out.println("pix:" + temp1[4] + " r:" + r); newpix[y*w+x] = 255<<24 | r<<16 | r<<8 |r; } else { newpix[y*w+x] = pix[y*w+x]; } } } return newpix; } public static int ostu(int[][] gray, int w, int h) { int[] histData = new int[w * h]; // Calculate histogram for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { int red = 0xFF & gray[x][y]; histData[red]++; } } // Total number of pixels int total = w * h; float sum = 0; for (int t = 0; t < 256; t++) sum += t * histData[t]; float sumB = 0; int wB = 0; int wF = 0; float varMax = 0; int threshold = 0; for (int t = 0; t < 256; t++) { wB += histData[t]; // Weight Background if (wB == 0) continue; wF = total - wB; // Weight Foreground if (wF == 0) break; sumB += (float) (t * histData[t]); float mB = sumB / wB; // Mean Background float mF = (sum - sumB) / wF; // Mean Foreground // Calculate Between Class Variance float varBetween = (float) wB * (float) wF * (mB - mF) * (mB - mF); // Check if new maximum found if (varBetween > varMax) { varMax = varBetween; threshold = t; } } return threshold; }}
效果如下
处理前:
处理几遍之后:
噪点基本被去除了。
阅读全文
0 0
- 图像处理去噪点(中值滤波)java
- 图像处理--中值滤波
- 数字图像处理—图像滤波(中值滤波)
- Delphi图像处理 -- 中值滤波
- 均值滤波、中值滤波【MATLAB】【图像处理】
- 图像平滑处理(归一化块滤波、高斯滤波、中值滤波、双边滤波)
- 图像平滑处理(归一化块滤波、高斯滤波、中值滤波、双边滤波)
- 图像平滑处理(归一化块滤波、高斯滤波、中值滤波、双边滤波)
- Delphi图像处理 -- 中值滤波(灰度分组统计法)
- 用于图像处理的自适应中值滤波(张旭明等)
- 【图像处理】高效的中值滤波(bug 已修复)
- 数字图像处理——图像平滑(中值滤波)
- MatLab 自编的 均值滤波、中值滤波、高斯滤波 图像处理函数(转)
- 基于DSP的图像中值滤波处理
- 图像滤波处理:均值、中值滤波器实现
- VC++高斯滤波\中值滤波实现图像模糊处理
- 中值滤波在图像去噪点中的应用
- 图像的中值滤波
- Dubbo(1)
- 1~9分成1:2:3的三个三位数
- poj1182食物链(带权并查集)
- 在visual studio中使用共享程序集的方法
- 利用纯css做选项卡源码
- 图像处理去噪点(中值滤波)java
- Mysql之in和Exists的用法
- 【String】为什么 Java 要把字符串设计成不可变的
- 【设计模式】—— 备忘录模式Memento
- FineCMS v5.0.5 系统实现置顶与推荐功能及排序
- 每天5道面试题——004
- Android中的WebView进行直接加载网页
- 一些有趣的博客
- 数组(Array)和列表(ArrayList) LinkedList 有什么区别?