title: sobel operator - false edges natural images
body: <p>I'm having  a problem with edge detection using Sobel operator: it produces too many false edges, effect is shown on pictures below.
  I'm using a 3x3 sobel operator - first extracting vertical then horizontal, final output is magnitude of each filter output.
  Edges on synthetic images are extracted properly but natural images produce have too many false edges or "noise" even if image is preprocessed by applying blur or median filter.
  What might be cause of this? Is it implementation problem (then: why synthetic images are fine?) or I need to do some more preprocessing?</p>
  
  <p><strong>Original:</strong><br>
  <img src="https://public.bay.livefilestore.com/y1pyWhyf0wpWLHM8p6yKSSn7mJloB_LCQFvVuFAvV-0PWFmrmvYKCuFQm4NMjr37eDi3eA_JnwNzJrFvqRbUqcJXw/clown.jpg?psid=1" alt="orig"> </p>
  
  <p><strong>Output:</strong><br>
  <img src="https://public.bay.livefilestore.com/y1prEAGf4iVdOjTUYXMbgFudKckBVbLvouiV8WYDBIySmd3HSxdo5aDx8cgbaQdru7amZalWNFyhInbhhLul7PiHQ/clownOut.jpg?psid=1" alt="out"></p>
  
  <p>code:</p>
  
  <pre><code>void imageOp::filter(image8* image, int maskSize, int16_t *mask)
  {
      if((image == NULL) || (maskSize/2 == 0) || maskSize &lt; 1)
      {
          if(image == NULL)
          {
              printf("filter: image pointer == NULL \n");
          }
          else if(maskSize &lt; 1)
          {
              printf("filter: maskSize must be greater than 1\n");
          }
          else
          {
              printf("filter: maskSize must be odd number\n");
          }
          return;
      }
  
      image8* fImage = new image8(image-&gt;getHeight(), image-&gt;getWidth());
      uint16_t sum = 0;
      int d = maskSize/2;
      int ty, tx;
  
      for(int x = 0; x &lt; image-&gt;getHeight(); x++)         //
      {                                                   // loop over image
          for(int y = 0; y &lt; image-&gt;getWidth(); y++)      // 
          {
              for(int xm = -d; xm &lt;= d; xm++)
              {
                  for(int ym = -d; ym &lt;= d; ym++)
                  {
                      ty = y + ym;
                      if(ty &lt; 0)  // edge conditions
                      {
                          ty = (-1)*ym - 1;
                      }
                      else if(ty &gt;= image-&gt;getWidth())
                      {
                          ty = image-&gt;getWidth() - ym;
                      }
  
                      tx = x + xm;
                      if(tx &lt; 0)  // edge conditions
                      {
                          tx = (-1)*xm - 1;
                      }
                      else if(tx &gt;= image-&gt;getHeight())
                      {
                          tx = image-&gt;getHeight() - xm;
                      }
  
                      sum += image-&gt;img[tx][ty] * mask[((xm+d)*maskSize) + ym + d];
                  }
              }
  
              if(sum &gt; 255)
              {
                  fImage-&gt;img[x][y] = 255;
              }
              else if(sum &lt; 0)
              {
                  fImage-&gt;img[x][y] = 0;
              }
              else
              {
                  fImage-&gt;img[x][y] = (uint8_t)sum;
              }
              sum = 0;
          }
      }
  
  
      for(int x = 0; x &lt; image-&gt;getHeight(); x++)
      {
          for(int y = 0; y &lt; image-&gt;getWidth(); y++)
          {
              image-&gt;img[x][y] = fImage-&gt;img[x][y];
          }
      }
  
      delete fImage;
  }
  </code></pre>