You are on page 1of 39

EXPERIMENT NUMBER 9

MORPHOLOGICAL OPERATION – EROSION,


EXPERIMENT NAME DILATION, OPENING, CLOSING, HIT-MISS
TRANSFORM, BOUNDARY EXTRACTION

DATE OF PERFORMANCE

DATE OF CORRECTION

SIGN & DATE

GRADE

TIMLEY LAB
ATTENDANC
CORRECTIO PERFORMANC ORAL TOTAL
E
N E

ALLOTTED 2 2 3 3 10

OBTAINED
EXPERIMENT NO: 9

⮚ NAME OF EXPT:
Morphological operation – Erosion, dilation, opening, closing, hit-miss transform,
Boundary extraction

⮚ AIM OF EXPT:
Implementation of Morphological operation – Erosion, dilation, opening, closing, hit-
miss transform, Boundary extraction

⮚ EQUIPMENT/COMPONENTS:
IMAGE PROCESSING TOOLBOX (MATLAB/Python)

THEORY:
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. By choosing the size and shape of the neighbourhood, you can construct a
morphological operation that is sensitive to specific shapes in the input image. In order to
understand morphology operations, it is necessary to get used to following concepts of set
theory. Let 𝑍 2 represents two-dimensional integer space and A is set of 𝑍 2 .
1. If 𝑎(𝑥, 𝑦)is an element of A:
𝑎∈𝐴

2. If every element of B belongs to A:


𝐵∈𝐴

3. Compliment of A is set of elements not contained in A:


𝐴𝑐 = {𝑤|𝑤 ∉ 𝐴}

4. Difference of two sets:


𝐴 − 𝐵 = {𝑤 ∈ 𝐴, 𝑤 ∉ 𝐵}

5. Reflection of set B is given as:


B^ = {𝑤|𝑤 = −𝑏, 𝑓𝑜𝑟 𝑏 ∈ 𝐵}

6. Translation of set A by point z:


(A)z= {𝑐|𝑐 = 𝑎 + 𝑧, 𝑓𝑜𝑟 𝑎 ∈ 𝐴}

Various types of morphological operations can be performed on image according to our


requirements. Such as;
⮚ Dilation
⮚ Erosion
⮚ Opening
⮚ Closing

Dilation:
The basic effect of the operator on a binary image is to gradually enlarge the boundaries of
regions of foreground pixels (i.e. white pixels, typically). Thus areas of foreground pixels grow
in size while holes within those regions become smaller.
If at least one pixel in the structuring element coincides with a foreground pixel in the image
underneath, then the input pixel is set to the foreground value. If all the corresponding pixels
in the image are background, however, the input pixel is left at the background value.
With A and B as sets in 𝑍 2 , the dilation of A by B, denoted by 𝐴 ⨁𝐵, is defined as;

𝐴⨁𝐵 = {𝑧|(𝐵^)z∩ 𝐴 ≠ 𝜙}

This equation is based on reflecting B about its origin, and shifting this reflection by z.
Dilation of A and B is set of all displacements z such that B^ and A overlap by at least one
element. Based on this interpretation above equation can also be written as;

We refer to B as structuring element where A is the image to be dilated.

Erosion:
The basic effect of the operator on a binary image is to erode away the boundaries of regions
of foreground pixels (i.e. white pixels, typically). Thus areas of foreground pixels shrink in size,
and holes within those areas become larger.
If for every pixel in the structuring element, the corresponding pixel in the image underneath
is a foreground pixel, then the input pixel is left as it is. If any of the corresponding pixels in
the image are background, however, the input pixel is also set to background value.

With A and B as sets in 𝑍 2 , the erosion of A by B, denoted by 𝐴 ⊖ 𝐵, is defined as;


In words, this equation indicates that erosion of A by B is the set of all points z such that B,
translated by z, is contained in A. As B has to be contained in A in equivalent to B not sharing
any common element with the background, we can represent erosion in the following
equivalent format.

Where 𝐴𝑐 is complement of A and 𝜙 is null set. We refer to B as structuring element where A


is the image to be eroded.

Opening:

The basic effect of an opening is somewhat like erosion in that it tends to remove some of the
foreground (bright) pixels from the edges of regions of foreground pixels. However it is less
destructive than erosion in general. As with other morphological operators, the exact
operation is determined by a structuring element. The effect of the operator is to preserve
foreground regions that have a similar shape to this structuring element, or that can
completely contain the structuring element, while eliminating all other regions of foreground
pixels.
An opening is defined as erosion followed by a dilation using the same structuring element for
both operations.
All pixels which can be covered by the structuring element with the structuring element being
entirely within the foreground region will be preserved. However, all foreground pixels which
cannot be reached by the structuring element without parts of it moving out of the foreground
region will be eroded away.
The opening of set A by structuring element B, denoted as 𝐴 ∘ 𝐵, is defined as;

Thus opening A by B, is erosion of A by B, followed by dilation of the result by B.

Closing:

Closing is similar in some ways to dilation in that it tends to enlarge the boundaries of
foreground (bright) regions in an image (and shrink background color holes in such regions),
but it is less destructive of the original boundary shape. As with other morphological
operators, the exact operation is determined by a structuring element. The effect of the
operator is to preserve background regions that have a similar shape to this structuring
element, or that can completely contain the structuring element, while eliminating all other
regions of background pixels.
Closing is opening performed in reverse. It is defined simply as dilation followed by erosion
using the same structuring element for both operations.
For any background boundary point, if the structuring element can be made to touch that
point, without any part of the element being inside a foreground region, then that point
remains background. If this is not possible, then the pixel is set to foreground. After the closing
has been carried out the background region will be such that the structuring element can be
made to cover any point in the background without any part of it also covering a foreground
point, and so further closings will have no effect.
The closing of set A by structuring element B, denoted as 𝐴 • 𝐵, is defined as;

Thus closing A by B, is dilation of A by B, followed by erosion of the result by B.

ALGORITHM:
1. Read an image and store it in variable.
2. Enter structuring element and store it in variable.
3. Compute size of image and store it in r and c.
4. Convert 2D structuring element into 1D array.
5. Perform dilation.
a. Compare neighboring elements of each pixel of image with 1D array.
b. If any element matches with the respective element in array, make center pixel 1.
c. Else leave center pixel as it is.
d. Do this for all possible pixel values of given image.
e. Store result of each iteration in another matrix.
f. Display resultant matrix.

6. Perform erosion.
a. Compare neighboring elements of each pixel of image with 1D array.
b. If all elements match with respective elements in array, make center pixel 1.
c. Else make every pixel in neighborhood and center 0.
d. Do this for all possible pixel values of given image.
e. Store result of each iteration in another matrix.
f. Display resultant matrix.
7. Perform Opening.
a. Perform erosion on the given image.
b. Perform dilation on the resultant image.
c. Display final resultant image.
8. Perform Closing.
a. Perform dilation on the given image.
b. Perform erosion on the resultant image.
c. Display final resultant image.
9. Perform all above operations using in-built commands and display result alongside.

CONCLUSION:

Erosion can also be used to remove small spurious bright spots (`salt noise‘ ) in images. We can
also use erosion for edge detection by taking the erosion of an image and then subtracting it
away from the original image, thus highlighting just those pixels at the edges of objects that
were removed by the erosion. Finally, erosion is also used as the basis for many other
mathematical morphology operators.

Dilation is the dual of erosion i.e. dilating foreground pixels is equivalent to eroding the
background pixels. Dilation can also be used for edge detection by taking the dilation of an
image and then subtracting away the original image, thus highlighting just those new pixels at
the edges of objects that were added by the dilation.
Opening isolates the objects in an image which may be just touching one another.
Closing is used to fill gaps and helps to fuse narrow breaks in an image.

REFERENCES:

1. Digital Image Processing – Gonzalez Woods.


2. Digital Image Processing Using Matlab – Gonzalez Woods.
3. Digital Image Processing – Pratt
4. Fundamentals of digital Image Processing – S. Annadurai

PROGRAM:
1) Erosion & Dilation
# Python program to demonstrate erosion
and # dilation of images.
import cv2
import numpy as np

# Reading the input image


img =
cv2.imread('nature.jpg')

# Taking a matrix of size 5 as the kernel


kernel = np.ones((5,5), np.uint8)

# The first parameter is the original


image, # kernel is the matrix with which
image is
# convolved and third parameter is the
number # of iterations, which will determine
how much # you want to erode/dilate a given
image.
img_erosion = cv2.erode(img, kernel, iterations=1)
img_dilation = cv2.dilate(img, kernel, iterations=1)

cv2.imshow('Input', img)
cv2.imshow('Erosion', img_erosion)
cv2.imshow('Dilation', img_dilation)
cv2.waitKey(0)
Output:

2) Opening

import cv2
import numpy as np

# Reading the input image


img = cv2.imread('download.jpg',
0) kernel =
np.ones((5,5),np.uint8)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN,
kernel) cv2.imshow('Input', img)
cv2.imshow('Opening',
opening) cv2.waitKey(0)
Output:

3) Closing

import cv2
import numpy as np

# Reading the input image


img = cv2.imread('download.jpg', 0)
kernel = np.ones((5,5),np.uint8)
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
cv2.imshow('Input', img)
cv2.imshow('Opening', closing)
cv2.waitKey(0)

Output:
4) Hit-miss transform

# importing required libraries


import mahotas as mh
import numpy as np
from pylab import imshow, show

# creating
region #
numpy.ndarray
regions = np.zeros((10, 10), bool)

# setting 1 value to the


region regions[1, :2] = 1
regions[5:8, 6: 8] = 1
regions[8, 0] = 1

# showing the image with interpolation = 'nearest'


print("Image")
imshow(regions, interpolation ='nearest') show()
# template for hit miss template = np.array([

[0, 1, 1],
[0, 1, 1],
[0, 1, 1]])
(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

# hit miss transform


img = mahotas.hitmiss(regions, template)

# showing image
print("Image after hit
miss transform")
imshow(img)
show()

Output:
Image Image after hit miss transform

5) Boundar
y extraction
import
numpy as np
import cv2
from matplotlib import pyplot as plt

image = cv2.imread('letter_A.jpg',0)
retVal,mask =
cv2.threshold(image,155,255,cv2.THRESH_BINARY_INV)
kernel = np.ones((7,7),np.uint8)
gradient = cv2.morphologyEx(mask,
cv2.MORPH_GRADIENT, kernel) titles = ['Original
Image',"Binary Image",'Morphological gradient'] images =
[image,mask,gradient]
plt.figure(figsize=(13,5))
for i in range(3):

LABORATORY: IMAGE PROCESSING & MACHINE VISION |11


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

plt.subplot(1,3,i+1)
plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([])
plt.yticks([])
plt.tight_layout()
plt.show()

Output:

LABORATORY: IMAGE PROCESSING & MACHINE VISION |12


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

EXPERIMENT NUMBER 10

EXPERIMENT NAME DETECT EDGE USING CANNY EDGE DETECTION

DATE OF PERFORMANCE

DATE OF CORRECTION

SIGN & DATE

GRADE

TIMLEY LAB
ATTENDANCE ORAL TOTAL
CORRECTION PERFORMANCE

ALLOTTED 2 2 3 3 10

OBTAINED

LABORATORY: IMAGE PROCESSING & MACHINE VISION |13


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

EXPERIMENT NO: 10

⮚ NAME OF EXPT:
Detect Edge Using Canny Edge Detection

⮚ AIM OF EXPT:
Detect Edge Using Canny Edge Detection

⮚ EQUIPMENT/COMPONENTS:
IMAGE PROCESSING TOOLBOX (MATLAB/Python)

THEORY:
The Canny edge detector is an edge detection operator that uses a multi- stage
algorithm to detect a wide range of edges in images.
The Canny filter is a multi-stage edge detector. It uses a filter based on the derivative
of a Gaussian in order to compute the intensity of the gradients. The Gaussian reduces
the effect of noise present in the image. Then, potential edges are thinned down to 1-
pixel curves by removing non-maximum pixels of the gradient magnitude. Finally, edge
pixels are kept or removed using hysteresis thresholding on the gradient magnitude.
The Canny has three adjustable parameters: the width of the Gaussian (the noisier the
image, the greater the width), and the low and high threshold for the hysteresis
thresholding.
The general criteria for edge detection include:
1. Detection of edge with low error rate, which means that the detection should
accurately catch as many edges shown in the image as possible
2. The edge point detected from the operator should accurately localize on the center of
the edge.
3. A given edge in the image should only be marked once, and where possible, image noise
should not create false edges.

ALGORITHM:
1) Apply Gaussian Filter to smooth the image and remove the noise.

LABORATORY: IMAGE PROCESSING & MACHINE VISION |14


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

2) Find the image intensity gradients.


3) To get rid of spurious edge detection responses, use non maximum suppression.
4) To find probable edges use double threshold method.
5) Suppress all other edges that are weak and not connected to strong edges to
complete edge identification process.

PROGRAM:
import numpy as np
import os
import cv2
import matplotlib.pyplot as plt
from google.colab import files

uploaded = files.upload()

# defining the canny detector function

# here weak_th and strong_th are thresholds for


# double thresholding step
def Canny_detector(img, weak_th = None, strong_th = None):

# conversion of image to grayscale


img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Noise reduction step


img = cv2.GaussianBlur(img, (5, 5), 1.4)

# Calculating the gradients


gx = cv2.Sobel(np.float32(img), cv2.CV_64F, 1, 0, 3)
gy = cv2.Sobel(np.float32(img), cv2.CV_64F, 0, 1, 3)

# Conversion of Cartesian coordinates to polar


mag, ang = cv2.cartToPolar(gx, gy, angleInDegrees = True)

# setting the minimum and maximum thresholds


# for double thresholding
mag_max = np.max(mag)
if not weak_th:weak_th = mag_max * 0.1
if not strong_th:strong_th = mag_max * 0.5

# getting the dimensions of the input image


height, width = img.shape

LABORATORY: IMAGE PROCESSING & MACHINE VISION |15


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

# Looping through every pixel of the grayscale


# image
for i_x in range(width):
for i_y in range(height):

grad_ang = ang[i_y, i_x]


grad_ang = abs(grad_ang-
180) if abs(grad_ang)>180 else abs(grad_ang)

# selecting the neighbours of the target pixel


# according to the gradient direction
# In the x axis direction
if grad_ang<= 22.5:
neighb_1_x, neighb_1_y = i_x-1, i_y
neighb_2_x, neighb_2_y = i_x + 1, i_y

# top right (diagnol-1) direction


elif grad_ang>22.5 and grad_ang<=(22.5 + 45):
neighb_1_x, neighb_1_y = i_x-1, i_y-1
neighb_2_x, neighb_2_y = i_x + 1, i_y + 1

# In y-axis direction
elif grad_ang>(22.5 + 45) and grad_ang<=(22.5 + 90
):
neighb_1_x, neighb_1_y = i_x, i_y-1
neighb_2_x, neighb_2_y = i_x, i_y + 1

# top left (diagnol-2) direction


elif grad_ang>(22.5 + 90) and grad_ang<=(22.5 + 13
5):
neighb_1_x, neighb_1_y = i_x-1, i_y + 1
neighb_2_x, neighb_2_y = i_x + 1, i_y-1

# Now it restarts the cycle


elif grad_ang>(22.5 + 135) and grad_ang<=(22.5 + 1
80):
neighb_1_x, neighb_1_y = i_x-1, i_y
neighb_2_x, neighb_2_y = i_x + 1, i_y

# Non-maximum suppression step


if width>neighb_1_x>= 0 and height>neighb_1_y>= 0:
if mag[i_y, i_x]<mag[neighb_1_y, neighb_1_x]:

LABORATORY: IMAGE PROCESSING & MACHINE VISION |16


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

mag[i_y, i_x]= 0
continue

if width>neighb_2_x>= 0 and height>neighb_2_y>= 0:


if mag[i_y, i_x]<mag[neighb_2_y, neighb_2_x]:
mag[i_y, i_x]= 0

weak_ids = np.zeros_like(img)
strong_ids = np.zeros_like(img)
ids = np.zeros_like(img)

# double thresholding step


for i_x in range(width):
for i_y in range(height):

grad_mag = mag[i_y, i_x]

if grad_mag<weak_th:
mag[i_y, i_x]= 0
elif strong_th>grad_mag>= weak_th:
ids[i_y, i_x]= 1
else:
ids[i_y, i_x]= 2

# finally returning the magnitude of


# gradients of edges
return mag

#img=Image.open(BytesIO(uploaded['outimage.jpg']))

#img = cv2.imread('lion.jpg')
#frame = cv2.imread('test.jpeg')
frame = cv2.imread('outimage.jpg')

# calling the designed function for


# finding edges
canny_img = Canny_detector(frame)

# Displaying the input and output image


plt.figure()
f, plots = plt.subplots(2, 1)

LABORATORY: IMAGE PROCESSING & MACHINE VISION |17


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

plots[0].imshow(frame)
plots[1].imshow(canny_img)

LABORATORY: IMAGE PROCESSING & MACHINE VISION |18


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

EXPERIMENT NUMBER 11

EXPERIMENT NAME GENERATE 8 NEIGHBOR CHAIN CODE

DATE OF PERFORMANCE

DATE OF CORRECTION

SIGN & DATE

GRADE

LABORATORY: IMAGE PROCESSING & MACHINE VISION |19


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

TIMLEY LAB
ATTENDANCE ORAL TOTAL
CORRECTION PERFORMANCE

ALLOTTED 2 2 3 3 10

OBTAINED

EXPERIMENT NO: 11

⮚ NAME OF EXPT:
Chain Code

⮚ AIM OF EXPT:
Generate 8 neighbor Chain Code

⮚ EQUIPMENT/COMPONENTS:
IMAGE PROCESSING TOOLBOX (MATLAB/Python)

THEORY:
Chain code is a lossless compression technique used for representing an object in
images. The co-ordinates of any continuous boundary of an object can be
represented as a string of numbers where each number represents a particular
direction in which the next point on the connected line is present. One point is
taken as the reference/starting point and on plotting the points generated from
the chain, the original figure can be re-drawn.
Chain codes are used to represent the binary by a connected sequence of straight
–line segments. This represented is based on 4-connectivity and 8-connectivity of
the segments.

LABORATORY: IMAGE PROCESSING & MACHINE VISION |20


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

The chain code works best with binary images and is a concise way of representing a
shape contour. The chain code direction convention is given below:

As an edge is traced from its beginning point to the end point the direction that must
be taken to move from one pixel to the next is given by the number represented in
either the 4-chain code or the 8-chain code.
As an edge can be completely described in terms of its starting coordinate and its
sequence of chain codes descriptors. Of the two chain codes, the 4-chain is easier
requiring only four different code values.

ALGORITHM:

1. Read the image.


2. Perform Chain Code for 2D Line
3. Display the result

THEORETICAL CALCULATION:

CONCLUSION:

REFERENCES:

1. Digital Image Processing – Gonzalez Woods.


2. Digital Image Processing Using Matlab – Gonzalez Woods.

LABORATORY: IMAGE PROCESSING & MACHINE VISION |21


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

3. Digital Image Processing – Pratt


4. Fundamentals of digital Image Processing – S. Annadurai

PROGRAM:

# Python3 code for generating 8-


neighbourhood chain # code for a 2-D
line

codeList = [5, 6, 7, 4, -1, 0, 3, 2, 1]


codeList = [5, 6, 7, 4, -1, 0, 3, 2, 1]

# This function generates the chaincode


# for transition between two neighbour points
def getChainCode(x1, y1, x2, y2):
dx = x2 - x1 dy = y2 - y1
hashKey = 3 * dy + dx + 4 return
codeList[hashKey]

# This function generates the chaincode


# for transition between two
neighbour points def
getChainCode(x1, y1, x2, y2):
dx=x2 - x1 dy = y2 - y1
hashKey = 3 * dy + dx + 4 return codeList[hashKey]

'''This function generates the list of chaincodes


for given list of points''' def
generateChainCode(ListOfPoints):
chainCode = []
for i in
range(len(ListOf

LABORATORY: IMAGE PROCESSING & MACHINE VISION |22


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Points) - 1): a =
ListOfPoints[i]
b = ListOfPoints[i + 1]
chainCode.append(getChainCode(a[0],
a[1], b[0], b[1]))
return chainCode

'''This function generates the list of points for a straight


line using Bresenham's Algorithm''' def
Bresenham2D(x1, y1, x2, y2):
ListOfPoints = [] ListOfPoints.append([x1, y1])
xdif = x2 - x1
ydif = y2 - y1 dx = abs(xdif) dy = abs(ydif) if(xdif
> 0):
xs = 1
else:
xs = -1
if (ydif > 0):
ys = 1

else:
ys = -1
if (dx > dy):

# Driving axis
is the X-axis p =
2 * dy - dx
while (x1 != x2):
x1 += xs
if (p >= 0):
y1 += ys
p
-= 2 *
dx p
+= 2 *
dy
ListOfPoints.append([x1, y1])
else:

# Driving axis
is the Y-axis p =
2 * dx-dy

LABORATORY: IMAGE PROCESSING & MACHINE VISION |23


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

while(y1 != y2):
y1 += ys
if (p >= 0):
x1 += xs
p
-= 2 *
dy p
+= 2 *
dx
ListOfPoints.appe
nd([x1, y1]) return ListOfPoints

def DriverFunction():
(x1, y1) = (-9, -3)
(x2, y2) = (10, 1)
ListOfPoints = Bresenham2D(x1, y1, x2,
y2) chainCode =
generateChainCode(ListOfPoints)
chainCodeString = "".join(str(e) for e in
chainCode) print ('Chain code for the
straight line from', (x1, y1),
'to', (x2, y2), 'is', chainCodeString)

DriverFunction()

OUTPUT:

LABORATORY: IMAGE PROCESSING & MACHINE VISION |24


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

EXPERIMENT NUMBER 12

DIGIT RECOGNITION USING MULTI-LAYER


EXPERIMENT NAME PERCEPTRON

DATE OF PERFORMANCE

DATE OF CORRECTION

SIGN & DATE

GRADE

LABORATORY: IMAGE PROCESSING & MACHINE VISION |25


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

TIMLEY LAB
ATTENDANCE ORAL TOTAL
CORRECTION PERFORMANCE

ALLOTTED 2 2 3 3 10

OBTAINED

EXPERIMENT NO: 12

⮚ NAME OF EXPT:
Digit Recognition using Multi-Layer Perceptron

⮚ AIM OF EXPT:
Digit Recognition using Multi-Layer Perceptron

⮚ EQUIPMENT/COMPONENTS:
IMAGE PROCESSING TOOLBOX (MATLAB/Python)

THEORY:
To implement a multilayer perceptron neural network for recognition of dot matrix
digits 6, 8, and 9. Assume each dot matrix of size 7x5.
Relate practical application of Neural Networks to concepts and methods studied in
theory

LABORATORY: IMAGE PROCESSING & MACHINE VISION |26


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Character recognition involves the translation of handwritten or typewritten text


captured by a scanner into machine-editable text. This technology has long been used
by libraries to make old documents available electronically. The task is made difficult
by variations in how the characters are drawn. Neural networks are particularly adapt
at overcoming this problem, because they can learn to extract the features that are most
relevant to the recognition of the characters.
Our goal hence is to create design a neural network that takes a dot matrix digit as an
input and tells us which digit it represents. The database contains artificially distorted
matrices of digits. Each digit is represented by 35 inputs which indicate the values of
the pixels arranged as a 7X5 matrix as shown in Fig. 2.

Six = [ 0 1 1 1 0
100 01
100 00
1111 0
1000 1
1000 1
0111 0]

LABORATORY: IMAGE PROCESSING & MACHINE VISION |27


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Eight =[ 0 1 1 1 0
100 0 1
100 0 1
011 1 0
100 0 1
100 0 1
011 1 0]

Nine =[ 0 1 1 1 0
100 0 1
100 0 1
011 1 1
000 0 1
100 0 1
011 1 0]

Fig. 2. Digits 6, 8 and 9 in Dot matrix (size = 7x5) form

Fig. 3 Architecture of MLPNN [3]

ALGORITHM:
1. Training: Define the digit matrices, and train the network using relevant
parameters.

LABORATORY: IMAGE PROCESSING & MACHINE VISION |28


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

2. Test the trained network with noisy input.


3. Study effect of target mse, number of epochs etc.
4. Study relevant plots.

CONCLUSION:
Comment on the noise tolerance, ease of design and speed of MLP.

PROGRAM:
%program for digit recognition

clc;
clear all;
close all;

echo on
pause;
six_m=[0 1 1 1 0;
1 0 0 0 1;
1 0 0 0 0;
1 1 1 1 0;
1 0 0 0 1;
1 0 0 0 1;
0 1 1 1 0];
imshow(~six_m,'InitialMagnification', 5000);
pause;
eight_m=[0 1 1 1 0;
1 0 0 0 1;
1 0 0 0 1;
0 1 1 1 0;
1 0 0 0 1;
1 0 0 0 1;
0 1 1 1 0];
imshow(~eight_m,'InitialMagnification', 5000);
pause;
nine_m=[0 1 1 1 0;
1 0 0 0 1;

LABORATORY: IMAGE PROCESSING & MACHINE VISION |29


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

1 0 0 0 1;
0 1 1 1 1;
0 0 0 0 1;
1 0 0 0 1;
0 1 1 1 0];
imshow(~nine_m,'InitialMagnification', 5000);
pause;
six=reshape(six_m',[35,1]);
eight=reshape(eight_m',[35,1]);
nine=reshape(nine_m',[35,1]);
I=[six,eight,nine];
T=eye(3);

net=newff(I,T,[20,20],{'logsig','tansig','tansig'});
pause
net.performFcn='mse';
net.trainParam.goal=0.001;
net.trainParam.show=20;
net.trainParam.epochs=50;

[net,tr]=train(net,I,T);
disp('End_of_train_no');

%%%%%%%%%%%%code for testing %%%%%%%%%%%%%%%%%

clc;
close all;

test_no=[0 0.3 0.1 0.1 0;


1 0 0 0 1;
1 0 0 0 1;
0 1 1 1 1;
0 0 0 0 1;
0 0 0 0 1;
0 1 0.2 1 0];
J=uint8(255*(test_no));
figure;
imshow(~J, 'InitialMagnification', 5000)
%imshow(imcomplement(J),'InitialMagnification',5000);
pause
test_no=reshape(test_no',[35,1]);
a2=sim(net,test_no);
m=max(a2);
result_node=find(m==a2);
result_char=result_node;
pause

LABORATORY: IMAGE PROCESSING & MACHINE VISION |30


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

J=I(:, result_char);
J=reshape(J,[5,7]);
J=J';
figure;
imshow(~J, 'InitialMagnification', 5000);
%imshow(imcomplement(J),'InitialMagnification',5000);
title('detected digit');

EXPECTED OUTPUT:

test_no= [0 .3 1 .1 0
1 0 0 0 1
1 0 0 0 1
0 1 1 1 1
0 0 0 0 1
1 0 0 0 1
0 1 .2 1 0 ]

LABORATORY: IMAGE PROCESSING & MACHINE VISION |31


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Noisy data of digit nine from testing data set in complemented form

LABORATORY: IMAGE PROCESSING & MACHINE VISION |32


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

EXPERIMENT NUMBER 13
CLASSIFICATION OF IRIS FLOWER DATASET
USING SVM CLASSIFIER USING PYTHON

EXPERIMENT NAME

DATE OF PERFORMANCE

DATE OF CORRECTION

SIGN & DATE

GRADE

TIMLEY LAB
ATTENDANCE ORAL TOTAL
CORRECTION PERFORMANCE

ALLOTTED 2 2 3 3 10

OBTAINED

LABORATORY: IMAGE PROCESSING & MACHINE VISION |33


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

EXPERIMENT NO: 13

⮚ NAME OF EXPT:
Classification of given dataset Using SVM Classifier using Python

⮚ AIM OF EXPT:
Classification of Iris Flower dataset Using SVM Classifier using Python

⮚ EQUIPMENT/COMPONENTS:
IMAGE PROCESSING TOOLBOX (MATLAB/Python)

THEORY:
MachineLearning is about learning to predict something or extracting knowledge from d
ata. ML is a part of artificial intelligence. ML algorithms build a model based on sample d
ata or known as training data and based upon the training data the algorithm can predic
t something on new data.
Categories of Machine Learning :
Supervised machine learning: Supervised machine learning are types of machine learnin
g that are trained on well-
labeled training data. Labeled data means the training data is already tagged with the co
rrect output.
Unsupervised machine learning: Unlike supervised learning, unsupervised learning doe
sn’t have any tagged data. It learned patterns from untagged data. Basically, it creates a
group of objects based on the input data/features.
Semisupervised machine learning: Semisupervised learning falls between supervised an
d unsupervised learning. It has a small amount of tagged data and a large amount of unt
agged data.lem using a supervised learning approach. We’ll use an algorithm cal “Suppo
rt vector machine”.

Applications of Machine Learning:


1. Speech Recognition: Speech recognition uses NLP (Natural Language Processing) to p
rocess human speech into ledwritten format and vice versa. Some examples are – Googl
e Assistant, Alexa, Siri.

2. Recommendation Engine: Using the past behavior of a human’s search data the recom
mendation engine can produce new data to cross-

LABORATORY: IMAGE PROCESSING & MACHINE VISION |34


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

sell products to customers. For example – Amazon product recommendations, Spotify m


usic recommendations.

3. Chatbot: Chatbots are used to give customer services without any human agent. It tak
es questions from users and based on the question it gives an answer as a response.

Classification of Iris Flower dataset Using SVM Classifier using Python


Iris flower classification is a very popular machine learning project. The iris data
set contains three classes of flowers, Versicolor, Setosa, Virginica, and each class contain
s 4 features, ‘Sepal length’, ‘Sepal width’, ‘Petal length’, ‘Petal width’. The aim of the iris fl
ower classification is to predict flowers based on their specific features.
In this application, we’ll solve the problem of classification by Support vector machine: A
support vector machine (also known as a support vector network) is a supervised
machine learning algorithm that analyzes data for classification and regression. SVMs are
one of the most robust classifications methods.
SVM approximates a separate line (Hyperplane) between two classes.
SVM algorithm finds the points closest to the line from both classes. These points are
known as support vectors. Then it computes the distance between the line and support
vectors. This distance is called the margin. The main goal is to maximize the margin. The
hyperplane which has the maximum margin is known as the optimal hyperplane.
SVM mainly supports binary classification natively. For multiclass classification, It
separates the data for binary classification and utilizes the same principle by breaking
down multi-classification problems into multiple binary classification problems.
Prerequisites:
● Numpy- 1.19.3
● Matplotlib- 3.3.2
● Seaborn – 0.11.1
● Pandas – 1.2.4
● Scikit-learn – 0.24.2
Steps to Classify Iris Flower:
1. Load the data
2. Analyze and visualize the dataset
3. Model training.
4. Model Evaluation.
5. Testing the model.

First, we’ve imported some necessary packages for the project.

LABORATORY: IMAGE PROCESSING & MACHINE VISION |35


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Numpy will be used for any computational operations. We’ll use Matplotlib and seaborn
for data visualization. Pandas help to load data from various sources like local storage,
database, excel file, CSV file, etc.
From this visualization, we can tell that iris-setosa is well separated from the other two
flowers. And iris virginica is the longest flower and iris setosa is the shortest. Now let’s
plot the average of each feature of each class.
Since we have already done a general analysis of this data in earlier lectures, let's go
ahead and move on to using the Naive Bayes Method to separate this data set into
multiple classes.
● create and fit the model
● continue by separating into training and testing sets:
● fit the model using the training data set:
● predict the outcomes from the Testing Set:

CONCLUSION :

The Iris flower dataset is classified using Gaussian Model using 97.36% accuracy.

Program:
#DataFlair Iris Flower Classification

LABORATORY: IMAGE PROCESSING & MACHINE VISION |36


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

# Import Packages
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
from pandas import Series,DataFrame
from sklearn import datasets
from sklearn import metrics
from sklearn.naive_bayes import GaussianNB
%matplotlib inline
# load the iris datasets
iris = datasets.load_iris()
# Grab features (X) and the Target (Y)
X = iris.data
Y = iris.target
# Show the Built-in Data Description
print(iris.DESCR)
iris = datasets.load_iris()
# Since this is a bunch, create a dataframe
iris_df=pd.DataFrame(iris.data)
iris_df['class']=iris.target

iris_df.columns=['sepal_len', 'sepal_wid', 'petal_len', 'petal_wid', 'c


lass']
iris_df.dropna(how="all", inplace=True) # remove any empty lines

#selecting only first 4 columns as they are the independent(X) variable


# any kind of feature selection or correlation analysis should be first
done on these
iris_X=iris_df.iloc[:,[0,1,2,3]]

sns.pairplot(iris_df, hue='class')

model = GaussianNB()

from sklearn.model_selection import train_test_split


# Split the data into Trainging and Testing sets
X_train, X_test, Y_train, Y_test = train_test_split(X, Y)

# Fit the training model


model.fit(X_train,Y_train)
# Predicted outcomes
predicted = model.predict(X_test)

# Actual Expected Outvomes


expected = Y_test
expected

print (metrics.accuracy_score(expected, predicted))

LABORATORY: IMAGE PROCESSING & MACHINE VISION |37


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Output:

Finally we can see the metrics for performance:

LABORATORY: IMAGE PROCESSING & MACHINE VISION |38


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

LABORATORY: IMAGE PROCESSING & MACHINE VISION |39

You might also like