public class MedianBlur implements ImageBlur {
public BufferedImage blur(BufferedImage image) {
if (image == null) {
throw new ImageNullException("Cannot blur null image");
}
int width = image.getWidth();
int height = image.getHeight();
BufferedImage blurredImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int[] reds = new int[9];
int[] greens = new int[9];
int[] blues = new int[9];
int count = 0;
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
int nx = x + dx;
int ny = y + dy;
if (nx >= 0 && nx < width && ny >= 0 && ny < height) {
int rgb = image.getRGB(nx, ny);
reds[count] = (rgb >> 16) & 0xFF;
greens[count] = (rgb >> 8) & 0xFF;
blues[count] = rgb & 0xFF;
count++;
}
}
}
Arrays.sort(reds);
Arrays.sort(greens);
Arrays.sort(blues);
int medianRed = reds[4];
int medianGreen = greens[4];
int medianBlue = blues[4];
int blurredPixel = (medianRed << 16) | (medianGreen << 8) | medianBlue;
blurredImage.setRGB(x, y, blurredPixel);
}
}
return blurredImage;
}
Hello, guys I've tried to change:
BufferedImage blurredImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
But nothing seems to be happening