java实现滤波器(附完整源码)
时间: 2023-10-17 21:25:51 浏览: 186
滤波器是数字图像处理中常用的一种技术,用于去除图像中的噪声、平滑图像、增强图像等操作。Java 中可以通过卷积运算实现滤波器。下面是 Java 实现常用的三种滤波器(均值滤波器、中值滤波器、高斯滤波器)的完整源码:
```java
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Filter {
// 均值滤波器
public static BufferedImage averageFilter(BufferedImage img, int size) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage filteredImg = new BufferedImage(w, h, img.getType());
int[][] kernel = new int[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
kernel[i][j] = 1;
}
}
int sum = 0;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
sum += kernel[i][j];
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
int r = 0, g = 0, b = 0;
for (int k = 0; k < size; k++) {
for (int l = 0; l < size; l++) {
int x = j + l - (size / 2);
int y = i + k - (size / 2);
if (x < 0 || x >= w || y < 0 || y >= h) {
continue;
}
int pixel = img.getRGB(x, y);
r += (pixel >> 16) & 0xff;
g += (pixel >> 8) & 0xff;
b += (pixel) & 0xff;
}
}
r /= sum;
g /= sum;
b /= sum;
filteredImg.setRGB(j, i, new Color(r, g, b).getRGB());
}
}
return filteredImg;
}
// 中值滤波器
public static BufferedImage medianFilter(BufferedImage img, int size) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage filteredImg = new BufferedImage(w, h, img.getType());
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
int[] r = new int[size * size];
int[] g = new int[size * size];
int[] b = new int[size * size];
int k = 0;
for (int y = i - size / 2; y <= i + size / 2; y++) {
for (int x = j - size / 2; x <= j + size / 2; x++) {
if (x < 0 || x >= w || y < 0 || y >= h) {
continue;
}
int pixel = img.getRGB(x, y);
r[k] = (pixel >> 16) & 0xff;
g[k] = (pixel >> 8) & 0xff;
b[k] = (pixel) & 0xff;
k++;
}
}
int[] sortedR = r.clone();
int[] sortedG = g.clone();
int[] sortedB = b.clone();
Arrays.sort(sortedR);
Arrays.sort(sortedG);
Arrays.sort(sortedB);
int medianR = sortedR[sortedR.length / 2];
int medianG = sortedG[sortedG.length / 2];
int medianB = sortedB[sortedB.length / 2];
filteredImg.setRGB(j, i, new Color(medianR, medianG, medianB).getRGB());
}
}
return filteredImg;
}
// 高斯滤波器
public static BufferedImage gaussianFilter(BufferedImage img, int size, double sigma) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage filteredImg = new BufferedImage(w, h, img.getType());
int[][] kernel = new int[size][size];
double sum = 0;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
kernel[i][j] = (int) (Math.exp(-(Math.pow(i - size / 2, 2) + Math.pow(j - size / 2, 2)) / (2 * Math.pow(sigma, 2))) / (2 * Math.PI * Math.pow(sigma, 2)));
sum += kernel[i][j];
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
double r = 0, g = 0, b = 0;
for (int k = 0; k < size; k++) {
for (int l = 0; l < size; l++) {
int x = j + l - (size / 2);
int y = i + k - (size / 2);
if (x < 0 || x >= w || y < 0 || y >= h) {
continue;
}
int pixel = img.getRGB(x, y);
double weight = kernel[k][l] / sum;
r += ((pixel >> 16) & 0xff) * weight;
g += ((pixel >> 8) & 0xff) * weight;
b += (pixel & 0xff) * weight;
}
}
filteredImg.setRGB(j, i, new Color((int) r, (int) g, (int) b).getRGB());
}
}
return filteredImg;
}
public static void main(String[] args) throws IOException {
BufferedImage img = ImageIO.read(new File("input.png"));
BufferedImage filteredImg = averageFilter(img, 3); // 均值滤波器
// BufferedImage filteredImg = medianFilter(img, 3); // 中值滤波器
// BufferedImage filteredImg = gaussianFilter(img, 3, 1); // 高斯滤波器
ImageIO.write(filteredImg, "png", new File("output.png"));
}
}
```
上面的代码实现了三种滤波器:均值滤波器、中值滤波器和高斯滤波器。可以通过调用其中的一个方法来对图片进行滤波操作。具体实现细节可以看代码注释。其中,`input.png` 是要进行滤波的图片,`output.png` 是滤波后输出的图片。
可以通过下面的代码来测试上面的滤波器实现:
```java
BufferedImage img = ImageIO.read(new File("input.png"));
BufferedImage filteredImg = averageFilter(img, 3); // 均值滤波器
// BufferedImage filteredImg = medianFilter(img, 3); // 中值滤波器
// BufferedImage filteredImg = gaussianFilter(img, 3, 1); // 高斯滤波器
ImageIO.write(filteredImg, "png", new File("output.png"));
```
这段代码会对 `input.png` 图片进行均值滤波器操作,输出为 `output.png` 图片。
阅读全文