0% found this document useful (0 votes)
38 views

Lab 09n10 Morphological Image Processing

Uploaded by

mubi1026
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Lab 09n10 Morphological Image Processing

Uploaded by

mubi1026
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA

DEPARTMENT OF ELECTRICAL ENGINEERING

DIGITAL IMAGE PROCESSING LAB


Lab-09&10: 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

Semester: Fall-2024 Instructor: Dr. Junaid Mir


Session: 2K21 Designed by: Dr. Junaid Mir
UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA
DEPARTMENT OF ELECTRICAL ENGINEERING

1. Explore different types of morphological SEs.


a. Define square, elliptical and cross-shaped SEs of size 13 × 13 using the
cv2.getStructuringElement command. Also define a rectangle shape SE of size
3 × 13 and a horizontal line SE of size 1 × 13. Print and view their values to understand
each SE’s shape characteristics.
b. View and display all 5 defined SEs of part-a in a single 2 × 3 subplot grid with proper
titles. You should be able to create these shapes manually using NumPy array declaration.

 Erosion & Dilation:


The erosion of 𝐴 by 𝐵, denoted 𝐴 ⊖ 𝐵, is the set of pixel locations 𝑧, where the SE 𝐵 translated
to location 𝑧, i.e., 𝐵𝑧 , completely overlaps only with foreground pixels in 𝐴. The erosion operation
is defined mathematically as: 𝐴 ⊖ 𝐵 = {𝑧|𝐵𝑧 ⊆ 𝐴}. The dilation of 𝐴 by 𝐵, denoted 𝐴 ⊕ 𝐵, is the
set of pixel locations 𝑧, where the reflected SE 𝐵̂ translated to location 𝑧, i.e., 𝐵̂𝑧 overlaps with
foreground pixels in 𝐴. Defined mathematically as: 𝐴 ⊕ 𝐵 = {𝑧|[𝐵̂𝑧 ∩ 𝐴] ⊆ 𝐴}. In OpenCV,
cv2.erode(src, kernel, dst, anchor, iterations, borderType, borderValue) and
cv2.dilate(src, kernel, dst, anchor, iterations, borderType, borderValue) are used to perform
erosion and dilation, respectively, where
• src [required] is the input image to be eroded or dilated
• kernel [required] is the SE used for performing the morphological operation
• dst [optional] output image of the same size and type as the input image
• anchor [optional] is the position of the anchor within the element. By default, the position
is at the center of the element
• iterations [optional] is number of times the morphological operation is to be performed
• borderType [optional] defines the type of the border to be added
• borderValue [optional] is the border value in case of the constant border type

2. Read and display Fig0905(a)(wirebond-mask).tif in PYTHON.


a. Remove all components except the center square from the image using the morphological
erosion operation. Define suitable size elliptical shape and square shape SEs and use the
cv2.erode command. What is the minimum size of both SEs which can be used to
accomplish the said task? Display the eroded image from both SEs. Which shape is more
suitable for the said task?
b. Create a suitable SE to filter out all the lines (except horizontal and vertical) from the
image using the morphological erosion process. Your filtered image should look like the
image depicted below.

Semester: Fall-2024 Instructor: Dr. Junaid Mir


Session: 2K21 Designed by: Dr. Junaid Mir
UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA
DEPARTMENT OF ELECTRICAL ENGINEERING

3. Read and display Fig0907(a)(text_gaps_1_and_2_pixels).tif in PYTHON.


a. The given image is a poor resolution image as the text is broken. What could be the reason
for it? What could have gone wrong during the image acquisition?
b. One way to bridge or join the gaps between the characters is to thick characters which
can be accomplished via dilation operation. Use the cv2.dilation command with a
3 × 3 size square and elliptical shape SEs. Display the dilated images from both SEs
along with the original image in a single 1 × 3 subplot grid with proper titles. Which SE
results in the better results for the said task and why? What happens if we increase the
size of the SE?

 Opening & Closing


The opening of set 𝐴 by SE 𝐵, denoted 𝐴 ∘ 𝐵, is defined as 𝐴 ∘ 𝐵 = (𝐴 ⊖ 𝐵) ⊕ 𝐵, whereas, the
closing of set 𝐴 by SE 𝐵, denoted 𝐴 • 𝐵, is defined as 𝐴 • 𝐵 = (𝐴 ⊕ 𝐵) ⊖ 𝐵. In OpenCV,
cv2.morphologyEx(src, op, kernel, dst, anchor, iterations, borderType, borderValue) are used
to define different morphological operations, where
• src [required] is the input image to be eroded or dilated
• op [required] is the type of morphological operation to be applied and the operation
parameter can be set to
cv2.MORPH_CLOSE – morphological closing operation
cv2.MORPH_OPEN – morphological opening operation
cv2.MORPH_GRADIENT – morphological gradient, i.e., the difference between
dilation and erosion of an image.
• kernel [required] is the SE used for performing the morphological operation
• remaining parameters are same as defined for erosion and dilation operation

4. Read and display Fig0911(a)(noisy_fingerprint).tif in PYTHON.


a. Filter the image using the morphological image processing.
b. Perform the opening followed by the closing morphological image processing on the
image. Use a square SE of size 3 × 3. Display the three images: original image, opened
image and closing performed on the opened image, in a single 1 × 3 subplot grid with
titles.

5. Read and view Task5Img.png in PYTHON.


a. Filter the image using the morphological image processing by removing all diagonal and
horizontal image components. The filtered image should contain only the vertical image
components of the same size as in the original image. Display the filtered image.
b. Repeat part-b, but this the filtered image should contain only the horizontal components
of the same size as in the original image.

 Connected Component Analysis


Extraction of connected components from a binary image is central to many automated image
analysis applications. It can be used to count the objects and is frequently used in the automated
inspections. The cv2.connectedComponents(image, labels, connectivity, ltype, anchor,)
function in OpenCV is used to label the connected components in a binary image, where the input
parameters are

Semester: Fall-2024 Instructor: Dr. Junaid Mir


Session: 2K21 Designed by: Dr. Junaid Mir
UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA
DEPARTMENT OF ELECTRICAL ENGINEERING

• image [required] is the 8bit single-channel input image to be labeled


• labels [optional] parameter labels the destination labeled image
• connectivity [optional] is set to 8 or 4 for 8-way or 4-way connectivity, respectively
• ltype [optional] sets the datatype of the output labeled image. Can be set to
cv2.CV_32S – signed 32-bit integers (default)
cv2.CV_16U – unsigned 16-bit integers
cv2.CV_8S – unsigned 8-bit integers

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).

6. Read and display Fig0918(a)(Chickenfilet with bones).tif in PYTHON.


a. Binarize the grayscale image using a suitable threshold value to retain most of the bone
fragments present in the image. Display the binary image.
b. Perform the connected component analysis using the cv2.connectedComponents
command. What are the total number of objects present in the image.
c. Retain the three largest objects present in the image and remove all the remaining objects.

7. Read and display the image circles.tif .


a. How many objects (circles) can you count in this image?
b. If we use the connected component analysis, how many objects will be returned? Count
the number of objects in the image. Is there any difference when you use 4-connectivity
in contrast to the default 8-connectivity?
c. Pre-process the image to get the correct number of objects present in the image.

8. What does the cv2.MORPH_TOPHAT and cv2.MORPH_BLACKHAT options do in


cv2.morphologyEx command. Understand and perform these morphological operations on
image of your choice. Define these two operations as well.

Semester: Fall-2024 Instructor: Dr. Junaid Mir


Session: 2K21 Designed by: Dr. Junaid Mir

You might also like