You are on page 1of 18

IMAGE EROSION AND DILATION USING THE PYTHON

(20BM281) MEDICAL IMAGE PROCESSING


LABORATORY

A CAPSTONE PROJECT REPORT

Submitted by

(2007006) ASLAM PACKIR K

in partial fulfillment for the award of the

degree of

BACHELOR OF ENGINEERING

in

BIOMEDICAL ENGINEERING

SRI RAMAKRISHNA ENGINEERING


COLLEGE, COIMBATORE-641022
SRI RAMAKRISHNA ENGINEERING COLLEGE
COIMBATORE-641022

BONAFIDE CERTIFICATE

Certified that this project report “IMAGE EROSION AND DILATION USING
PYTHON” is the bonafide work of ASLAM PACKIR K who carried out the
project work under my supervision.

Dr.V.Ramamurthy Mrs .S. Sree Sanjanaa Bose(AP)


Head of the Department Supervisor
Department of Biomedical Engineering Department of Biomedical Engineering
Sri Ramakrishna Engineering College Sri Ramakrishna Engineering College

1
TABLE OF CONTENTS
CHAPTER TITLE PAGE NO
NO
1 Introduction 5
1.1 Image processing 5
1.2 Python 5
1.3 Anaconda 7
1.4 Spyder 7
1.5 Morphological operations 8
1.5.1 Dilation 8
1.5.2 Erosion 9
1.5.3 Opening 10
1.5.4 Closing 10

2 Methodologies 11
2.1 Required libraries to perform Preprocessing 11
2.2Installation 11
2.3 Loading the Original Image 12
2.4 Resizing the Original Image 12
2.5 Erosion Function 12
2.6 Dilation Function 12

3 Results and Discussion 13


3.1 Original Image 13
3.2 Grayscale Image 13
3.3 Dilated Image 14
3.4 Erosion Image 14

4 Conclusion 15

5 References 16

6 Appendix 17

2
LIST OF FIGURES

S. NO Figure No Name of the Figure Page


No
1 1.5.1.1 Dilated image 9
2 1.5.2.1 Eroded image 9
3 1.5.3.1 Opening image 10
4 1.5.4.1 Closing image 10
5 2.2.1 Installation Command 11
6 2.2.2 Syntax for checking successful Installation of cv2 package 11
7 2.2.3 Syntax for NumPy library existence confirmation 11
8 2.3.1 Loading original Image code snippet 12
9 2.4.1 Resizing the image 12
10 2.5.1 Erosion Code snippet 12
11 2.6.1 Dilation code snippet 12
12 3.1.1 Original RGB image 13
13 3.2.1 Grayscale image 13
14 3.3.1 Dilated image 14
15 3.4.1 Erosion applied image 14
16 6.1 Code snippet for Erosion and dilution 17

3
LIST OF ABBREVIATIONS

RGB Red Green Blue

PIL Python Imaging Library

OS Operating System

4
CHAPTER 1
INTRODCUTION
1.1 Image processing
Information of an image can be obtained with the help of the technique known as Image Processing.
It is the core part of computer vision which plays a crucial role in many real-world examples like
robotics, self-driving cars, and object detection. Image processing allows us to transform and
manipulate thousands of images at a time and extract useful insights from them. It has a wide range
of applications in almost every field. 

Python is one of the widely used programming languages for this purpose. Its amazing libraries and
tools help in achieving the task of image processing very efficiently. Image processing means
processing the image and this may include many different techniques until we reach our goal. The
final output can be either in the form of an image or a corresponding feature of that image. This can
be used for further analysis and decision making.

An image can be represented as a 2D function F(x,y) where x and y are spatial coordinates. The
amplitude of F at a particular value of x,y is known as the intensity of an image at that point. If x,y,
and the amplitude value is finite then we call it a digital image. It is an array of pixels arranged in
columns and rows. Pixels are the elements of an image that contain information about intensity and
colour. An image can also be represented in 3D where x,y, and z become spatial coordinates. Pixels
are arranged in the form of a matrix. This is known as an RGB image.

There are various types of images:

 RGB image: It contains three layers of 2D image, these layers are Red, Green, and Blue channels.
 Grayscale image: These images contain shades of black and white and contain only a single channel.

1.2 Python

Python is a very popular general-purpose interpreted, interactive, object-oriented, and high-level


programming language. Python is dynamically-typed and garbage-collected programming language.
It was created by Guido van Rossum during 1985- 1990. Like Perl, Python source code is also

5
available under the GNU General Public License (GPL). Python becomes an apt choice for such
Image processing tasks. This is due to its growing popularity as a scientific programming language
and the free availability of many State of Art Image Processing tools in its ecosystem.

Python provides lots of libraries for image processing, including

 OpenCV − Image processing library mainly focused on real-time computer vision with
application in wide-range of areas like 2D and 3D feature toolkits, facial & gesture
recognition, Human-computer interaction, Mobile robotics, Object identification and others.
 Numpy and Scipy libraries − For image manipulation and processing.
 Python Imaging Library (PIL) − To perform basic operations on images like create
thumbnails, resize, rotation, convert between different file formats etc.
 Skimage - Provides lots of algorithms for image processing which includes algorithms
for segmentation, geometric transformations, colour space manipulation, analysis, filtering,
morphology, feature detection, and more. It is designed to interoperate with the Python
numerical and scientific libraries NumPy and SciPy.
 Matplotlib -Matplotlib is an amazing visualization library in Python for 2D plots of arrays.
Matplotlib is a multi-platform data visualization library built on NumPy arrays and
designed to work with the broader SciPy stack. It was introduced by John Hunter in the year
2002.One of the greatest benefits of visualization is that it allows us visual access to huge
amounts of data in easily digestible visuals. Matplotlib consists of several plots like line,
bar, scatter, histogram etc.

6
1.3 Anaconda

The world’s most popular open-source Python distribution platform that can be used for projects
in any field, from data visualization to robotics.One can easily search and install packages and
create, load, and switch between environments.

The packages are securely hosted and artifacts are methodically tested and regularly updated. Its
repository features over 8,000 open-source data science and machine learning packages,
Anaconda-built and compiled for all major operating systems and architectures. Conda is an
open-source package and environment management system that runs on Windows, macOS, and
Linux.

Conda quickly installs, runs, and updates packages and their dependencies. It also easily creates,
saves, loads, and switches between environments on your local computer. It was created for
Python programs, but it can package and distribute software for any language. Anaconda
Navigator

let the user to easily manage integrated applications, packages, and environments without using
the command line. Anaconda Nucleus securely stores the user’s local environments in the cloud.
Anaconda Distribution equips individuals to easily search and install thousands of Python/R
packages and access a vast library of community content and support.

1.4 Spyder

Spyder is an open-source cross-platform IDE. The Python Spyder IDE is written completely in
Python. It is designed by scientists and is exclusively for scientists, data analysts, and engineers. It
is also known as the Scientific Python Development IDE and has a huge set of remarkable
features.

Some of the remarkable features of Spyder are:

 Customizable Syntax Highlighting

7
 Availability of breakpoints (debugging and conditional breakpoints)

 Interactive execution which allows you to run line, file, cell, etc.

 Run configurations for working directory selections, command-line options, current/


dedicated/ external console, etc

 Can clear variables automatically ( or enter debugging )

 Navigation through cells, functions, blocks, etc can be achieved through the Outline Explorer

 It provides real-time code introspection (The ability to examine what functions, keywords,
and classes are, what they are doing and what information they contain)

 Automatic colon insertion after if, while, etc

 Supports all the IPython magic commands

 Inline display for graphics produced using Matplotlib

 Also provides features such as help, file explorer, find files, etc

1.5 MORPHOLOGICAL OPERATIONS

Morphology is a broad set of image processing operations that process images based on shapes.
Morphological operations apply a structuring element 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 neighbours.

1.5.1 DILATION

Morphological dilation makes objects more visible and fills in small holes in objects. Lines
appear thicker, and filled shapes appear larger.

8
Fig 1.5.1.1 dilated image

In figure 1.5.1.1 the value of the output pixel is the maximum value of all pixels in the
neighbourhood. In a binary image, a pixel is set to 1 if any of the neighbouring pixels have the
value 1.

1.5.2 EROSION

Morphological erosion removes floating pixels and thin lines so that only substantive objects
remain. Remaining lines appear thinner and shapes appear smaller.

Fig 1.5.2.1 eroded image

In figure 1.5.1.2 the value of the output pixel is the minimum value of all pixels in the
neighbourhood. a pixel is set to 0 if any of the neighbouring pixels have the value 0.

9
1.5.3 OPENING
Opening is a process in which first erosion operation is performed and then dilation operation
is performed. It eliminates the thin protrusions of the obtained image. Opening is used for
removing internal noise of the obtained image

Opening is denoted by:-


A∘B=(A⊖B) ⊕B

Fig 1.5.3.1 opening image

In figure 1.5.3.1 f is the input image, B is the structuring element, f⊖B is the eroded image f∘B
is the opened image where dilation is done to The eroded image.

1.5.4 CLOSING
Closing is a process in which first dilation operation is performed and then erosion operation is
performed. It eliminates the small holes from the obtained image. Closing is used for
smoothening of contour and fusing of narrow breaks.
Closing is represented by:-
A •B=(A⊕B) ⊖B

Fig 1.5.4.1 closing image


In fig 1.5.4.1 f is the original image, B is the structuring element. f⊕B is the dilated image of the
original image. f•B is the eroded image of the dilated image.

10
CHAPTER-2
METHODOLOGIES
2.1Required Libraries to Perform Preprocessing

 Opencv
 Numpy

2.2 Installation

Fig 2.2.1: Installation Command

The figure 2.2.1 shows the message from anaconda terminal that it is already existed.

Checking the successful installation of OpenCV:

Fig 2.2.2 Syntax for checking successful Installation of cv2 package

The figure 2.2.2 confirms that cv2 library is successfully installed and its version is displayed in
the console.

Checking the successful installation of NumPy library:

Fig 2.2.3: Syntax for NumPy library existence confirmation

The figure 2.2.3 shows that NumPy package was successfully installed and also its version is
displayed in the console.

11
2.3 Loading the original image

Fig 2.3.1: Loading original Image code snippet

The figure 2.4 shows the code snippet to load an RGB and Grayscale images through Opencv
library.
2.4 Resizing the original image

Fig2.4.1 resizing the image

The figure 2.4.1 shows the code snippet to resize the original image to 400*400 pixel image

2.5 Erosion function

Fig 2.5.1 Erosion Code snippet

The Fig 2.5 shows the code to apply erosion function to the image. The parameters inside the
erosion function are image which we given as input and kernel is structuring element .

2.6 Dilation Function

Fig 2.6.1 Dilation code snippet

The figure 2.6.1 shows the dilation function that is applied to that image.

CHAPTER-3
12
RESULTS AND DISCUSSION
3.1 ORIGINAL IMAGE

Fig3.1.1 original RGB image


The figure 3.1 shows the loaded RGB image on which we need to perform the morphological
operations.
3.2 GRAYSCALE IMAGE

Fig3.2.1 grayscale image

The fig 3.1.1 shows the RGB to Grayscale converted image.

13
3.3 DILATED IMAGE

Fig 3.3.1 dilated image

The fig 3.3.1 shows the output of dilation operation.

3.4 EROSION IMAGE

Fig 3.4.1 the erosion applied image

The above figure shows the output image after erosion.

14
CHAPTER 4

CONCLUSION

Morphological image processing (or morphology) describes a range of image processing


techniques that deal with the shape (or morphology) of features in an image. Morphological
operations are typically applied to remove imperfections introduced during segmentation, and
they are typically operated on binary (where the pixels of the image can only be 0 or 1) images.
Dilation can repair breaks and missing pixels in foreground objects. The effects of dilation are
the opposite of erosion. Dilation adds pixels on object boundaries. Fill the holes in the
foreground and enlarge foreground objects. Erosion removes pixels on object boundaries.
it shrinks the foreground objects. Enlarge foreground holes. Like in Image Processing Kernels, a
larger size of the Structure Element, the effect of erosion increase. A different Structure Element
gives different outputs on the same input image.

15
CHAPTER - 5
REFERENCES
1. https://www.mathworks.com/help/images/morphological-dilation-and-erosion.html
2. https://www.geeksforgeeks.org/difference-between-opening-and-closing-in-digital-
image-processing/
3. https://towardsdatascience.com/image-processing-part-3-dbf103622909

4. mailto:1. https://medium.com/@ami25480/morphological-image-processing-
operations-dilation-erosion-opening-and-closing-with-and-without-c95475468fca

16
CHAPTER – 6

APPENDIX

Fig 6.1 Code snippet for Erosion and dilution


import cv2
import numpy as np
img = cv2.imread('B1.png', 1)
img1=cv2.imread('B1.png',0)
image=cv2.resize(img1,(400,400))
image1=cv2.resize(img,(400,400))
kernel = np.ones((5,5), np.uint8)
img_erosion = cv2.erode(image, kernel, iterations=1)
img_dilation = cv2.dilate(image, kernel, iterations=1)
cv2.imshow('Input', image1)
cv2.imshow('grayscale',image)
cv2.imshow('Dilation', img_dilation)
cv2.imshow('Erosion',img_erosion)
cv2.waitKey(0)
cv2.destroyAllWindows()

17

You might also like