<> Morphological gradient

Basic gradient : The difference between the image after expansion operation and the image after corrosion operation , It can be used to extract the edge of the object .

Internal gradient : The difference image between the original image and the corroded image .

External gradient : The difference image between the dilated image and the original image .

opencv The basic gradient is supported in , Let's put a piece of code and run the results more intuitive understanding :
import cv2 import matplotlib.pyplot as plt img = cv2.imread("fig.jpg") kernel =
cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) gradient = cv2.morphologyEx(
img, cv2.MORPH_GRADIENT, kernel) plt.subplot(121); plt.imshow(img); plt.title(
"original") plt.subplot(122); plt.imshow(gradient); plt.title("gradient") plt.
show()

<> Top hat

The difference image between the original image and the open operation image , The function is to extract noise , Highlight the part of the original image that is brighter than the surrounding area .( Because the open operation itself can remove some outliers , Subtle connection , Burr and other details , So these details can be extracted through the top hat operation )

The following code and running results to more intuitive understanding :
import cv2 import matplotlib.pyplot as plt img = cv2.imread("fig.jpg") kernel =
cv2.getStructuringElement(cv2.MORPH_RECT, (25, 25)) tophat = cv2.morphologyEx(
img, cv2.MORPH_TOPHAT, kernel) plt.subplot(121); plt.imshow(img); plt.title(
"original") plt.subplot(122); plt.imshow(tophat); plt.title("tophat") plt.show()

<> Black hat

The difference image between the closed operation image and the original image , The effect is to highlight the dark areas in the original image .( For example, the closed operation itself can fill some black holes inside the object , These black holes can be highlighted by black hat operations )

The following code and running results to more intuitive understanding :
import cv2 import matplotlib.pyplot as plt img = cv2.imread("fig.jpg") kernel =
cv2.getStructuringElement(cv2.MORPH_RECT, (25, 25)) blackhat = cv2.morphologyEx
(img, cv2.MORPH_BLACKHAT, kernel) plt.subplot(121); plt.imshow(img); plt.title(
"original") plt.subplot(122); plt.imshow(blackhat); plt.title("blackhat") plt.
show()

Technology