Implementation of OpenCV image gradient

Time:2021-10-12
catalogue
  • summary
  • Gradient operation
  • formal hat
  • Black hat
  • Sobel operator
    • Calculate x
    • Calculate y
    • Calculate x + y
    • fuse

summary

Opencv is a cross platform computer vision library, which supports multiple languages and has powerful functions. Today Xiaobai will take you into the world of OpenCV hand in hand

Gradient operation

Gradient: expanding – eroding

example:

#Read picture
pie = cv2.imread("pie.png")

#Nucleus
kernel = np.ones((7, 7), np.uint8)

#Calculated gradient
gradient = cv2.morphologyEx(pie, cv2.MORPH_GRADIENT, kernel=kernel)

#Picture display
cv2.imshow("gradient", gradient)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output results:

在这里插入图片描述

formal hat

Top hat: original input – open operation result

example:

#Read picture
img = cv2.imread("white.png")

#Nucleus
kernel = np.ones((7, 7), np.uint8)

#Top hat
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel=kernel)

#Picture display
cv2.imshow("tophat", tophat)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output results:

在这里插入图片描述

Black hat

Black hat: closed operation – original input

example:

#Read picture
img = cv2.imread("white.png")

#Nucleus
kernel = np.ones((7, 7), np.uint8)

#Top hat
blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel=kernel)

#Picture display
cv2.imshow("blackhat", blackhat)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output results:

在这里插入图片描述

Sobel operator

Sobel operator is a very important operator in edge detection. Sobel operator is a kind of discrete difference operator, which is used to calculate the approximate gray value of image high brightness function

Format:


cv2.Sobel(src, ddepth, dx, dy, ksize)

Parameters:

  • SRC: Original
  • Ddepth: picture depth
  • DX: horizontal direction
  • Dy: vertical direction
  • Ksize: operator size

Calculate x

code:

#Read picture
img = cv2.imread("pie.png")

#Sobel operator
sobelx = cv2.Sobel(img, -1, 1, 0, ksize=3)

#Show pictures
cv2.imshow("sobelx", sobelx)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output results:

在这里插入图片描述

Calculate y

code:

#Read picture
img = cv2.imread("pie.png")

#Sobel operator
sobely = cv2.Sobel(img, -1, 0, 1, ksize=3)

#Show pictures
cv2.imshow("sobely", sobely)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output results:

在这里插入图片描述

Calculate x + y

code:

#Read picture
img = cv2.imread("pie.png")

#Sobel operator
sobel = cv2.Sobel(img, -1, 1, 1, ksize=3)

#Show pictures
cv2.imshow("sobel", sobel)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output results:

在这里插入图片描述

fuse

code:

#Sobel operator
sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)

#Convert to absolute value
sobelx = cv2.convertScaleAbs(sobelx)
sobely = cv2.convertScaleAbs(sobely)

#Fusion
sobel_xy = cv2.addWeighted(sobelx, 0.5, sobely, 0.5, 0)

#Show pictures
cv2.imshow("sobel_xy", sobel_xy)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output results:

在这里插入图片描述

Note: when ddepth is set to – 1, that is, it is consistent with the original figure, the result may be wrong. The calculated gradient value may be negative, and the negative number will be automatically truncated to 0. In order to avoid information loss, we need to use a higher data type cv2.cv_ 64F, and then map it to cv2.cv by taking the absolute value_ 8U type

This is the end of this article on the implementation method of OpenCV image gradient. For more information about OpenCV image gradient, please search for previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!