title: Laplacian Filter opencv c++
body: <p>I was learning filters in OpenCV, but I'm a little confused about the Laplacian filter. My result is very different from the Laplacian filter in OpenCV lib.</p>
  
  <p>For first, I use a Gaussian filter for the image:</p>
  
  <pre><code>Mat filtroGauss(Mat src){
      Mat gauss = src.clone();
      Mat temp(src.rows+2,src.cols+2,DataType&lt;uchar&gt;::type);
  
      int y,x;
      for (y=0; y&lt;src.rows; y++){
      for (x=0; x&lt;src.cols; x++) temp.at&lt;uchar&gt;(y+1,x+1) = src.at&lt;uchar&gt;(y,x);
          }
  
      int mask[lenMask*lenMask];
      mask[0] = mask[2] = mask[6] = mask[8] = 1;
      mask[1] = mask[3] = mask[5] = mask[7] = 2;
      mask[4] = 4;
  
      int denominatore = 0;
      for (int i=0; i&lt;lenMask*lenMask; i++) denominatore += mask[i];
  
      int value[lenMask*lenMask];
      for(y=0; y&lt;src.rows; y++){
          for (x=0; x&lt;src.cols; x++){
              value[0] = temp.at&lt;uchar&gt;(y-1,x-1)*mask[0];
              value[1] = temp.at&lt;uchar&gt;(y-1,x)*mask[1];
              value[2] = temp.at&lt;uchar&gt;(y-1,x+1)*mask[2];
              value[3] = temp.at&lt;uchar&gt;(y,x-1)*mask[3];
              value[4] = temp.at&lt;uchar&gt;(y,x)*mask[4];
              value[5] = temp.at&lt;uchar&gt;(y,x+1)*mask[5];
              value[6] = temp.at&lt;uchar&gt;(y+1,x-1)*mask[6];
              value[7] = temp.at&lt;uchar&gt;(y+1,x)*mask[7];
              value[8] = temp.at&lt;uchar&gt;(y+1,x+1)*mask[8];
  
              int avg = 0;
              for(int i=0; i&lt;lenMask*lenMask; i++)avg+=value[i];
              avg = avg/denominatore;
  
              gauss.at&lt;uchar&gt;(y,x) = avg;
          }
      }
      return gauss;
  }
  </code></pre>
  
  <p>Then I use the Laplacian function:</p>
  
  <p><code>L(y,x) = f(y-1,x) + f(y+1,x) + f(y,x-1) + f(y,x+1) + 4*f(y,x)</code>  </p>
  
  <pre><code>Mat filtroLaplace(Mat src){
      Mat output = src.clone();
      Mat temp = src.clone();
  
      int y,x;
      for (y =1; y&lt;src.rows-1; y++){
          for(x =1; x&lt;src.cols-1; x++){
  
              output.at&lt;uchar&gt;(y,x) = temp.at&lt;uchar&gt;(y-1,x) + temp.at&lt;uchar&gt;(y+1,x) + temp.at&lt;uchar&gt;(y,x-1) + temp.at&lt;uchar&gt;(y,x+1) -4*( temp.at&lt;uchar&gt;(y,x));
          }
      }
      return output;
      }
  </code></pre>
  
  <p>And here is the final result from my code:</p>
  
  <p><a href="https://i.stack.imgur.com/Bio6d.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Bio6d.jpg" alt=""></a></p>
  
  <hr>
  
  <p>OpenCV result:</p>
  
  <p><a href="https://i.stack.imgur.com/TrrFz.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TrrFz.jpg" alt=""></a></p>