title: Image has white border after Sobel Edge Detection
body: <p>I try to find edge of image. The my main problem is, why my edge detection <strong>has white border</strong>?</p>
  
  <p>Here the code :</p>
  
  <pre><code>public static BufferedImage executeSobelEdgeDetection(BufferedImage inputImage, int threshold) 
  {
      int width = inputImage.getWidth();
      int height = inputImage.getHeight();
      double red = 0.0;
      double green = 0.0;
      double blue = 0.0;
      BufferedImage borderedImage = new BufferedImage(width+2, height+2, BufferedImage.TYPE_INT_RGB);
  
      for(int x = 0 ; x &lt; width; x++)
      {
          for(int y = 0 ; y &lt; height; y++)
          {
              Color color = new Color(inputImage.getRGB(x ,y));
              red = color.getRed();
              green = color.getGreen();
              blue = color.getBlue();
              borderedImage.setRGB(x+1, y+1, new Color((int) red, (int) green, (int) blue).getRGB());
          }
      }
  
      BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  
  
      for (int x = 1; x &lt;= width; x++) 
      {
          for (int y = 1; y &lt;= height; y++) 
          {
  
              double gx = 0;
              double gy = 0;
  
  
              //Left Column Kernel
              Color color = new Color(borderedImage.getRGB(x-1 , y-1));
              red = color.getRed();
              green = color.getGreen();
              blue = color.getBlue();
              double intensity = red + green + blue;
              gx = gx + (intensity) *-1;
              gy = gy + (intensity) *-1;
  
              color = new Color(borderedImage.getRGB(x-1 , y));
              red = color.getRed();
              green = color.getGreen();
              blue = color.getBlue();
              intensity = red + green + blue;
              gx = gx + (intensity) *-2;
              gy = gy + (intensity) * 0;
  
              color = new Color(borderedImage.getRGB(x-1 , y+1));
              red = color.getRed();
              green = color.getGreen();
              blue = color.getBlue();
              intensity = red + green + blue;
              gx = gx + (intensity) *-1;
              gy = gy + (intensity) * 1;
  
              //Middle Column Kernel
              color = new Color(borderedImage.getRGB(x , y-1));
              red = color.getRed();
              green = color.getGreen();
              blue = color.getBlue();
              intensity = red + green + blue;
              gx = gx + (intensity) * 0;
              gy = gy + (intensity) * -2;
  
              color = new Color(borderedImage.getRGB(x , y));
              red = color.getRed();
              green = color.getGreen();
              blue = color.getBlue();
              intensity = red + green + blue;
              gx = gx + (intensity) * 0;
              gy = gy + (intensity) * 0;
  
              color = new Color(borderedImage.getRGB(x , y+1));
              red = color.getRed();
              green = color.getGreen();
              blue = color.getBlue();
              intensity = red + green + blue;
              gx = gx + (intensity) * 0;
              gy = gy + (intensity) * 2;
  
              //Right Column Kernel
              color = new Color(borderedImage.getRGB(x+1 , y-1));
              red = color.getRed();
              green = color.getGreen();
              blue = color.getBlue();
              intensity = red + green + blue;
              gx = gx + (intensity) * 1;
              gy = gy + (intensity) * -1;
  
              color = new Color(borderedImage.getRGB(x+1 , y));
              red = color.getRed();
              green = color.getGreen();
              blue = color.getBlue();
              intensity = red + green + blue;
              gx = gx + (intensity) * 2;
              gy = gy + (intensity) * 0;
  
              color = new Color(borderedImage.getRGB(x+1 , y+1));
              red = color.getRed();
              green = color.getGreen();
              blue = color.getBlue();
              intensity = red + green + blue;
              gx = gx + (intensity) * 1;
              gy = gy + (intensity) * 1;
  
  
              double length = Math.sqrt((Math.pow(gx, 2) + Math.pow(gy, 2)));
  
              length = length/4328 * 255;
  
  
              if(length &lt;= threshold)
              {
                  length = 0;
              }
              else
              {
                  length = 255;
              }
  
              outputImage.setRGB(x-1, y-1, new Color((int) length, (int) length, (int) length).getRGB());
          }
      }
  
      return outputImage;
  }
  </code></pre>
  
  <p>Here is input image as example :</p>
  
  <p><a href="https://i.stack.imgur.com/JaThL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JaThL.png" alt="enter image description here"></a></p>
  
  <p>Output Sobel Edge Detection :</p>
  
  <p><a href="https://i.stack.imgur.com/bc6KR.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bc6KR.jpg" alt="enter image description here"></a></p>
  
  <blockquote>
    <p>If you cannot see the border, you can save the image and open with IrfanView or something else which has black/dark background (cause the border has white color )</p>
  </blockquote>