title: What is the correct way to apply filter to a image
body: <p>I was wondering what the correct way would be to apply filter to a image. The image processing textbook that I am reading only talks about the mathematical and theoretical aspect of filters but doesn't talk much the programming part of it !</p>
  
  <p>I came up with this pseudo code could some one tell me if it is correct cause I applied the sobel edge filter to a image and I am not satisfied with the output. I think it detected many unnecessary points as edges and missed out on several points along the edge.</p>
  
  <p><img src="https://i.stack.imgur.com/43eTB.jpg" alt="Input image"></p>
  
  <p><img src="https://i.stack.imgur.com/V0fyS.jpg" alt="Output image"></p>
  
  <pre><code>int filter[][] = {{0d,-1d,0d},{-1d,8d,-1d},{0d,-1d,0d}};// I dont exactly remember the       //sobel filter
  int total = 0;
  for(int i = 2;i&lt;image.getWidth()-2;i++)
      for(int j = 2;j&lt;image.getHeight()-2;j++)
      {
          total = 0;
  
          for(int k = 0;k&lt;3;k++)
              for(int l = 0;l&lt;3;l++)
              {
                  total += intensity(image.getRGB(i,j)) * filter[i+k][j+l];
              }
  
          if(total &gt;= threshold){
          image.setRGB(i,j,WHITE);
      }
  }
  
  int intensity(int color)
  {
      return (((color &gt;&gt; 16) &amp; 0xFF) + ((color &gt;&gt; 8) &amp; 0xFF) + color)/3;
  }
  </code></pre>