Breaking

Be Strong Let's Try!

Wednesday, 25 August 2021

What Is Morphological Operations ? And How To Do Morphological Operations Using Python Coding

 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.

What Is Morphological Operations ? And How To Do Morphological Operations Using Python Coding
On the other hand, dilation expands the size of foreground (1-valued) objects; smooths object boundaries and closes holes and gaps. In dilation, for each foreground pixel (also called input pixel) 
 4. Superimpose the structuring element on top of the input image so that the origin of the structuring element coincides with the input pixel position 
5. 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

 6. If all the corresponding pixels in the image are background, however, the input pixel is left at the background value.

What Is Morphological Operations ? And How To Do Morphological Operations Using Python Coding
More interesting morphological operations can be performed by performing combinations of erosions and dilations. The most widely used of these compound operations are opening and closing. In opening, erosion is followed by dilation. On the other hand, in closing, dilation is followed by erosion. All these operations can be applied on grayscale images as well. Following image shows the application of morphological operation on a grayscale image. Grayscale Erosion is to select the minimum of the pixel values under the mask while Grayscale Dilation is the maximum of the pixel values. A morphological gradient is the difference between the dilation and the erosion of a given image.
1. The top-hat transform is defined as the difference between the original image and its opening. 
2. The bottom-hat transform is defined as the difference between the closing of the original image and the original image.

After theoretical part we will understand this topic through some practical work using python language.
Here is the practical problem let's try:

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.

What Is Morphological Operations ? And How To Do Morphological Operations Using Python Coding

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.

What Is Morphological Operations ? And How To Do Morphological Operations Using Python Coding

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

What Is Morphological Operations ? And How To Do Morphological Operations Using Python Coding
3. We have 512 *512 image of a head CT scan. Perform Gray scale 3x3 dilation and erosion on Fig 3. Also find Morphological gradient. Use following expression to compute gradient.
What Is Morphological Operations ? And How To Do Morphological Operations Using Python Coding

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)

OUTPUT
What Is Morphological Operations ? And How To Do Morphological Operations Using Python Coding









No comments:

Post a Comment

Pages