Edge detection using Laplacian operator

void Laplacian(InputArray src, OutputArray dst, int ddepth, int ksize=1, double scale=1, double delta=0, int borderType=BORDER_DEFAULT )

Parameters:
  • src – Source image.
  • dst – Destination image of the same size and the same number of channels as src .
  • ddepth – Desired depth of the destination image.
  • ksize – Aperture size used to compute the second-derivative filters. See getDerivKernels() for details. The size must be positive and odd.
  • scale – Optional scale factor for the computed Laplacian values. By default, no scaling is applied. See getDerivKernels() for details.
  • delta – Optional delta value that is added to the results prior to storing them in dst .
  • borderType – Pixel extrapolation method. See borderInterpolate() for details.
You can find an example in OpenCV Documentation.

Steps:

  1. Load image
  2. Remove noise by blurring with a Gaussian filter
  3. Convert to gray-scale.
  4. Apply the Laplacian operator to find the edges.(Laplacian)
  5. The resulting image needs to be converted to 8-bit image for display.(convertScaleAbs)
  6. Show the result

Functions:

Example:

------------
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>

using namespace cv;

int main( int argc, char** argv )
{
    Mat src, gray, dst, abs_dst;
    src = imread( "lena.jpg" );

    /// Remove noise by blurring with a Gaussian filter
    GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );
    cvtColor( src, gray, CV_RGB2GRAY );

    /// Apply Laplace function
    Laplacian( gray, dst, CV_16S, 3, 1, 0, BORDER_DEFAULT );
    convertScaleAbs( dst, abs_dst );
    imshow( "result", abs_dst );

    waitKey(0);
    return 0;
}  
------------

14 comments:

  1. I an learning Im Proc with OpenCV. I have a basic question. Does Laplacian Edge detection takes longer time than the Canny or Sobel. If yes, Why?

    ReplyDelete
    Replies
    1. No. Laplacian operator takes same time that sobel operator takes. However, canny takes more time than others.

      In both Laplacian and Sobel, edge detection involves convolution with one kernel which is different in case of both. So they take almost same time.
      However, canny takes more time since it involves many steps for edge extraction.

      Delete
  2. Your steps don't match your algorithm. There's no Hough Transform ;)

    ReplyDelete
    Replies
    1. Hi. Thanks for pointing out the mistake. I have modified the steps.

      Delete
  3. Why would you blur the input image? It seems counter intuitive when trying to detect blur.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. You have a mistake in your code. Not an error but a very basic mistake. Check shis code and you will find it: http://paste.ofcode.org/fEiy8E82mWPFLbKnbivy25

    ReplyDelete
  6. where did we find the zero crossing

    ReplyDelete
  7. How to fix this error?
    "Traceback (most recent call last):
    File "C:/Documents/Python Programs/Laplacian.py", line 7, in
    laplacian = cv2.Laplacian(img,cv2.CV_64F)
    cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\core\src\matrix.cpp:757: error: (-215:Assertion failed) dims <= 2 && step[0] > 0 in function 'cv::Mat::locateROI'"

    ReplyDelete

  8. الحشرات الضارة هي الحشرات التي تلحق الضرر بالإنسان وممتلكاتهم ومحاصيلهم ومواشيهم ، وتنتشر في جميع أنحاء العالم ، وقد تسبب الحشرات أضرارًا مباشرة ، أي بامتصاص دماء الكائنات الحية ، أو أكل أنسجتها ، أو بشكل غير مباشر ، عن طريق إزالتها. تنتقل مسببات الأمراض أو الطفيليات إلى الكائنات الحية ، وغالبًا ما يكون الضرر هو البالغين. وأحيانًا يكون الضرر في البالغين ويرقاتهم. قد تعمل هذه الحشرات بمفردها أو قد تهاجم في مجموعات. في أي حال ، يجب مكافحة الآفات والقضاء عليها لتجنب التسبب في ضرر
    شركة ارمور للخدمات المنزلية

    ReplyDelete