Image processing techniques Morphological filtering and erosion operations

1 Introduction

Welcome back, my fellow image processing enthusiasts! Today, let’s delve into morphological computation in the field of image processing. These nonlinear image processing techniques allow us to manipulate the shape and structure of objects in images. In this series, we will introduce the four basic morphological operations in order: erosion, dilation, opening operation and closing operation.

Without further ado, let’s get started!

2. Principle of corrosion operation

Erosion is one of the common morphological operations that finely shrinks objects in an image by removing pixels from the image boundaries. Specifically, it does this by considering each pixel's neighborhood and setting its value to the minimum value among all pixels in that neighborhood. In a binary image, if any adjacent pixel has a value of 0, the output pixel is also set to 0.

Next, let us explain through specific examples. First, import our today's image. The code is as follows:

# Define the image
original_image = np.array([[0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 1, 1, 1, 0, 0],
                           [0, 0, 1, 1, 1, 1, 0, 0],
                           [0, 1, 1, 1, 1, 0, 0, 0],
                           [0, 1, 1, 1, 0, 0, 0, 0],
                           [0, 1, 1, 1, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0]])

plt.figure(figsize=(10,10))
plt.imshow(original_image, cmap='gray', extent=[0, 8, 0, 8])
plt.title('Original Image', fontsize=20);

The result is as follows:
Insert image description here
Next is defining the structural elements, for this example let's use the cross as our choice:

# Define the structuring element
selem_cross = np.array([[0,1,0],
                        [1,1,1],
                        [0,1,0]])
plt.figure(figsize=(9,9))
plt.imshow(selem_cross, cmap='gray')
plt.title('Structuring Element: Cross', fontsize=20);

The visualization effect of structured elements is as follows:
Insert image description here

3. Corrosion operation effect

After the above operations, we define the original image and the corresponding structured template element that need to be operated, and then we use the function to apply_erosion apply the above structured template element to the corresponding image. The code is as follows:

def apply_erosion(image, selem):
    # Perform erosion on the given image using the structuring element, selem
    eroded_image = erosion(image, selem)

    # Display the original and eroded images
    fig, axes = plt.subplots(1, 3, figsize=(15, 10))
    ax = axes.ravel()
    
    ax[0].imshow(selem, cmap='gray', 
                 extent=[0, selem.shape[1], 0, selem.shape[0]])
    ax[0].set_title('Structuring Element', fontsize=20)

    ax[1].imshow(image, cmap='gray', 
                 extent=[0, image.shape[1], 0, image.shape[0]])
    ax[1].set_title('Original Image', fontsize=20)

    ax[2].imshow(eroded_image, cmap='gray', 
                 extent=[0, image.shape[1], 0, image.shape[0]])
    ax[2].set_title('Eroded Image', fontsize=20)

    plt.tight_layout()
    plt.show()

# Apply erosion on the original image with cross structuring element
apply_erosion(original_image, selem_cross)

The result is as follows:
Insert image description here
Observing the above output, you can see that the transformed image corresponds to a reduced version of the original image. This is the effect of the corrosion operation. You can have a more detailed understanding through the following animations. The sample pictures are as follows:

Insert image description here

4. Other effects

It is important to note that the neighborhood size or choice of structuring elements used in the corrosion operation can have a different impact on the results. We can choose different structural elements such as squares, discs or custom shapes to achieve specific corrosion effects based on the desired results.

For example, if I chose to use a square as my structural element, the code would be as follows:

# Define the structuring element 
selem_square = np.array([[0,0,0,0],
                         [0,1,1,0],
                         [0,1,1,0],
                         [0,0,0,0]])

# Apply erosion on the original image with square structuring element
apply_erosion(original_image, selem_square)

The resulting eroded image will look like this:Insert image description here

5. Summary

The main purpose of etching is to remove stray spots and fine lines so that only the substantial object remains. After we apply the erosion operation, the remaining lines look thinner and the shapes of the objects in the image appear smaller. Erosion Erosion is usually a necessary basic operation in tasks such as object segmentation and boundary extraction.

Have you failed in your studies?

Guess you like

Origin blog.csdn.net/sgzqc/article/details/132239536