Article directory
foreword
In edge regions, pixel intensities show "jumps" or high changes in intensity. Taking the first derivative of the intensity, we observe that the edge is characterized by a maximum value, as shown:
what happens if we take the second derivative?
You can observe that the second derivative is zero! Therefore, we can also use this criterion to try to detect edges in images. Note, however, that zeros don't just appear at edges (they can actually appear in otherwise meaningless places); this can be fixed by applying filtering where needed.
Laplacian
From the above explanation, we deduce that the second derivative can be used to detect edges. Since the image is " 2D ", we need to take the derivative in two dimensions. Here, the Laplacian operator comes in handy.
The Laplace operator is defined as:
1. Opencv function support
In fact, since Laplacian uses the gradient of the image, it internally calls the Sobel operator to perform its computation.
1. Function prototype:
CV_EXPORTS_W void Laplacian( InputArray src, OutputArray dst, int ddepth,
int ksize = 1, double scale = 1, double delta = 0,
int borderType = BORDER_DEFAULT );
2. Parameter description:
src: source image.
dst: The size and number of channels of the destination image are the same as src.
ddepth: The desired depth of the target image.
ksize: The aperture size used to calculate the second derivative filter. Size must be positive and odd.
scale : optional scale factor for calculating Laplacian values. By default, no scaling is applied.
delta: optional value, the offset value of the final result.
test code
cv::Mat src;
src = cv::imread("D:\\QtProject\\Opencv_Example\\laplace\\laplace.png", cv::IMREAD_GRAYSCALE);
if (src.empty()) {
cout << "Cannot load image" << endl;
return;
}
cv::imshow("src", src);
// Reduce noise by blurring
cv::GaussianBlur( src, src, cv::Size(5, 5), 3, 3, cv::BORDER_DEFAULT );
cv::Mat dst;
cv::Laplacian(src, dst, CV_16S, 3, 1, 0, cv::BORDER_DEFAULT );
// converting back to CV_8U
cv::Mat abs_dst;
cv::convertScaleAbs( dst, abs_dst );
cv::imshow( "abs_dst", abs_dst );
Effect:
Reference article: https://docs.opencv.org/4.x/d5/db5/tutorial_laplace_operator.html