OpenCV  4.2.0
Open Source Computer Vision
Laplace Operator

Prev Tutorial: Sobel Derivatives
Next Tutorial: Canny Edge Detector

Goal

In this tutorial you will learn how to:

  • Use the OpenCV function Laplacian() to implement a discrete analog of the Laplacian operator.

Theory

  1. In the previous tutorial we learned how to use the Sobel Operator. It was based on the fact that in the edge area, the pixel intensity shows a "jump" or a high variation of intensity. Getting the first derivative of the intensity, we observed that an edge is characterized by a maximum, as it can be seen in the figure:

  2. And...what happens if we take the second derivative?

    You can observe that the second derivative is zero! So, we can also use this criterion to attempt to detect edges in an image. However, note that zeros will not only appear in edges (they can actually appear in other meaningless locations); this can be solved by applying filtering where needed.

Laplacian Operator

  1. From the explanation above, we deduce that the second derivative can be used to detect edges. Since images are "*2D*", we would need to take the derivative in both dimensions. Here, the Laplacian operator comes handy.
  2. The Laplacian operator is defined by:

    \[Laplace(f) = \dfrac{\partial^{2} f}{\partial x^{2}} + \dfrac{\partial^{2} f}{\partial y^{2}}\]

  3. The Laplacian operator is implemented in OpenCV by the function Laplacian() . In fact, since the Laplacian uses the gradient of images, it calls internally the Sobel operator to perform its computation.

Code

  1. What does this program do?
    • Loads an image
    • Remove noise by applying a Gaussian blur and then convert the original image to grayscale
    • Applies a Laplacian operator to the grayscale image and stores the output image
    • Display the result in a window

Explanation

Declare variables

Load source image

Reduce noise

Grayscale

Laplacian operator

  • The arguments are:
    • src_gray: The input image.
    • dst: Destination (output) image
    • ddepth: Depth of the destination image. Since our input is CV_8U we define ddepth = CV_16S to avoid overflow
    • kernel_size: The kernel size of the Sobel operator to be applied internally. We use 3 in this example.
    • scale, delta and BORDER_DEFAULT: We leave them as default values.

Convert output to a <em>CV_8U</em> image

Display the result

Results

  1. After compiling the code above, we can run it giving as argument the path to an image. For example, using as an input:

  2. We obtain the following result. Notice how the trees and the silhouette of the cow are approximately well defined (except in areas in which the intensity are very similar, i.e. around the cow's head). Also, note that the roof of the house behind the trees (right side) is notoriously marked. This is due to the fact that the contrast is higher in that region.

cv::String
std::string String
Definition: cvstd.hpp:150
cv::IMREAD_COLOR
@ IMREAD_COLOR
If set, always convert image to the 3 channel BGR color image.
Definition: imgcodecs.hpp:67
cv::cvtColor
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
Converts an image from one color space to another.
cv::samples::findFile
cv::String findFile(const cv::String &relative_path, bool required=true, bool silentMode=false)
Try to find requested data file.
cv::waitKey
int waitKey(int delay=0)
Waits for a pressed key.
cv::BORDER_DEFAULT
@ BORDER_DEFAULT
same as BORDER_REFLECT_101
Definition: base.hpp:277
highgui.hpp
cv::namedWindow
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
cv::quality::quality_utils::scale
void scale(cv::Mat &mat, const cv::Mat &range, const T min, const T max)
Definition: quality_utils.hpp:90
cv::convertScaleAbs
void convertScaleAbs(InputArray src, OutputArray dst, double alpha=1, double beta=0)
Scales, calculates absolute values, and converts the result to 8-bit.
cv::Size
Size2i Size
Definition: types.hpp:347
cv::imread
Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
cv::Mat::empty
bool empty() const
Returns true if the array has no elements.
imgcodecs.hpp
cv::imshow
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
cv::GaussianBlur
void GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX, double sigmaY=0, int borderType=BORDER_DEFAULT)
Blurs an image using a Gaussian filter.
cv::Mat
n-dimensional dense array class
Definition: mat.hpp:791
cv::Laplacian
void Laplacian(InputArray src, OutputArray dst, int ddepth, int ksize=1, double scale=1, double delta=0, int borderType=BORDER_DEFAULT)
Calculates the Laplacian of an image.
cv::imshow
void imshow(const String &winname, const ogl::Texture2D &tex)
Displays OpenGL 2D texture in the specified window.
CV_16S
#define CV_16S
Definition: interface.h:76
cv::COLOR_BGR2GRAY
@ COLOR_BGR2GRAY
convert between RGB/BGR and grayscale, color conversions
Definition: imgproc.hpp:542
cv
"black box" representation of the file storage associated with a file on disk.
Definition: affine.hpp:51
imgproc.hpp