I am writing a Image Processing project. Everything is done except blur effect. The common simple algorithm says :
- Take the 8 pixels surrounding your pixel and your pixel
- Average the RGB values of all 9 pixels and stick them in your current pixel location
- Repeat for every pixel
Below the I implemented to add blur effect
BufferedImage result = new BufferedImage(img.getWidth(),
img.getHeight(), img.getType());
int height = img.getHeight();
int width = img.getWidth();
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++) {
int pixel1 = (x == 0 || y == 0) ? 0 : img.getRGB(x - 1, y - 1);
int pixel2 = (y == 0) ? 0 : img.getRGB(x, y - 1);
int pixel3 = (y == 0 || x >= width-1) ? 0 : img.getRGB(x + 1, y - 1);
int pixel4 = (x == 0) ? 0 :img.getRGB(x - 1, y);
int pixel5 = img.getRGB(x, y);
int pixel6 = (x >= height -1) ? 0 :img.getRGB(x + 1, y);
int pixel7 = (x == 0 || y >= height -1) ? 0 :img.getRGB(x - 1, y + 1);
int pixel8 = (y >= height -1) ? 0 :img.getRGB(x, y + 1);
int pixel9 = (x >= width-1 || y >= height - 1) ? 0 :img.getRGB(x + 1, y + 1);
int newPixel = pixel1 + pixel2 + pixel3 + pixel4 + pixel5
+ pixel6 + pixel7 + pixel8 + pixel9;
newPixel = newPixel/9;
int redAmount = (newPixel >> 16) & 0xff;
int greenAmount = (newPixel >> 8) & 0xff;
int blueAmount = (newPixel >> 0) & 0xff;
newPixel = (redAmount<< 16) | (greenAmount << 8) | blueAmount ;
result.setRGB(x, y, newPixel);
}
I got noisy image as result rather than blurred image. I think I am doing something wrong.
Thanks in advance. Note : Any external API is restricted like Kernal, AffineTransfomation or etc...