Advertisement
Avatarr

newResult

May 28th, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. public class Image2D {
  2.  
  3.     static BufferedImage imageInput;
  4.     static BufferedImage imageOutput;
  5.  
  6.     public static BufferedImage convolution(float kernel[][], int wigth, int height, int sizeKernel, int kernelXY) {
  7.  
  8.  
  9.         imageOutput = imageInput.getSubimage(0, 0, wigth, height);
  10.         int r = 0, g = 0, b = 0;
  11.         RGB c = new RGB();
  12.         int pixels[][] = new int[wigth][height]; // contian a pixels
  13.  
  14.         // calculate imageInput --------------------
  15.         for (int i = 0; i < wigth; i++) {
  16.             for (int j = 0; j < height; j++) {
  17.                 pixels[i][j] = imageInput.getRGB(i, j); // get RGB
  18.                 for (int k = i; k < sizeKernel - 1; k++) {
  19.                     for (int l = j; l < sizeKernel - 1; l++) {
  20.                         int xloc = i + (k - kernelXY);
  21.                         int yloc = j + (l - kernelXY);
  22.                         if (xloc >= 0 && xloc < i && yloc >= 0 && yloc < j) {
  23.                             //     try {
  24.  
  25.                             r += c.red(pixels, xloc, yloc) * (kernel[i - 1 + k][j - 1 + l]/10); // calculate a RED and call red method
  26.                             g += c.green(pixels, xloc, yloc) * (kernel[i - 1 + k][j - 1 + l]/10); // calculate a GREEN and call green method
  27.                             b += c.blue(pixels, xloc, yloc) * (kernel[i - 1 + k][j - 1 + l]/10); // calculate a BLUE and call blue method
  28.  
  29.                             int rgb = (r << 16) | (g << 8) | b;
  30.                             imageOutput.setRGB(i, j, rgb);
  31.                             //  } catch (Exception e) {
  32.                             //      System.out.println(e.getMessage());
  33.                             //  }
  34.  
  35.                         }
  36.                     }
  37.                 }
  38.             }
  39.         }
  40.         return imageInput = imageOutput;
  41.  
  42.     }
  43.  
  44.     public static void gaussianFillter(BufferedImage img) {
  45.         float gaussian[][] = {
  46.             {1 / 9, 1 / 9, 1 / 9},
  47.             {1 / 9, 1 / 9, 1 / 9},
  48.             {1 / 9, 1 / 9, 1 / 9}
  49.         };
  50.  
  51.         // get size imageInput and kernel
  52.         int wight = img.getWidth();
  53.         int heigth = img.getHeight();
  54.         int kernelSize = gaussian.length;
  55.         int kernelXY = kernelSize / 2; // find a center of kernel
  56.  
  57.         // make a result with convolution method
  58.         convolution(gaussian, wight, heigth, kernelSize, kernelXY);
  59.  
  60.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement