Top hat (Top Hat):
The difference between the original image and the opening operation shown in (a difference), projecting the original image is brighter than the surrounding area
Black Hat (Black Hat):
Closing operations of the image - the original image, the original projection image darker than the surrounding area
Morphological Gradient (Gradient):
Gradient base: based gradient image by subtracting the expanded image to obtain a difference image after etching, referred to as a method of calculating a gradient image morphological gradient opencv is supported, and this method has been known as basic gradient gradient.
Internal Gradient: is an image with the original image after the difference image obtained by subtracting the corrosion, known as internal gradient image.
Outer gradient: difference minus the original image obtained after image expansion, referred to as external gradient image.
import cv2
import numpy as np
from matplotlib import pyplot as plt
__author__ = "zxsuperstar"
__email__ = "[email protected]"
"""
顶帽、黑帽、形态学梯度
"""
def top_hat_demo(image): #顶帽
cv2.imshow("image", image)
gray = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
dst = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, kernel)
# 增加亮度
cimage = np.array(gray.shape, np.uint8)
cimage = 100
dst = cv2.add(dst,cimage)
cv2.imshow("dst", dst)
def black_hat_demo(image): #黑帽
cv2.imshow("image", image)
gray = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
dst = cv2.morphologyEx(gray, cv2.MORPH_BLACKHAT, kernel)
# 增加亮度
cimage = np.array(gray.shape, np.uint8)
cimage = 100
dst = cv2.add(dst,cimage)
cv2.imshow("dst", dst)
def threshold_top_hat_demo(image):#顶帽 二值图像
cv2.imshow("image", image)
gray = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY)
ret, binary = cv2.threshold(gray, 0 ,255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
dst = cv2.morphologyEx(binary, cv2.MORPH_TOPHAT, kernel)
cv2.imshow("dst", dst)
def threshold_black_hat_demo(image):#黑帽 二值图像
cv2.imshow("image", image)
gray = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY)
ret, binary = cv2.threshold(gray, 0 ,255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
dst = cv2.morphologyEx(binary, cv2.MORPH_BLACKHAT, kernel)
cv2.imshow("dst", dst)
def gradient_threshold_hat_demo(image):
cv2.imshow("image", image)
gray = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY)
ret, binary = cv2.threshold(gray, 0 ,255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
dst = cv2.morphologyEx(binary, cv2.MORPH_GRADIENT, kernel) #基本梯度
cv2.imshow("dst", dst)
def gradients_threshold_hat_demo(image):
cv2.imshow("image", image)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
dm = cv2.dilate(image, kernel)
em = cv2.erode(image, kernel)
dst1 = cv2.subtract(image, em) #内部梯度
dst2 = cv2.subtract(image, dm) # 外部梯度
cv2.imshow("internal", dst1)
cv2.imshow("external", dst2)
if __name__ == "__main__":
image = cv2.imread("slant1.jpg") #blue green red
image = cv2.resize(image, (0, 0), fx=0.5, fy=0.5, interpolation=cv2.INTER_NEAREST)
# top_hat_demo(image)
black_hat_demo(image)
# threshold_top_hat_demo(image)
# threshold_black_hat_demo(image)
# gradient_threshold_hat_demo(image)
# gradients_threshold_hat_demo(image)
cv2.waitKey(0)
cv2.destroyAllWindows()
operation result:
Top hat black hat
Description:
cv2.morphologyEx()
See the first three parameters on the line, the latter will use the default value
The first parameter input
The second parameter type of operation
MORTH_OPEN function does open operation
MORTH_CLOSE closing operation function does
MORTH_GRADIENT function for morphological gradient operator
MORTH_TOPHAT function does top hat operation
MORTH_BLACKHAT function does black hat operation
MORTH_DILATE do dilation function
MORTH_ERODE function as a corrosion operation
The third parameter type of kernel function obtained using getStructuringElement