Java: finding edges with the Sobel operator

An edge is a place where brightness changes quickly. The Sobel operator estimates the horizontal change with a 3×3 kernel that subtracts the left column from the right column, giving the middle row double weight. A large absolute value means a strong vertical edge.

Java
class Main {
  public static void main(String[] args) {
    int[][] image = {
      {0, 0, 100, 100, 100},
      {0, 0, 100, 100, 100},
      {0, 0, 100, 100, 100},
      {0, 0, 100, 100, 100},
      {0, 0, 100, 100, 100},
    };
    int[][] gx = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
    for (int r = 1; r < 4; r++) {
      StringBuilder row = new StringBuilder();
      for (int c = 1; c < 4; c++) {
        int sum = 0;
        for (int kr = 0; kr < 3; kr++) {
          for (int kc = 0; kc < 3; kc++) {
            sum += image[r - 1 + kr][c - 1 + kc] * gx[kr][kc];
          }
        }
        row.append(Math.abs(sum) > 200 ? '#' : '.');
      }
      System.out.println(row);
    }
  }
}
Read the full lesson