abdulfatirs

ConvolutionFilter.java | Kernel Image Processing in Java

May 7th, 2014
1,016
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 KB | None | 0 0
  1. // ConvolutionFilter.java
  2. // Author: Abdul Fatir
  3.  
  4. import javax.imageio.ImageIO;
  5. import java.awt.image.BufferedImage;
  6. import java.awt.Color;
  7. import java.io.File;
  8. import java.io.InputStreamReader;
  9. import java.io.BufferedReader;
  10. import java.io.IOException;
  11. import static java.lang.System.out;
  12. public class ConvolutionFilter
  13. {
  14.     public static String filename = "raw.png";
  15.     public static void main(String args[])throws IOException
  16.     {
  17.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  18.        
  19.         out.print("Kernel Order:");
  20.         int order = Integer.parseInt(reader.readLine());
  21.        
  22.         float[][] kernel = new float[order][order];
  23.         float sum_of_elements = 0.0f;
  24.         float mult_factor = 1.0f;
  25.         float bias = 0f;
  26.        
  27.         // Getting the Kernel Matrix as input from the user
  28.         for(int i=0; i < order; i++)
  29.         for(int j=0; j < order; j++)
  30.         {
  31.             out.print(i+","+j+":");
  32.             kernel[i][j] = Float.parseFloat(reader.readLine());
  33.         }
  34.        
  35.         out.println("\nThe Kernel Matrix is:\n");
  36.        
  37.         for(int i=0; i < order; i++)
  38.         {
  39.             for(int j=0; j < order; j++)
  40.             {
  41.                 out.print("\t"+kernel[i][j]);
  42.                 sum_of_elements += kernel[i][j];
  43.             }
  44.             out.println();
  45.         }
  46.        
  47.         out.println("\nThe sum of matrix elements is: "+sum_of_elements);
  48.        
  49.         // mult_factor is the value with which each computed sum is multiplied
  50.         // mult_factor = 1 gives no changes to input kernel matrix
  51.         out.print("\nMultlipication Factor: ");
  52.         mult_factor = Float.parseFloat(reader.readLine());
  53.        
  54.         // Bias can be added to increase brightness of the image
  55.         // bias = 0 gives no change in brightness if the sum_of_elements is 1
  56.         out.print("Bias: ");
  57.         bias = Float.parseFloat(reader.readLine());
  58.        
  59.         BufferedImage input,output;
  60.        
  61.         input = ImageIO.read(new File(filename));
  62.         int WIDTH = input.getWidth();
  63.         int HEIGHT = input.getHeight();
  64.        
  65.         output = new BufferedImage(WIDTH, HEIGHT, input.getType());
  66.         out.println("[*] Rendering the image...");
  67.         for(int x=0;x<WIDTH;x++)
  68.         {
  69.             for(int y=0;y<HEIGHT;y++)
  70.             {
  71.                 float red=0f,green=0f,blue=0f;
  72.                 for(int i=0;i<order;i++)
  73.                 {
  74.                     for(int j=0;j<order;j++)
  75.                     {
  76.                         // Calculating X and Y coordinates of the pixel to be multiplied with current kernel element
  77.                         // In case of edges of image the '% WIDTH' wraps the image and the pixel from opposite edge is used
  78.                         int imageX = (x - order / 2 + i + WIDTH) % WIDTH;
  79.                         int imageY = (y - order / 2 + j + HEIGHT) % HEIGHT;
  80.                        
  81.                         int RGB = input.getRGB(imageX,imageY);
  82.                         int R = (RGB >> 16) & 0xff; // Red Value
  83.                         int G = (RGB >> 8) & 0xff;  // Green Value
  84.                         int B = (RGB) & 0xff;       // Blue Value
  85.                        
  86.                         // The RGB is multiplied with current kernel element and added on to the variables red, blue and green
  87.                         red += (R*kernel[i][j]);
  88.                         green += (G*kernel[i][j]);
  89.                         blue += (B*kernel[i][j]);
  90.                     }
  91.                 }
  92.                 int outR, outG, outB;
  93.                 // The value is truncated to 0 and 255 if it goes beyond
  94.                 outR = Math.min(Math.max((int)(red*mult_factor),0),255);
  95.                 outG = Math.min(Math.max((int)(green*mult_factor),0),255);
  96.                 outB = Math.min(Math.max((int)(blue*mult_factor),0),255);
  97.                 // Pixel is written to output image
  98.                 output.setRGB(x,y,new Color(outR,outG,outB).getRGB());
  99.             }
  100.         }
  101.         out.print("[?] Save file as:");
  102.         String outputfname = reader.readLine();
  103.         ImageIO.write(output,"PNG", new File(outputfname+".png"));
  104.         out.print("[+] File saved as "+outputfname+".png");
  105.     }
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment