title: Java Convolution
body: <p>Hi I am in need of some help. I need to write a convolution method from scratch that takes in the following inputs: <code>int[][]</code> and <code>BufferedImage inputImage</code>. I can assume that the kernel has size 3x3. </p>
  
  <p>My approach is to do the follow:</p>
  
  <pre><code>convolve inner pixels
  convolve corner pixels
  convolve outer pixels
  </code></pre>
  
  <p>In the program that I will post below I believe I convolve the inner pixels but I am a bit lost at how to convolve the corner and outer pixels. I am aware that corner pixels are at <code>(0,0)</code>, <code>(width-1,0)</code>, <code>(0, height-1)</code> and <code>(width-1,height-1)</code>. I think I know to how approach the problem but not sure how to execute that in writing though. Please to aware that I am very new to programming :/ Any assistance will be very helpful to me.</p>
  
  <pre><code>import java.awt.*;
  import java.awt.image.BufferedImage;
  import com.programwithjava.basic.DrawingKit;
  import java.util.Scanner;
  
  public class Problem28 {
       // maximum value of a sample
      private static final int MAX_VALUE = 255;
      //minimum value of a sample
      private static final int MIN_VALUE = 0; 
  
      public BufferedImage convolve(int[][] kernel, BufferedImage inputImage) {
  
  
      }
  
      public BufferedImage convolveInner(double center, BufferedImage inputImage) {
          int width = inputImage.getWidth();
          int height = inputImage.getHeight();
  
          BufferedImage inputImage1 = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
          //inner pixels
          for (int x = 1; x &lt; width - 1; x++) {
              for (int y = 1; y &lt; height - 1; y ++) {
                  //get pixels at x, y
                  int colorValue = inputImage.getRGB(x, y);
                  Color pixelColor = new Color(colorValue);
                  int red =  pixelColor.getRed() ;
                  int green =  pixelColor.getGreen() ;
                  int blue =  pixelColor.getBlue();
  
                int innerred =  (int) center*red;
                int innergreen =  (int) center*green;
                int innerblue =  (int) center*blue;
  
                Color newPixelColor = new Color(innerred, innergreen, innerblue);
                int newRgbvalue = newPixelColor.getRGB();
                inputImage1.setRGB(x, y, newRgbvalue);    
              }
          }
          return inputImage1;
      }
  
      public BufferedImage convolveEdge(double edge, BufferedImage inputImage) {
          int width = inputImage.getWidth();
          int height = inputImage.getHeight();
  
          BufferedImage inputImage2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
          //inner pixels
          for (int x = 0; x &lt; width - 1; x++) {
              for (int y = 0; y &lt; height - 1; y ++) {
                  //get pixels at x, y
                  int colorValue = inputImage.getRGB(x, y);
                  Color pixelColor = new Color(colorValue);
                  int red =  pixelColor.getRed() ;
                  int green =  pixelColor.getGreen() ;
                  int blue =  pixelColor.getBlue();
  
                int innerred =  (int) edge*red;
                int innergreen =  (int) edge*green;
                int innerblue =  (int) edge*blue;
  
                Color newPixelColor = new Color(innerred, innergreen, innerblue);
                int newRgbvalue = newPixelColor.getRGB();
                inputImage2.setRGB(x, y, newRgbvalue);    
              }
          }
          return inputImage2;
      }
  
      public BufferedImage convolveCorner(double corner, BufferedImage inputImage) {
          int width = inputImage.getWidth();
          int height = inputImage.getHeight();
  
          BufferedImage inputImage3 = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
          //inner pixels
          for (int x = 0; x &lt; width - 1; x++) {
              for (int y = 0; y &lt; height - 1; y ++) {
                  //get pixels at x, y
                  int colorValue = inputImage.getRGB(x, y);
                  Color pixelColor = new Color(colorValue);
                  int red =  pixelColor.getRed() ;
                  int green =  pixelColor.getGreen() ;
                  int blue =  pixelColor.getBlue();
  
                int innerred =  (int) corner*red;
                int innergreen =  (int) corner*green;
                int innerblue =  (int) corner*blue;
  
                Color newPixelColor = new Color(innerred, innergreen, innerblue);
                int newRgbvalue = newPixelColor.getRGB();
                inputImage3.setRGB(x, y, newRgbvalue);    
              }
          }
          return inputImage3;
      }
  
  
    public static void main(String[] args) {
      DrawingKit dk = new DrawingKit("Compositor", 1000, 1000);
      BufferedImage p1 = dk.loadPicture("image/pattern1.jpg");
      Problem28 c = new Problem28();
      BufferedImage p5 = c.convolve();
      dk.drawPicture(p5, 0, 100);
    }
  }
  </code></pre>
  
  <p>I changed the code a bit but the output comes out as black. What did I do wrong:</p>
  
  <pre><code>import java.awt.*;
  import java.awt.image.BufferedImage;
  import com.programwithjava.basic.DrawingKit;
  import java.util.Scanner;
  
  public class Problem28 {
      // maximum value of a sample
      private static final int MAX_VALUE = 255;
      //minimum value of a sample
      private static final int MIN_VALUE = 0; 
  
  
      public BufferedImage convolve(int[][] kernel, BufferedImage inputImage) {
  
  
          int width = inputImage.getWidth();
          int height = inputImage.getHeight();
  
          BufferedImage inputImage1 = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  
          //for every pixel
          for (int x = 0; x &lt; width; x ++) {
              for (int y = 0; y &lt; height; y ++) {
  
                  int colorValue = inputImage.getRGB(x,y);
                  Color pixelColor = new Color(colorValue);
                  int red = pixelColor.getRed();
                  int green = pixelColor.getGreen();
                  int blue = pixelColor.getBlue();
  
                  double gray = 0;
  
                  //multiply every value of kernel with corresponding image pixel
                  for (int i = 0; i &lt; 3; i ++) {
                      for (int j = 0; j &lt; 3; j ++) {
  
                          int imageX = (x - 3/2 + i + width) % width;
                          int imageY = (x -3/2 + j + height) % height;
  
  
                          int RGB = inputImage.getRGB(imageX, imageY);
                          int GRAY = (RGB) &amp; 0xff;
  
                          gray += (GRAY*kernel[i][j]);
  
                      }
                  }
                  int out;
                  out = (int) Math.min(Math.max(gray * 1, 0), 255);
                  inputImage1.setRGB(x, y, new Color(out,out,out).getRGB());
              }
          }
          return inputImage1;
      }
  
  
  public static void main(String[] args) {
      int[][] newArray = {{1/9, 1/9, 1/9}, {1/9, 1/9, 1/9}, {1/9, 1/9, 1/9}};
      DrawingKit dk = new DrawingKit("Problem28", 1000, 1000);
      BufferedImage p1 = dk.loadPicture("image/pattern1.jpg");
      Problem28 c = new Problem28();
      BufferedImage p2 = c.convolve(newArray, p1);
      dk.drawPicture(p2, 0, 100);
    }
  }
  </code></pre>