🟠 Morphological Operations

Function Description

This operator performs a series of shape-based image processing operations on input images. Morphological operations modify pixel values by sliding a small window called "structural element" or "kernel" over the image, thereby achieving the purpose of modifying image geometric features (such as contours, holes, connections, etc.). It is mainly used for preprocessing and feature extraction of binary and grayscale images.

Usage Scenarios

Noise removal:

  • Use opening operation (OPEN) to remove small, isolated bright spots ("salt" in salt-and-pepper noise).

  • Use closing operation (CLOSE) to fill small black holes inside objects ("pepper" in salt-and-pepper noise).

Object connection and separation:

  • Use dilation (DILATE) to connect broken contours or merge adjacent objects.

  • Use erosion (ERODE) to separate slightly adhered objects or thin object contours.

Edge and contour extraction:

  • Use gradient operation (GRADIENT) to effectively extract object edge contours.

Specific feature extraction:

  • Use top hat operation (TOPHAT) to extract tiny details or spots brighter than surrounding areas.

  • Use black hat operation (BLACKHAT) to extract tiny details or scratches darker than surrounding areas.

  • Use hit-or-miss (HITMISS) for specific pattern matching, used for thinning or finding specific pixel arrangement patterns (advanced applications).

Input Output

Input

image(Image): Image to be processed. Usually binary images after binarization, but most operations also support grayscale or even color images.

Output

morphology_image(Image): Image obtained after performing morphological operations.

Parameter Description

Operation Type

Parameter Description

Select the specific morphological algorithm to execute.

Parameter Adjustment

Erosion (ERODE): Effect is to make bright regions (white) in images "thinner" and dark regions (black) "fatter". Can break thin connections and remove burrs from object boundaries.

Dilation (DILATE): Opposite of erosion, makes bright regions "fatter" and dark regions "thinner". Can fill holes inside objects and connect broken contours.

Opening (OPEN): Erosion followed by dilation. Main function is to eliminate small bright spots and thin bright lines while keeping overall contour size unchanged. Very suitable for removing discrete noise points.

Closing (CLOSE): Dilation followed by erosion. Main function is to fill small holes inside objects and close small cracks on object contours while keeping overall contour size unchanged.

Morphological gradient (GRADIENT): Calculates the difference between image dilation and erosion. Result looks like object contour lines.

Top hat (TOPHAT): Calculates the difference between original image and "opening operation" result. Can separate lines or noise points brighter than adjacent points, suitable for extracting bright details in bright backgrounds.

Black hat (BLACKHAT): Calculates the difference between "closing operation" result and original image. Can extract small dark objects or scratches in bright backgrounds.

Hit-or-miss transform (HITMISS): A special pattern matching operation that requires input to be single-channel grayscale image (usually binary image), used to find specific pixel neighborhood patterns.

Kernel Size

Parameter Description

Defines the size of structural element (kernel) used in morphological operations, format is [width, height].

Parameter Adjustment

  • Increase kernel size: Morphological operations will have more significant effects. For example, a larger kernel can remove larger noise points when performing opening operation; can fill larger holes when performing closing operation.

  • Decrease kernel size: More refined operation effects with smaller influence range.

When adjusting, the kernel size should be slightly larger than the image feature size you want to process (or connect).

Parameter Range

[3,3]

Kernel Shape

Parameter Description

Defines the shape of structural element.

Parameter Adjustment

The kernel shape affects the directionality of morphological operations.

  • Rectangle (RECT): Most commonly used shape, uniform influence in all directions.

  • Cross (CROSS): Only considers horizontal and vertical connectivity, ignoring diagonal directions.

  • Ellipse (ELLIPSE): Less influence on corner points than horizontal and vertical directions, smoother shape, suitable for processing circular or arc features.

  • Ones matrix (MatrixofOnes): Equivalent to rectangular kernel.

Iteration Count

Parameter Description

Sets the number of consecutive executions of erosion or dilation operations.

Parameter Adjustment

Increasing iteration count is equivalent to applying the same operation multiple times, with effects similar to using a larger kernel but not exactly the same.

  • Increase iteration count: Will strengthen operation effects. For example, 2 iterations of 3x3 erosion will produce stronger shrinkage effects than 1 iteration.

  • Usually, adjusting "kernel size" to control effects is more intuitive and efficient. When fine control or specific effects are needed, iteration count can be adjusted.

Parameter Range

[1,100], default value: 1