title: Sobel Edge Detecting program in java
body: <p>I am trying to write a sobel edge detecting program in java. But i am getting werid outputs. HELP. Here is my program.</p>
  
  <pre><code>import java.io.*;
  import java.awt.*;
  import javax.imageio.ImageIO;
  import java.awt.image.BufferedImage;
  import java.util.Scanner;
  import java.lang.Math;
  
  public class sobeltest
  {
    public static void main(String args[]) throws IOException{
  
      System.out.println("Enter the file name :");
      Scanner ne1=new Scanner(System.in);
      String filename=ne1.nextLine();
  
  
     File file= new File(filename);
     BufferedImage image = ImageIO.read(file);
  
     int x=image.getWidth();
     int y=image.getHeight();
  
     for(int i=1;i&lt;x-1;i++)
      {
       for(int j=1;j&lt;y-1;j++)
        {
  
       int val00=image.getRGB(i-1,j-1);
       int val01=image.getRGB(i-1,j);
       int val02=image.getRGB(i-1,j+1);
  
       int val10=image.getRGB(i,j-1);
       int val11=image.getRGB(i,j);
       int val12=image.getRGB(i,j+1);
  
       int val20=image.getRGB(i+1,j-1);
       int val21=image.getRGB(i+1,j);
       int val22=image.getRGB(i+1,j+1);
  
       int gx=(((-1*val00)+(0*val01)+(1*val02))+((-2*val10)+(0*val11)+(2*val12))+((-1*val20)+(0*val21)+(1*val22)));
       int gy=(((-1*val00)+(-2*val01)+(-1*val02))+((0*val10)+(0*val11)+(0*val12))+((1*val20)+(2*val21)+(1*val22)));
  
       double gval=Math.sqrt((gx*gx)+(gy*gy));
       int g=(int)gval;
  
       image.setRGB(i,j,g);
  
        }
        }
      File outputfile = new File("sobel.png");
      ImageIO.write(image, "png", outputfile);
    }
    }
  </code></pre>
  
  <p>INPUT IMAGE:
  <a href="https://i.stack.imgur.com/YZAwl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YZAwl.png" alt="This is input image i given"></a></p>
  
  <p>and here is the output i get when i run the code. <a href="https://i.stack.imgur.com/yPH5z.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yPH5z.png" alt="OUTPUT IMAGE"></a></p>
  
  <p>......................................              </p>