Site icon Machine Learning Projects

Top 5 Types of thresholding techniques in Python using OpenCV – 2023

Machine Learning Projects

In today’s blog, we are going to perform one of the most important operations of image processing which is thresholding. So without any further due, let’s do it…

Step 1 – Import the libraries required for thresholding.

import cv2
import matplotlib.pyplot as plt

Step 2 – Read the grayscale image.

img = cv2.imread('gray21.512.tiff')
original image

Step 3 – Let’s instantiate some values.

th = 127
max_val = 255

Step 4 – Performing the thresholding operation using the following 5 methods.

ret, o1 = cv2.threshold(img, th, max_val, cv2.THRESH_BINARY)
ret, o2 = cv2.threshold(img, th, max_val, cv2.THRESH_BINARY_INV)
ret, o3 = cv2.threshold(img, th, max_val, cv2.THRESH_TOZERO)
ret, o4 = cv2.threshold(img, th, max_val, cv2.THRESH_TOZERO_INV)
ret, o5 = cv2.threshold(img, th, max_val, cv2.THRESH_TRUNC)

NOTE – Refer to the resulting image below for further understanding.

Step 5 – Plot the results.

output = [img, o1, o2, o3, o4, o5]

titles = ['Original', 'Binary', 'Binary Inv', 'Zero', 'Zero Inv', 'Trunc']

for i in range(6):
    plt.subplot(2, 3, i + 1)
    plt.imshow(output[i])
    plt.title(titles[i])
    plt.xticks([])
    plt.yticks([])

plt.show()
Results

NOTE – Thresholding can be further optimized by using OTSU Thresholding.

ret, o1 = cv2.threshold(img, 0, max_val, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
ret, o2 = cv2.threshold(img, 0, max_val, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
ret, o3 = cv2.threshold(img, 0, max_val, cv2.THRESH_TOZERO+cv2.THRESH_OTSU)
ret, o4 = cv2.threshold(img, 0, max_val, cv2.THRESH_TOZERO_INV+cv2.THRESH_OTSU)
ret, o5 = cv2.threshold(img, 0, max_val, cv2.THRESH_TRUNC+cv2.THRESH_OTSU)

Let’s see the whole code…

import cv2
import matplotlib.pyplot as plt

img = cv2.imread('gray21.512.tiff')

th = 127
max_val = 255

# give max value as soon as value crosses threshold
ret, o1 = cv2.threshold(img, th, max_val, cv2.THRESH_BINARY)
# give max value till it does not crosses threshold
ret, o2 = cv2.threshold(img, th, max_val, cv2.THRESH_BINARY_INV)
# give 0 value till it does not crosses threshold
ret, o3 = cv2.threshold(img, th, max_val, cv2.THRESH_TOZERO)
# give 0 as it crosses threshold
ret, o4 = cv2.threshold(img, th, max_val, cv2.THRESH_TOZERO_INV)
# give threshold value as it crosses threshold
ret, o5 = cv2.threshold(img, th, max_val, cv2.THRESH_TRUNC)

output = [img, o1, o2, o3, o4, o5]

titles = ['Original', 'Binary', 'Binary Inv',
          'Zero', 'Zero Inv', 'Trunc']

for i in range(6):
    plt.subplot(2, 3, i + 1)
    plt.imshow(output[i])
    plt.title(titles[i])
    plt.xticks([])
    plt.yticks([])

plt.show()

NOTE – Read more about thresholding operation here.

Do let me know if there’s any query regarding this topic by contacting me by email or LinkedIn.

So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time ?…

Read my previous post: HOW TO SPLIT AND MERGE CHANNELS IN PYTHON USING OPENCV

Check out my other machine learning projectsdeep learning projectscomputer vision projectsNLP projectsFlask projects at machinelearningprojects.net.

Exit mobile version