Top hat (Top Hat):
A difference image between the original image and the opening operation, the original projection image brighter than the surrounding area
Black Hat (Black Hat):
Closing operation image difference image and 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.
Top hat and the results achieved python
def top_hat_demo(image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 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("top_hat_demo", dst)
Black Hat python implementation and results
def black_hat_demo(image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 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("black_hat_demo", dst)
Overcap and black binary image capper
def threshold_top_hat_demo(image): # 二值图像顶帽操作 gray = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY) ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) dst = cv2.morphologyEx(thresh, cv2.MORPH_TOPHAT, kernel) cv2.imshow("dst", dst) def threshold_black_hat_demo(image): # 二值图像黑帽操作 gray = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY) ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) dst = cv2.morphologyEx(thresh, cv2.MORPH_BLACKHAT, kernel) cv2.imshow("dst", dst)
形态学梯度操作
def gradient1_demo(image): cv2.imshow("image", image) gray = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY) ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) dst = cv2.morphologyEx(thresh, cv2.MORPH_GRADIENT, kernel) # 基本梯度 cv2.imshow("dst", dst) def gradients2_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(dm, image) # 外部梯度 cv2.imshow("internal", dst1) cv2.imshow("external", dst2)
内部梯度,外部梯度结果