Reputation: 141
I am trying to write a program that would remove noise from an image with a Gaussian filter. I am trying to write the following code:
public class Main {
public static void main(String[] args) {
File file = new File("src/main/resources/123.jpg");
BufferedImage source;
try {
source = ImageIO.read(file);
BufferedImage result = new BufferedImage(source.getWidth(), source.getHeight(), source.getType());
int n = 3;
double[][] w = new double[n][n];
genGaussTemplate(w, n, 0.5);
for (int i = 1; i < source.getWidth() - 1; i++) {
for (int j = 1; j < source.getHeight() - 1; j++) {
// Получаем цвет текущего пикселя
Color color = new Color(source.getRGB(i, j));
// Получаем каналы этого цвета
int blue = color.getBlue();
int red = color.getRed();
int green = color.getGreen();
red = (int) ((float) red * w[1][1]);
green = (int) ((float) green * w[1][1]);
blue = (int) ((float) blue * w[1][1]);
// Cоздаем новый цвет
Color newColor = new Color(red, green, blue);
// И устанавливаем этот цвет в текущий пиксель результирующего изображения
result.setRGB(i, j, newColor.getRGB());
}
}
// Сохраняем результат в новый файл
File output = new File("src/main/resources/output/321.jpg");
ImageIO.write(result, "jpg", output);
}
catch (IOException e) {
System.out.println("Произошла ошибка - " + e.getMessage());
}
}
static void genGaussTemplate(double[][] window, int ksize, double sigma) {
final double pi = 3.1415926;
int center = ksize / 2;
double x2, y2;
double sum = 0;
for (int i = 0; i < ksize; i++) {
x2 = Math.pow(i - center, 2);
for (int j = 0; j < ksize; j++) {
y2 = Math.pow(j - center, 2);
double g = Math.exp(-(x2 + y2) / (2 * sigma * sigma));
g /= 2 * pi * sigma * sigma;
sum += g;
window[i][j] =g;
}
}
for (int i = 0; i < ksize; i++) {
for (int j = 0; j < ksize; j++) {
window[i][j] /=sum;
}
}
}
}
But instead of removing noise i'm getting darkening the image. What's i do wrong?
Gaussian noise is statistical noise having a probability density equal to the probability density of a normal distribution, also known as Gaussian. In other words, the values that such noise can take have a Gaussian distribution. Named after Karl Gauss.
A special case is white Gaussian noise, then the values at any given time are independent and equally distributed random variables (which means that together they do not correlate). When testing and modeling communication channels, Gaussian noise is used as additive white noise to generate additive white Gaussian noise.
In telecommunications, communication channels can be affected by broadband Gaussian noise coming from various natural sources, such as thermal vibrations of atoms in conductors (thermal noise or Johnson-Nyquist noise), shot noise, blackbody radiation from the earth or other warm objects, and from celestial sources such as the Sun
Upvotes: -1
Views: 155
Reputation: 7290
You are creating a 3*3 Gauss template w
, but you are using only the center element w[1][1]
instead of computing a weighted combination of the 9 pixels around your current position.
This effectively multiplies all pixel values with a constant value w[1][1]
, which has a value below 1, producing just a darker version of the original image.
Upvotes: 0