Table of contents
Image gradient - Scharr operator
Image gradient—laplacian operator
Image Gradient—Sobel Operator
Mainly consider two directions: horizontal and vertical (right minus left, down minus up)
img=cv2.imread('pie.png',cv2.IMREAD_GRAYSCALE)
cv2.imshow("img",img)
cv2.waitKey()
cv2.destroyAllWindows()
dst=cv2.Sobel(src,ddepth,dx,dy,ksize)
- src: current image
- ddepth: the depth of the image (usually specified as -1)
- dx and dy represent the horizontal and vertical directions, respectively
- ksize is the size of the Sobel operator (usually 3*3)
def cv_show(img,name):
cv2.imshow(name,img)
cv2.waitKey()
cv2.destroyAllWindows()
#cv2.CV_64F: Negative values may appear due to subtraction from right to left, and subtraction from up to down. Negative values in opencv are truncated to 0. Here, a method with a higher number of digits is used to display negative values.
sobelx=cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
cv_show(sobelx,'sobelx')
The white point is the boundary, which is equivalent to the position with gradient. However, it is found that there is no white border on the right, because the gradient value cannot be displayed with negative numbers (white-black is positive, black-white is negative), so it is necessary to take the absolute value to become a positive number.
sobelx=cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
sobelx=cv2.convertScaleAbs(sobelx) # Calculate the absolute value of negative numbers
cv_show(sobelx,'sobely')
Graphics after execution
Next, perform the operation on dy
sobely=cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3)
sobely=cv2.convertScaleAbs(sobely)
cv_show(sobely,'sobely')
Image after execution
Calculate x and y separately, then sum
sobelxy=cv2.addWeighted(sobelx,0.5,sobely,0.5,0) #calculate summation by weight
cv_show(sobelxy,'sobelxy')
It is not recommended to directly calculate dx, dy
sobelxy=cv2.Sobel(img,cv2.CV_64F,1,1,ksize=3)
If Gx and Gy are directly calculated according to the above formula, ghosting will appear , as follows
The following uses the lena image to operate
img=cv2.imread('lena.jpg',cv2.IMREAD_GRAYSCALE)
sobelx=cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
sobelx=cv2.convertScaleAbs(sobelx)
sobely=cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3)
sobely=cv2.convertScaleAbs(sobely)
sobelxy=cv2.addWeighted(sobelx,0.5,sobely,0.5,0)
cv_show(sobelxy,'sobelxy')
Comparison of original image and processed image
The following direct calculation
img=cv2.imread('lena.jpg',cv2.IMREAD_GRAYSCALE)
sobelxy=cv2.Sobel(img,cv2.CV_64F,1,1,ksize=3)
sobelxy=cv2.convertScaleAbs(sobelxy)
cv_show(sobelxy,'sobelxy')
ghosting, blur
Image gradient - Scharr operator
Compared with the sobel operator, the value becomes larger and more sensitive to the difference in results
Image gradient—laplacian operator
More sensitive to some transformations, more sensitive to some noise points, not necessarily a good thing, often used in combination with other tools.
This operator compares the middle point with its edge point, because the perimeter is four points, so the middle is specified as -4
The following is the image comparison between the three operators
img=cv2.imread('lena.jpg',cv2.IMREAD_GRAYSCALE)
sobelx=cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
sobelx=cv2.convertScaleAbs(sobelx)
sobely=cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3)
sobely=cv2.convertScaleAbs(sobely)
sobelxy=cv2.addWeighted(sobelx,0.5,sobely,0.5,0)
scharrx=cv2.Scharr(img,cv2.CV_64F,1,0)
scharry=cv2.Scharr(img,cv2.CV_64F,0,1)
scharrx=cv2.convertScaleAbs(scharrx)
sharry=cv2.convertScaleAbs(sharry)
scharrxy=cv2.addWeighted(scharrx,0.5,scharry,0.5,0)
laplacian=cv2.Laplacian(img,cv2.CV_64F)
laplacian=cv2.convertScaleAbs(laplacian)
res=np.hstack((sobelxy,scharrxy,lapacian))
cv_show(res,'res')
The scharr operator is more sensitive, and the description of the image is more delicate and richer