Breaking

Be Strong Let's Try!

Tuesday, 24 August 2021

What Is Image Thresholding ? And How To Do Image Thresholding Using Python Coding

What Is The Objective Of This Topic:

The objective of this topic is to apply different thresholding techniques on image. 

What Is The Description Of This Topic: 

 Thresholding is the simplest method of image segmentation. From a grayscale image, thresholding can be used to create binary images. For example in the image shown below threshold is applied around the gray value 160 and we have easily extracted the hand out of the image.

What  Is Image Thresholding And How To Do Image Thresholding Using Python Coding


Adaptive thresholding:

 In some cases a simple threshold doesn’t work. An alternative approach is local threshold which is to statistically examine the intensity values of the local neighborhood of each pixel and threshold on the bases of local mean, median or mode etc.

 Local adaptive thresholding: 

Local adaptive thresholding is used to convert an image consisting of gray scale pixels to just black and white scale pixels. Unlike the global thresholding technique, local adaptive thresholding chooses different threshold values for every pixel in the image based on an analysis of its neighboring pixels. This is to allow images with varying contrast levels where a global thresholding technique will not work satisfactorily. There are a number of different forms of adaptive thresholding algorithm reported in the image processing literature. 


Global adaptive algorithm: 

1. Estimate initial mean value of image T1. Make two groups (G1,G2) of pixels based on mean value T1. 

2. Compute average gray values m1 and m2 of each group. 

3. Compute new threshold value T=(m1+m2)/2

4. Repeat steps 2 to 4 to get abs(Ti-Ti-1)<epsilon

5. When the above condition will satisfy we will have final threshold value depend upon on that we will create binary 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.Write a program that threshold the provided image using global mean and median.
What  Is Image Thresholding And How To Do Image Thresholding Using Python Coding

Code

import numpy as np

import numpy as np
import cv2 as cv
img = cv.imread(
'pp.png',0) # Read input image
dims = np.shape(img)         #image dimensions
rows = dims[0]
cols = dims[
1]
Mean_thres = np.zeros((rows, cols),
dtype=np.uint8) #for mean thresholding
Median_thres = np.zeros((rows, cols), dtype=np.uint8) #for median thresholding
mean =np.mean(img) #calculate mean
median = np.median(img) #calculate median
#for mean image
for i in range(0, rows):
   
for j in range(0, cols):
       
if img[i,j]<=mean:
            Mean_thres[i,j] =
0
       
else:
            Mean_thres[i,j] =
255
#for median image           
for i in range(0, rows):
   
for j in range(0, cols):
       
if img[i,j]<=median:
            Median_thres[i,j] =
0
       
else:
            Median_thres[i,j] =
255
cv.imshow('Mean thresholded Image',Mean_thres)
cv.imshow(
'Median thresholded Image',Median_thres)
cv.waitKey(
0)

 

Output

What  Is Image Thresholding And How To Do Image Thresholding Using Python Coding
2. Now threshold the image by taking threshold value mean of 3x3 block locally.

Code

import numpy as np
import cv2 as cv
img = cv.imread(
'pp.png',0) # Read input image
dims = np.shape(img)         #image dimensions
rows = dims[0]
cols = dims[
1]
Local_Threshold = np.zeros((rows,cols),
dtype=np.uint8)    #final output image
for i in range(0,rows,3):
   
for j in range(0,cols,3):
        Local_img = img[i:i+3,j:j+3]     
# window of 3x3
       
Mean = np.mean(Local_img)         #mean of local image
       
Mean=Mean-3                       #for better visualization
       
if img[i,j]<=Mean:
            Local_Threshold[i,j] =
0
       
else:
            Local_Threshold[i,j] =
255
cv.imshow('Local Thresholded Image',Local_Threshold)
cv.waitKey(
0)

 

Output

What  Is Image Thresholding And How To Do Image Thresholding Using Python Coding
3. Implement global adaptive thresholding algorithm to threshold given image.

Code

import numpy as np
import cv2 as cv
img = cv.imread(
pp.png',0) # Read input image
dims = np.shape(img)         #image dimensions
rows = dims[0]
cols = dims[
1]
Global_Adaptive = np.zeros((rows,cols),
dtype=np.uint8)    #final output image
epsilon = 7
# (a) Select initial threshold value
T0 = np.mean(img)           # choosing mean as initial threshold
while (1):
# (b) Make two groups on the basis of threshold value
   
G1 = []  # For lesser value pixels
   
G2 = []  # For higher value pixels
   
for i in range(0, rows):
       
for j in range(0, cols):
           
if img[i, j] <= T0:
                G1.append(img[i, j])
           
elif img[i, j] > T0:
                G2.append(img[i, j])


# (c) Calculate mean of both groups
   
m1 = np.mean(G1)    #mean of Group 1 pixels
   
m2 = np.mean(G2)    #mean of Group 2 pixels


# (d) New threshold is the average of the two means
   
T1 = np.round((m1 + m2) / 2)  # new threshold value
   
if abs(T1-T0)<epsilon:        #breaking condition
       
break
   
T0=T1       #use new value for next iteration
# (e) Apply thresholding on input image using final thresholf Th
Th = T1                     #Th is the final threshold value
for i in range(0, rows):
   
for j in range(0, cols):
       
if img[i,j]<=Th:
            Global_Adaptive[i,j] =
0
       
else:
            Global_Adaptive[i,j] =
255
cv.imshow('Global Adaptive Thresholded Image',Global_Adaptive)
cv.waitKey(
0)

Output

What  Is Image Thresholding And How To Do Image Thresholding Using Python Coding
Home Task!!

How you can generate Local Adaptive Thresholded Image??

Something Like This !!

What  Is Image Thresholding And How To Do Image Thresholding Using Python Coding









1 comment:

  1. Lucky Club Casino site review – Slots, tables, bonuses,
    Lucky Club Casino luckyclub - All about slots, tables and bonuses, games, promotions, security, games, banking and promotions. All are

    ReplyDelete

Pages