What Is The Objective Of This Topic:
The objective of this topic is to apply morphological operations on binary images.
Morphological image processing is quite like spatial filtering. The structuring element just like a spatial mask is moved across every pixel in the original image to produce an output pixel. The value of this new pixel depends on the operation performed. Two basic morphological operations are erosion and dilation. Erosion shrinks the size of foreground (1-valued) objects; smooths object boundaries and removes small objects. In erosion, for each foreground pixel (also called input pixel):
1. Superimpose the structuring element on top of the input image so that the origin of the structuring element coincides with the input pixel position.
2. 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.
3. If any of the corresponding pixels in the image are background, however, the input pixel is also set to background value.
6. If all the corresponding pixels in the image are background, however, the input pixel is left at the background value.
1. Perform Erosion on Fig 1 such that all balls get separated from each other. Optional (you can further apply your connected component analysis algorithm to count total number of balls present in this image.
Code
import numpy as np
import cv2 as cv
#(1) : Take size of Structuring Element
def Structuring_Element():
size = int(input("Enter size of Structuring Element : "))
SE = np.ones((size, size), dtype=np.uint8)
return SE
#(2) : Take
image and padding size and add padding to image
def Padding(image,paddingsize):
padded_img =
np.pad(image,(paddingsize,paddingsize),'constant') #perform zero padding
return padded_img
# (3) : Take
image, padding size and Structuring Element and apply erosion
def Erosion(img,pad_size,SE):
dims = np.shape(SE) #dimensions of input Structuring Element
rows_SE = dims[0] #rows of input Structuring Element
cols_SE = dims[1] #columns of input Structuring Element
dims = np.shape(img) #dimensions of input image
rows_img = dims[0] #rows of input image
cols_img = dims[1] #columns of input image
Eroded_Img =
np.zeros((rows_img, cols_img), dtype=np.uint8) # initialize output image with
zeros and same size as input image
Temp = np.zeros((rows_SE, cols_SE), dtype=np.uint8)
i_end = rows_img-pad_size #end limt of rows
j_end = cols_img-pad_size #end limt of columns
Sum = np.sum(SE)
for i in range (pad_size,i_end):
for j in range (pad_size,j_end):
Temp = img[i-pad_size:i+pad_size+1, j-pad_size:j+pad_size+1] #underneath the structuring element
Result = Temp * SE
R = np.sum(Result)
if Sum==R:
Eroded_Img[i][j] = 255
else:
Eroded_Img[i][j] = 0
return Eroded_Img
#take
Structuring Element from user
SE =
Structuring_Element()
#Perform zero
padding
img =
cv.imread(‘freestudystrom.jpg',0) # Read input image
ret, img =
cv.threshold(img,127,255,cv.THRESH_BINARY) #BINARY IMAGE
dims =
np.shape(SE) #for determining padding size
rows = dims[0]
padding_size =int(np.floor(rows/2)) #padding size is half the mask
size rounded down
img[img == 255] = 1 #image in zeros and ones
padded_img =
Padding(img,padding_size) #perform padding
Eroded_Img =
Erosion(padded_img,padding_size,SE) #applying erosion on image
cv.imshow('Eroded Image', Eroded_Img)
cv.waitKey(0)
Output
Structuring Element 3x3
Structuring Element 9x9
2. Remove the noise from Fig 2 and then fill the holes or gap between thumb impression. You can apply morphological closing and opening.
Code
import numpy as np
import cv2 as cv
#(1) : Take size of Structuring Element
def Structuring_Element():
size = int(input("Enter size of Structuring Element : "))
SE = np.ones((size, size), dtype=np.uint8)
return SE
#(2) : Take image and padding size and add padding to image
def Padding(image,paddingsize):
padded_img = np.pad(image,(paddingsize,paddingsize),'constant') #perform zero padding
return padded_img
# (3) : Take image, padding size and Structuring Element and apply erosion
def Erosion(img,pad_size,SE):
dims = np.shape(SE) #dimensions of input Structuring Element
rows_SE = dims[0] #rows of input Structuring Element
cols_SE = dims[1] #columns of input Structuring Element
dims = np.shape(img) #dimensions of input image
rows_img = dims[0] #rows of input image
cols_img = dims[1] #columns of input image
Eroded_Img = np.zeros((rows_img, cols_img), dtype=np.uint8) # initialize output image with zeros and same size as input image
Temp = np.zeros((rows_SE, cols_SE), dtype=np.uint8)
i_end = rows_img-pad_size #end limt of rows
j_end = cols_img-pad_size #end limt of columns
Sum = np.sum(SE)
for i in range (pad_size,i_end):
for j in range (pad_size,j_end):
Temp = img[i-pad_size:i+pad_size+1, j-pad_size:j+pad_size+1] #underneath the structuring element
Result = Temp * SE
R = np.sum(Result)
if Sum==R:
Eroded_Img[i][j] = 255
else:
Eroded_Img[i][j] = 0
return Eroded_Img
# (4) : Take image, padding size and Structuring Element and apply dilation
def Dilation(img,pad_size,SE):
dims = np.shape(SE) #dimensions of input Structuring Element
rows_SE = dims[0] #rows of input Structuring Element
cols_SE = dims[1] #columns of input Structuring Element
dims = np.shape(img) #dimensions of input image
rows_img = dims[0] #rows of input mask
cols_img = dims[1] #columns of input mask
Dilated_Img = np.zeros((rows_img, cols_img), dtype=np.uint8) # initialize output image with zeros and same size as input image
Temp = np.zeros((rows_SE, cols_SE), dtype=np.uint8)
i_end = rows_img-pad_size #end limt of rows
j_end = cols_img-pad_size #end limt of columns
Sum = np.sum(SE)
for i in range (pad_size,i_end):
for j in range (pad_size,j_end):
Temp = img[i-pad_size:i+pad_size+1, j-pad_size:j+pad_size+1] #underneath the structuring element
Result = Temp * SE
R = np.sum(Result)
if R>0:
Dilated_Img[i][j] = 255
else:
Dilated_Img[i][j] = 0
return Dilated_Img
#take Structuring Element from user
SE = Structuring_Element()
#Perform zero padding
img = cv.imread(‘freestudystrom.tif',0) # Read input image
ret, img = cv.threshold(img,127,255,cv.THRESH_BINARY) #BINARY IMAGE
dims = np.shape(SE) #for determining padding size
rows = dims[0]
padding_size =int(np.floor(rows/2)) #padding size is half the mask size rounded down
img[img == 255] = 1 #image in zeros and ones
padded_img = Padding(img,padding_size) #perform padding
Eroded_Img = Erosion(padded_img,padding_size,SE) #applying erosion on image
Dilated_img = Dilation(Eroded_Img,padding_size,SE) #applying dilation on image
cv.imshow('Eroded Image', Eroded_Img)
cv.imshow('Dilated Image', Dilated_img)
cv.waitKey(0)
Output
Code
import numpy as np
import cv2 as cv
#(1) : Take size of Structuring Element
def Structuring_Element():
size = int(input("Enter size of Structuring Element : ")) #take number of rows
SE = np.ones((size, size), dtype=np.uint8)
return SE
#(2) : Take image and padding size and add padding to image
def Padding(image,paddingsize):
padded_img = np.pad(image,(paddingsize,paddingsize),'constant') #perform zero padding
return padded_img
# (3) : Take image, padding size and Structuring Element and apply erosion
def Erosion(img,pad_size,SE):
dims = np.shape(SE) #dimensions of input Structuring Element
rows_SE = dims[0] #rows of input Structuring Element
cols_SE = dims[1] #columns of input Structuring Element
dims = np.shape(img) #dimensions of input image
rows_img = dims[0] #rows of input image
cols_img = dims[1] #columns of input image
Eroded_Img = np.zeros((rows_img, cols_img), dtype=np.uint8) # initialize output image with zeros and same size as input image
Temp = np.zeros((rows_SE, cols_SE), dtype=np.uint8)
i_end = rows_img-pad_size #end limt of rows
j_end = cols_img-pad_size #end limt of columns
Sum = np.sum(SE)
for i in range (pad_size,i_end):
for j in range (pad_size,j_end):
Temp = img[i-pad_size:i+pad_size+1, j-pad_size:j+pad_size+1] #underneath the structuring element
Result = Temp * SE
Eroded_Img[i, j] = np.min(Result)
return Eroded_Img
# (4) : Take image, padding size and Structuring Element and apply dilation
def Dilation(img,pad_size,SE):
dims = np.shape(SE) #dimensions of input Structuring Element
rows_SE = dims[0] #rows of input Structuring Element
cols_SE = dims[1] #columns of input Structuring Element
dims = np.shape(img) #dimensions of input image
rows_img = dims[0] #rows of input mask
cols_img = dims[1] #columns of input mask
Dilated_Img = np.zeros((rows_img, cols_img), dtype=np.uint8) # initialize output image with zeros and same size as input image
Temp = np.zeros((rows_SE, cols_SE), dtype=np.uint8)
i_end = rows_img-pad_size #end limt of rows
j_end = cols_img-pad_size #end limt of columns
Sum = np.sum(SE)
for i in range (pad_size,i_end):
for j in range (pad_size,j_end):
Temp = img[i-pad_size:i+pad_size+1, j-pad_size:j+pad_size+1] #underneath the structuring element
Result = Temp * SE
Dilated_Img[i, j] = np.max(Result) #for gray scale images
return Dilated_Img
#take Structuring Element from user
SE = Structuring_Element()
#Perform zero padding
img = cv.imread('freestudystrom.tif',0) # Read input image
dims = np.shape(SE) #for determining padding size
rows = dims[0]
padding_size =int(np.floor(rows/2)) #padding size is half the mask size rounded down
padded_img = Padding(img,padding_size) #perform padding
Dilated_img = Dilation(padded_img,padding_size,SE) #applying dilation on image
Eroded_Img = Erosion(padded_img,padding_size,SE) #applying erosion on image
Gradient_img = Dilated_img - Eroded_Img #Dilated image minus Eroded image
cv.imshow('Original Image', img)
cv.imshow('Eroded Image', Eroded_Img )
cv.imshow('Dilated Image', Dilated_img)
cv.imshow('Morphological Gradient Image', Gradient_img)
cv.waitKey(0)
No comments:
Post a Comment