Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Image2D {
- static BufferedImage imageInput;
- static BufferedImage imageOutput;
- public static BufferedImage convolution(float kernel[][], int wigth, int height, int sizeKernel, int kernelXY) {
- imageOutput = imageInput.getSubimage(0, 0, wigth, height);
- int r = 0, g = 0, b = 0;
- RGB c = new RGB();
- int pixels[][] = new int[wigth][height]; // contian a pixels
- // calculate imageInput --------------------
- for (int i = 0; i < wigth; i++) {
- for (int j = 0; j < height; j++) {
- pixels[i][j] = imageInput.getRGB(i, j); // get RGB
- for (int k = i; k < sizeKernel - 1; k++) {
- for (int l = j; l < sizeKernel - 1; l++) {
- int xloc = i + (k - kernelXY);
- int yloc = j + (l - kernelXY);
- if (xloc >= 0 && xloc < i && yloc >= 0 && yloc < j) {
- // try {
- r += c.red(pixels, xloc, yloc) * (kernel[i - 1 + k][j - 1 + l]/10); // calculate a RED and call red method
- g += c.green(pixels, xloc, yloc) * (kernel[i - 1 + k][j - 1 + l]/10); // calculate a GREEN and call green method
- b += c.blue(pixels, xloc, yloc) * (kernel[i - 1 + k][j - 1 + l]/10); // calculate a BLUE and call blue method
- int rgb = (r << 16) | (g << 8) | b;
- imageOutput.setRGB(i, j, rgb);
- // } catch (Exception e) {
- // System.out.println(e.getMessage());
- // }
- }
- }
- }
- }
- }
- return imageInput = imageOutput;
- }
- public static void gaussianFillter(BufferedImage img) {
- float gaussian[][] = {
- {1 / 9, 1 / 9, 1 / 9},
- {1 / 9, 1 / 9, 1 / 9},
- {1 / 9, 1 / 9, 1 / 9}
- };
- // get size imageInput and kernel
- int wight = img.getWidth();
- int heigth = img.getHeight();
- int kernelSize = gaussian.length;
- int kernelXY = kernelSize / 2; // find a center of kernel
- // make a result with convolution method
- convolution(gaussian, wight, heigth, kernelSize, kernelXY);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement