Lab 09n10 Morphological Image Processing
Lab 09n10 Morphological Image Processing
LEARNING OUTCOME:
Implementing and understanding the use of the morphological operators for image processing.
INTRODUCTION:
Morphology is a broad set of image processing operations that process images based on shapes.
Morphological operations apply a Structuring Element (SE) to an input image, creating an output
image of the same size. In a morphological operation, the value of each pixel in the output image
is based on a comparison of the corresponding pixel in the input image with its neighbors. By
choosing the size and shape of the neighborhood, you can construct a morphological operation that
is sensitive to specific shapes in the input image.
LEARNING TASKS:
Download chapter 9 images of the Gonzalez DIP 3rd edition book from the link given below.
https://www.imageprocessingplace.com/DIP-3E/dip3e_book_images_downloads.htm
Import the libraries using the following commands
import os # Importing operating system library
import cv2 # Importing OpenCV library
import matplotlib.pyplot as plt # Importing Matploblib library
import numpy as np # Importing NumPy library
Working directory can be checked and changed using the following commands
print(os.getcwd()) # Checking default current working directory
new_cwd = os.chdir('complete path') # Changing/updating current working directory
print(os.getcwd()) # Should output new changed directory
Structuring Elements:
The basic principle of mathematical morphology is the extraction of geometrical and topological
information from an unknown set (an image) through transformations using another, well- defined,
set known as structuring element (SE). In morphological image processing, the design of SEs, their
shape and size, is crucial to the success of the morphological operations that use them. SEs can be
can be created manually by using NumPy. However, in some cases, one might need
elliptical/circular shaped kernels. In OpenCV, cv2.getStructuringElement(shape, ksize,
anchor) is used to define SE, where
• shape [required] is the shape of the kernel and the input parameter can be set to
cv2.MORPH_RECT – rectangular SE
cv2.MORPH_ELLIPSE – elliptical SE
cv2.MORPH_CROSS – cross-shaped SE
• size [required] is the size of the defined SE
• anchor [optional] is the position of the anchor within the element. By default, the position
is at the center of the element
The function returns num_labels which are the number of connected components found, including
the background (which is labelled 0) and labels which is the labelled image, where each connected
component is assigned a unique label (starting from 1 for the first component, with background
labelled as 0).