Advertisement
abdulfatirs

ConvolutionFilter.java | Kernel Image Processing in Java

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