@@ -0,0 +1,1675 @@
+package cs3246.a2;%0A%0Aimport java.lang.Math;%0A%0A/**%0A * Sobel operator for edge detection%0A * %0A * @author jasonpeng%0A *%0A */%0A%0Aclass SobelOperator %7B%0A  private byte%5B%5D%5B%5D mMatrix;%0A  private int mSizeX;%0A  private int mSizeY;%0A  private double%5B%5D%5B%5D mGradient;%0A  private double%5B%5D%5B%5D mDirection;%0A  %0A  %0A  private static final int%5B%5D%5B%5D KERNEL_X = new int%5B%5D%5B%5D%7B%0A    %7B 1,  0, -1 %7D,%0A    %7B 2,  0, -2 %7D,%0A    %7B 1,  0, -1 %7D%0A  %7D;%0A  %0A  private static final int%5B%5D%5B%5D KERNEL_Y = new int%5B%5D%5B%5D%7B%0A    %7B 1,  2,  1 %7D,%0A    %7B 0,  0,  0 %7D,%0A    %7B-1, -2, -1 %7D%0A  %7D;%0A  %0A  /**%0A   * @param imageMatrix the luminance matrix of the image%0A   * @param sizeX x dimension (left to right) of the image%0A   * @param sizeY y dimension (up to down)    of the image%0A   */%0A  public SobelOperator(byte%5B%5D%5B%5D imageMatrix, int sizeX, int sizeY) %7B%0A    this.mMatrix = imageMatrix;%0A    this.mSizeX = sizeX;%0A    this.mSizeY = sizeY;%0A  %7D%0A  %0A  /**%0A   * computes the edge gradients using KERNEL_X and KERNEL_Y%0A   * @return %0A   */%0A  public void compute() %7B%0A    mGradient = new double%5BmSizeX%5D%5BmSizeY%5D;%0A    mDirection = new double%5BmSizeX%5D%5BmSizeY%5D;%0A    int gx, gy;%0A    int x, y;%0A    %0A    // ignore border pixels%0A    for (x = 1; x %3C mSizeX - 1; x++) %7B%0A      for (y = 1; y %3C mSizeY - 1; y++) %7B%0A        // compute gx and gy for each pixel%0A        gx = gy = 0;%0A        for (int i = -1; i %3C= 1; i++) %7B%0A          for (int j = -1; j %3C= 1; j++) %7B%0A            gx += KERNEL_X%5Bi+1%5D%5Bj+1%5D * mMatrix%5Bx+i%5D%5By+j%5D;%0A            gy += KERNEL_Y%5Bi+1%5D%5Bj+1%5D * mMatrix%5Bx+i%5D%5By+j%5D;%0A          %7D%0A        %7D%0A        %0A        // compute gradient and direction%0A        mGradient%5Bx%5D%5By%5D = Math.sqrt(gx * gx + gy * gy);%0A        mDirection%5Bx%5D%5By%5D = Math.atan2(gy, gx);%0A      %7D%0A    %7D%0A    %0A  %7D%0A%7D

add sobel operator for edge detection