Scharr filter

OpenCV also provides us with Scharr filter, which has the same processing speed as Sobel filter and higher accuracy. It can be seen as a modified version of the Sobel filter, whose core is usually:

In OpenCV, it provides the function cv2.scharr () to compute the Scharr filter, which is fully defined as follows:

def Scharr(src, ddepth, dx, dy, dst=None, scale=None, delta=None, borderType=None) :
Copy the code

Parameters and Sobel filter exactly the same, do not understand can be read, here is not repeated. Similarly, the gradient (derivative) calculated is exactly the same as the Sobel filter, with X, Y and XY superimposed. (Note that Scharr filter does not have XY direction, only superposition, if set dx,dy is equal to 1 will give an error)

Here we also implement these three effects and compare them, starting with the x-direction:

import cv2

img = cv2.imread("4.jpg", cv2.IMREAD_UNCHANGED)
sobel_x=cv2.Scharr(img,cv2.CV_64F,1.0)
result=cv2.convertScaleAbs(sobel_x)
cv2.imshow("img", img)
cv2.imshow("result", result)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code

After running, the result is as follows:And then in the Y direction:

import cv2

img = cv2.imread("4.jpg", cv2.IMREAD_UNCHANGED)
sobel_y=cv2.Scharr(img,cv2.CV_64F,0.1)
result=cv2.convertScaleAbs(sobel_y)
cv2.imshow("img", img)
cv2.imshow("result", result)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code

After running, the result is as follows:

And finally, XY superimposed:

import cv2

img = cv2.imread("4.jpg", cv2.IMREAD_UNCHANGED)
sobel_x=cv2.Scharr(img,cv2.CV_64F,1.0)
sobel_y=cv2.Scharr(img,cv2.CV_64F,0.1)
abs_x=cv2.convertScaleAbs(sobel_x)
abs_y=cv2.convertScaleAbs(sobel_y)
result=cv2.addWeighted(abs_x,0.5,abs_y,0.5.0)
cv2.imshow("img", img)
cv2.imshow("result", result)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code

After running, the result is as follows:

The difference between Scharr filter and Sobel filter is that Sobel filter has low accuracy and smaller nuclear structure, while Scharr filter has higher accuracy, as can be seen from the comparison below.

Laplacian filter

Laplacian filter is a kind of second derivative operator, which has rotation invariance and can meet the requirements of image edge sharpening in different directions. In general, the sum of the coefficients of its operator needs to be zero. For example, the following operator matrix is the Laplacian operator, and the sum is also 0.

The principle is as follows. Assume that our image is still 9 pixels from P1 to P9. The Laplacian operator and the matrix of the image will look like the following figure:

Even if the approximate derivative value of pixel P5 is as follows:

P5lap=(P2+P4+P6+P8)-4*P5

In OpenCV, it gives us the cv2.laplacian () function to implement the Laplacian filter, which is fully defined as follows:

def Laplacian(src, ddepth, dst=None, ksize=None, scale=None, delta=None, borderType=None) :
Copy the code

These parameters are basically the same as those described above and will not be described here. Note, however, that kszie is used to compute the kernel size of the derivative, which must be an odd number of positive numbers. When kszie is 1, it is the (1, -4) matrix above.

Let’s use the Laplacian filter to test the effect as follows:

import cv2

img = cv2.imread("4.jpg", cv2.IMREAD_UNCHANGED)
laplacian=cv2.Laplacian(img,cv2.CV_64F,ksize=1)
result=cv2.convertScaleAbs(laplacian)
cv2.imshow("img", img)
cv2.imshow("result", result)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code

After running, the result is as follows:

To sum up, Sobel and Scharr filters calculate the value of the first approximation derivative, while Laplacian filters calculate the value of the second approximation derivative.