用java编写代码实现图像平滑降噪(中值滤波、均值滤波、高斯滤波)
时间: 2024-05-09 12:18:48 浏览: 93
数字图像处理均值中值滤波
以下是Java代码实现图像平滑降噪的三种滤波方法:
1. 中值滤波:
```java
import java.awt.Color;
import java.awt.image.BufferedImage;
public class MedianFilter {
public static BufferedImage filter(BufferedImage image, int filterSize) {
int width = image.getWidth();
int height = image.getHeight();
BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int[] pixels = new int[filterSize * filterSize];
int index = 0;
for (int i = x - filterSize / 2; i <= x + filterSize / 2; i++) {
for (int j = y - filterSize / 2; j <= y + filterSize / 2; j++) {
if (i >= 0 && i < width && j >= 0 && j < height) {
Color color = new Color(image.getRGB(i, j));
pixels[index++] = color.getRed();
}
}
}
java.util.Arrays.sort(pixels);
int median = pixels[filterSize * filterSize / 2];
result.setRGB(x, y, new Color(median, median, median).getRGB());
}
}
return result;
}
}
```
2. 均值滤波:
```java
import java.awt.Color;
import java.awt.image.BufferedImage;
public class MeanFilter {
public static BufferedImage filter(BufferedImage image, int filterSize) {
int width = image.getWidth();
int height = image.getHeight();
BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int sum = 0;
int count = 0;
for (int i = x - filterSize / 2; i <= x + filterSize / 2; i++) {
for (int j = y - filterSize / 2; j <= y + filterSize / 2; j++) {
if (i >= 0 && i < width && j >= 0 && j < height) {
Color color = new Color(image.getRGB(i, j));
sum += color.getRed();
count++;
}
}
}
int mean = sum / count;
result.setRGB(x, y, new Color(mean, mean, mean).getRGB());
}
}
return result;
}
}
```
3. 高斯滤波:
```java
import java.awt.Color;
import java.awt.image.BufferedImage;
public class GaussianFilter {
public static BufferedImage filter(BufferedImage image, int filterSize, double sigma) {
int width = image.getWidth();
int height = image.getHeight();
BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
double[] kernel = new double[filterSize * filterSize];
double kernelSum = 0;
for (int i = 0; i < filterSize; i++) {
for (int j = 0; j < filterSize; j++) {
int x = i - filterSize / 2;
int y = j - filterSize / 2;
kernel[i * filterSize + j] = Math.exp(-(x * x + y * y) / (2 * sigma * sigma));
kernelSum += kernel[i * filterSize + j];
}
}
for (int i = 0; i < kernel.length; i++) {
kernel[i] /= kernelSum;
}
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
double sum = 0;
for (int i = x - filterSize / 2; i <= x + filterSize / 2; i++) {
for (int j = y - filterSize / 2; j <= y + filterSize / 2; j++) {
if (i >= 0 && i < width && j >= 0 && j < height) {
Color color = new Color(image.getRGB(i, j));
double weight = kernel[(i - x + filterSize / 2) * filterSize + j - y + filterSize / 2];
sum += weight * color.getRed();
}
}
}
int value = (int) Math.round(sum);
if (value < 0) {
value = 0;
}
if (value > 255) {
value = 255;
}
result.setRGB(x, y, new Color(value, value, value).getRGB());
}
}
return result;
}
}
```
以上代码中,`image` 是输入的 BufferedImage 对象,`filterSize` 是滤波器的大小,`sigma` 是高斯滤波的标准差。对于中值滤波和均值滤波,`result` 是经过滤波处理后的 BufferedImage 对象;对于高斯滤波,`kernel` 是高斯卷积核,`kernelSum` 是卷积核的和,`sum` 是卷积结果的和,`value` 是最终的像素值。
阅读全文