[Reproduced] Laplacian of OpenCV-Python series (24)

We learned to use Sobel edge detection in the previous example in the previous tutorial. The principle is to use the jump of pixel values ​​in the edge area. By finding the first derivative, the edge value can be maximized. As shown below:
Insert picture description here

So, what do you get if you find the second derivative?
Insert picture description here
You can observe where the second derivative is 0. Therefore, this method can be used to obtain edges in an image. However, it should be noted that the second derivative of 0 not only appears at the edge, but may also be some meaningless positions. This situation can be handled by filtering as needed.

Second order differential

Now let’s discuss the second-order differential, which is the basis of the Laplace operator. It is slightly different from the differential defined in calculus. The digital image deals with discrete values, so the first-order differential of a one-dimensional function The basic definition is the difference:
Insert picture description here
similarly, the second-order differential is defined as:
Insert picture description here
extending a one-dimensional function to two dimensions:

Insert picture description here
The definition of second-order differential guarantees the following points:

1. The differential value in the constant gray area is 0

2. The differential value is non-zero at the starting point of the gray step or slope

It can be seen that the second-order differentiation can detect the edges of the image and enhance the details

Laplacian

From the above explanation, it can be seen that the second derivative can have edge detection. Since the image is two-dimensional, it is necessary to obtain the derivatives in two directions separately. The Laplacian operator is used here for approximation.

The Laplace operator is defined by the following formula:

Insert picture description here
among them:

Insert picture description here
It can be represented in digital form in many ways. For a 3*3 area, in general, the most recommended form is:

Insert picture description here
The filter template to implement the above formula is:
Insert picture description here
we can find that the Laplacian operator does not need to process the x and y directions separately like the Sobel operator, it can be processed directly, now let's take a look at the Lap in OpenCV The function prototype of the Lass operator:

dst = cv2.Laplacian(src, ddepth[, dst[, ksize[, scale[, delta[, borderType]]]]])

If you read the introduction to the Sobel operator in the previous tutorial, the parameters here should not be difficult to understand.

The first two are required parameters:

The first parameter is the image to be processed;

The second parameter is the depth of the image, -1 means the same depth as the original image is used. The depth of the target image must be greater than or equal to the depth of the original image;

Optional parameters follow:

dst need not explain;

ksize is the size of the operator and must be 1, 3, 5, 7. The default is 1.

scale is the scaling constant of the scaling derivative, and there is no scaling factor by default;

delta is an optional increment that will be added to the final dst. Similarly, no additional value is added to dst by default;

borderType is the mode for judging the border of the image. The default value of this parameter is cv2.BORDER_DEFAULT.

Let's look at the code:

	view plaincopy to clipboardprint? 
import cv2   
import numpy as np      
img = cv2.imread("pie.png")   
dst = cv2.Laplacian(img,cv2.CV_16S,ksize=3)   
dst = cv2.convertScaleAbs(dst) 
cv2.imshow("img",img)   
cv2.imshow("res",dst)      
cv2.waitKey(0)   
cv2.destroyAllWindows()

Insert picture description here

Now you can compare this result with the result of the previous tutorial. We found that this result is much better than the result of the previous tutorial, and there is no big deviation for edge detection.

However, in fact, this is only for simple images, and for a complex image, then the edge extraction is a bit helpless, let's look at the code:

	view plaincopy to clipboardprint?
import cv2  
import numpy as np  
  
img = cv2.imread("cat.jpg")  
dst = cv2.Laplacian(img,cv2.CV_16S,ksize=3)  
dst = cv2.convertScaleAbs(dst)  
cv2.imshow("img",img)  
cv2.imshow("res",dst)  
  
cv2.waitKey(0)  
cv2.destroyAllWindows()

Insert picture description here
It can be seen that for more complex images, the effect of Laplacian is not very good. Due to certain limitations of the second-order differential, the current edge detection is not perfect. We need a comprehensive algorithm. This will be introduced in the next tutorial.

Check the article summary page https://blog.csdn.net/weixin_44237705/article/details/107864965
More openvino technical information can be exchanged in the group~
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44237705/article/details/108398323