3 of 9 for Introduction to Image Processing

Filtering and Morphological Operations

How can we filter and change certain structures in the image?

Cyril Benedict Lugod
7 min readMay 11, 2023

In the realm of image processing, there are tons of ways to enhance the quality of images. Among these are methods called filtering and morphological operations. Filtering involves the modification of pixels in an image based on a certain filter. Morphological operations are interested in the modification of the shape and structure of an image.

Filtering

The main technique for filtering is called convolution. Convolution is an integral operation in image processing in order to extract certain information from an image by applying a filter (also called a kernel) to the image. The filter is simply a matrix of numbers which represent weights and multiplied to corresponding pixels in an image. The sum of each multiplication operation produces a new value which is characteristic of the filtered output for that specific part of the image. Each pixel in the image will be subjected to this filter.

Sample convolution

From the sample convolution, the filter can be seen sliding from left to right then from top to down. The amount of movement of the filter across the image is called the stride of the convolution. In our case above, the stride is 1 since the filter moves 1 unit with each convolution.

The convoluted image can be interpreted as the similarity measure of the filter with a certain locality within the image. For the last element of the convoluted image above, it has the highest value within the convoluted image (5). If we look back to the locality the filter considers for this particular element, we can notice that this specific area of the image matches perfectly with the filter. Thus, the convoluted image will always give us bright spots where the part of the image matches with the filter and dim areas where it does not.

There are several filters for different types of filtering in images such as:

  1. Horizontal sobel
  2. Vertical sobel
  3. Edge detection
  4. Sharpening
  5. Box blur
  6. Gaussian blur

Consider this grayscale image of the Gamcheon Culture Village in Busan, South Korea.

gamcheon = rgb2gray(imread('gamcheon.jpg'))
imshow(gamcheon);
Gamcheon Cultural Village sans its famous colors

Applying different filters to this image will produce the different convoluted features of the image which correspond to the particular filters. On the images below, the 3x3 grid on the left side is the filter with the overlaid numbers representing the matrix values for the filter. The image on the right side shows the convoluted image using the filter

Horizontal sobel filter

Applying the horizontal sobel filter highlighted the horizontal lines present in the scenery. The horizontal sobel filter considers the differences in intensities along the vertical axis, something a horizontal line divides.

Vertical sobel filter

Likewise, the vertical sobel filter considers the differences in intensities in the image along the horizontal axis, something that a vertical line would divide. This highlights the vertical elements in the image.

Edge detection filter

The edge detection filter seeks areas with boundaries thus it is able to highlight all the edges in the image.

Sharpening filter

Using a sharpening filter sharpens the images by increasing the contrast between adjacent pixels that have different intensities.

Box blur
Gaussian blur

Aside from filtering out elements from the image, filters can also adjust the relative intensities of all the pixels in the image by averaging out their intensities using blurring filters.

The effectiveness of filtering strongly depends on the size, shape, and values of the kernel used on the image. Larger kernels tend to provide more smoothing or blurring while smaller kernels are best for sharpening and edge detection. Overall, filtering using convolution is a powerful and versatile technique for a multitude of tasks, ranging from simple image smoothing applications and complex features extraction.

Morphological Operations

Morphological operations are used to manipulate the shape and structure of an image. The two primary operations used in morphological operations are dilation and erosion. Dilation involves expanding the edges of an image, while erosion involves shrinking the edges of an image. These two operations are essential in creating a skeleton of an image, which is useful in recognizing shapes and patterns.

Morphological operations are a set of image processing techniques which are used to manipulate the shape and structure of elements in the image. Two of the most common morphological operations are dilation and erosion. Dilation involves adding pixels to the boundaries of a given object in an image thus expanding the edges of an image. Erosion involves the removal of pixels from the boundaries of an object thus shrinking the size of the element. The number and location of the pixels added or removed from the elements in the image depends on the size and shape of the structuring element used.

To illustrate how a structuring element dilates and erodes an element in the image, consider the following example. First, we shall look at dilation with the original image on the left and the structuring element to be used for dilation on the right.

Structuring element to be used to dilate the original image

Dilation follows an OR operation wherein if there is an intersection between the center pixel of the structuring element and the element in the image, the overlay of the structuring element will get filled. The structuring element will scan across all the pixels in a similar fashion as the convolution described earlier.

Structuring element scans the entire image and the structuring element gets filled if there is an intersection with the center pixel

Once the structuring element has been scanned and applied across the entire image, the resulting dilated image appears to be an enlarged version of the original image. The particular effect achieved is dependent on the size and shape of the structuring element applied on the image.

Dilated image produced an enlarged version of the original image

Meanwhile, erosion follows an AND operation wherein if there is no complete intersection between the structuring element and the element in the image, the center pixel of the structuring element will get eroded. The structuring element will scan across all the pixels as with dilation. For simplicity, let us consider the same structuring element as earlier.

Structuring element to be used to erode the original image
Structuring element scans the entire image and the center pixel gets eroded if without complete intersection

After the structuring element has been applied across the entire image, the resulting eroded image appears to be a shrunk version of the original image. As with dilation, the particular effect achieved is dependent on the size and shape of the structuring element applied on the image.

Eroded image produced a shrunk version of the original image

However, some applications will require consecutive applications of dilation and erosion. An operation that performs a dilation then an erosion is called closing. Predictably, one that performs an erosion then a dilation is called opening. To provide an example where these aggregated functions are applicable, let us consider the following scenario.

Suppose we are tasked to remove the lines from the image below without affecting the size and shape of the circles.

lc = rgb2gray(imread('line_circles.png'))
imshow(lc)
Dilation and erosion alone cannot filter out the circles

Performing erosion alone on the image using a circular structuring element will remove the sticks but likewise shrink the circles.

struct_elem = np.array([[0,1,1,1,1,0],
[1,1,1,1,1,1],
[1,1,1,1,1,1],
[1,1,1,1,1,1],
[1,1,1,1,1,1],
[0,1,1,1,1,0]])

lc = erosion(lc, struct_elem)
imshow(lc);
Circle sizes and shapes not preserved with erosion alone

Hence, we will have to rescale the circles back to their original size by dilating them using the same circular structuring element.

lc = dilation(lc, struct_elem)
imshow(lc);
Circle sizes and shapes are reverted after dilating

Fortunately, we can also perform this operation in one go by using the opening and closing functions from skimage.morphology.

from skimage.morphology import opening, closing

imshow(opening(lc, struct_elem));
Same results as eroding then dilating — basically what opening is

Morphological operations are useful in extracting information from images by manipulating the shape and structure of its elements. These operations are crucial in several fields, including medical image analysis, remote sensing, and industrial inspection.

Conclusion

Overall, filtering and morphological operations are essential techniques in image processing as they have the ability to extract useful information from images. Application of spatial filters such as sobel and blurring filters aids in image de-noising and edge enhancement. Morphological operations, on the other hand, focus on manipulating the shapes and structures of the elements in an image to extract useful information. These techniques are important for various applications such as object detection, image segmentation, and feature extraction.

--

--

Cyril Benedict Lugod

Aspiring Data Scientist | MS Data Science @ Asian Institute of Management