Table of Contents
show
morphologyEx
cv2.morphologyEx(src, op, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]])
- src: The source image on which the morphological operation is to be performed. This can be a grayscale or binary image.
- op: The type of morphological operation to perform. Possible values are:cv2.MORPH_OPEN: Opening (Erosion followed by Dilation).
- cv2.MORPH_CLOSE: Closing (Dilation followed by Erosion).
- cv2.MORPH_GRADIENT: Morphological Gradient (Difference between Dilation and Erosion).
- cv2.MORPH_TOPHAT: Top Hat (Difference between input image and Opening of the image).
- cv2.MORPH_BLACKHAT: Black Hat (Difference between the Closing of the image and the input image).
- kernel: Structuring element used for the operation (can be created using cv2.getStructuringElement()).
- dst (optional): Output image of the same size and type as the source image.
- anchor (optional): Anchor point for the kernel. Default is (-1, -1), which means the anchor is at the kernel center.
- iterations (optional): Number of times the operation is applied.
- borderType (optional): Pixel extrapolation method. Default is cv2.BORDER_CONSTANT.
- borderValue (optional): Border value in case of a constant border type. Default is 0.

Morphological gradient
- The morphological gradient is a morphological operation that highlights the edges or boundaries of objects in a binary or grayscale image.
- It is computed as the difference between the dilation and erosion of the image.
- This operation helps in edge detection and can be particularly useful for identifying contours.
- The morphological gradient of image A with Structuring Element B is given as:
Gradient (A) = Dilation (A) – Erosion (A)
- Where dilation expands the boundaries of the objects
- Erosion shrinks the boundaries of the objects

Steps to compute Morphological Gradient
- Perform dilation on the image using the given structuring element.
- Perform erosion on the image using the same structuring element.
- Subtract the eroded image from the dilated image.
If a pixel in the dilated image is 0 and in the eroded image is 1, the gradient result will be 0 (since negative values are clipped).
Applications:
- The morphological gradient detects edges by calculating the difference between dilation and erosion of an image.
- It’s useful for boundary detection and identifying object outlines.
Top hat
- Morphological top-hat transformation (also called white top-hat) is a morphological operation that highlights small elements or objects that are brighter than their surroundings.
- It is useful for detecting bright objects on a darker background.
The top hat transformation is computed as difference between original image and closing of the image.
Tophat(A) = A – Opening (A,B)

- Where A is the input image
- Opening (A) is the opening of the image
Black hat
- The Black Hat operation is a morphological transformation that highlights the darker regions in an image which are smaller than the structuring element.
- It is the difference between the closing of the image and the original image
The black hat transformation is computed as difference between original image and closing of the image.
blackhat(A) = Closing (A,B) - A

- Where A is the input image
- Closing (A) is the closing of the image
Practice Exercise


Views: 0