Opencv image edge detection: 2.Scharr operator (cv2.Scharr)

2.1 Principle introduction:  

        In a discrete space, there are many methods that can be used to calculate approximate derivatives. When using a 3×3 Sobel operator, the calculation results may not be very accurate. OpenCV provides the Scharr operator, which has the same speed as the Sobel operator and has higher precision. The Scharr operator can be regarded as an improvement to the Sobel operator, and its kernel is usually:

        

2.2 Function syntax:

        OpenCV provides the function cv2.Scharr() to calculate the Scharr operator, and its syntax format is as follows:

        dst=cv2.Scharr(src,ddepth,dx,dy[,scale[,delta[,borderType]]])

        In the formula:

        ● dst represents the output image.

        ● src represents the original image.

        ● ddepth represents the output image depth. This value has the same meaning as the parameter ddepth in the function cv2.Sobel().

        ● dx represents the derivative order in the x direction.

        ● dy represents the derivative order in the y direction.

        ● scale represents the scaling factor when calculating the derivative value. This item is optional, and the default value is 1, which means no scaling.

        ● delta represents the brightness value added to the target image, this item is optional, and the default value is 0.

        ● borderType represents the border style.

        Introduced in the function cv2.Sobel(), if ksize=-1, the Scharr filter will be used. Therefore, the following statements: dst=cv2.Scharr(src,ddepth,dx,dy) and dst=cv2.Sobel(src,ddepth,dx,dy,-1) are equivalent.

        The usage of function cv2.Scharr() and function cv2.Sobel() is basically the same. First of all, it should be noted that the value of the parameter ddepth should be set to "cv2.CV_64F", and the absolute value of the calculation result of the function cv2.Scharr() can be taken to ensure the correct processing result.

        In addition, it should be noted that in the function cv2.Scharr(), the parameters dx and dy are required to meet the conditions:

        dx >=0 && dy >=0 && dx+dy==1

        Therefore, the combination of parameters dx and parameters dy is:

        ● Calculate the edge (gradient) in the x direction: dx=1, dy=0.

        ● Calculate the edge (gradient) in the y direction: dx=0,dy=1.

        ● Calculate the edge superposition in the x direction and the y direction: realized by combination.

2.3 Program example

import cv2  as cv

def cv_show(name, img):
    cv.imshow(name, img)
    cv.waitKey(0)
    cv.destroyAllWindows()

img = cv.imread('D:\\qipan.jpg')
if img is None:
    print('Failed to read the image')

# x方向边缘检测
img1 = cv.Scharr(img, cv.CV_64F, 1, 0)
scharrx = cv.convertScaleAbs(img1)
cv_show('scharrx', scharrx)

# y方向边缘检测
img2 = cv.Scharr(img, cv.CV_64F, 0, 1)
scharry = cv.convertScaleAbs(img2)
cv_show('scharry', scharry)

# 需要注意的是,参数dx和dy的值不能都为1。例如,如下语句是错误的:
# dst=Scharr(src,ddpeth,dx=1,dy=1)

scharr = cv.addWeighted(scharrx, 0.5, scharry, 0.5, 0)
cv_show('scharr', scharr)

The original picture is as follows:

 Edge detection in x direction:

 Edge detection in the y direction:

 X, y direction overlay effect:

 

Guess you like

Origin blog.csdn.net/qq_49478668/article/details/123742957
Recommended