Tuesday 23 January 2024

Image thresholding in OpenCV

Image thresholding is a technique to convert the image into a binary image (Binarization of an image). In a binary image, each pixel has a value of either 0 or 255.

How this thresholding is achieved?

Each pixel value in the image is compared against a threshold value. If the pixel value is greater than or equal to threshold value, then it is set 255, else to 0.

 

Use cases of Image thresholding

a.   Used to detect objects in an image

b.   Used to extract features from an image, such as the edges of objects or the corners of buildings.

 

In general, an image is converted to grayscale image before applying threshold operation. You will get following benefits of converting an image to grayscale before applying thresholding.

 

a.   Better accuracy: Thresholding is more accurate on gray scale images, when the image has more noise.

b.   Simple to process: Grayscale images are simple to process than color images.

c.    Faster computation:  Grayscale images are smaller than color images, so thresholding operations on grayscale images will be faster.

How to perform image thresholding in opencv?

Using cv2.threshold method, we can perform image thresholding.

 

Signature

retval, threshold_image = cv2.threshold(src, threshold, maxval, type[, dst])

retval: it is the threshold value we used in thresholding process

threshold_image: Thresholded image

 

Following table summarizes the parameters of threshold method.

Parameter

Description

src

Source image

threshold

If the image pixel value is greater than or equal to this threshold,then the pixel value is set to maxval, else it is set to 0 or the minimum value, depending on the type parameter.

maxval

This is the maximum value that a pixel is set to when the current pixel value is greater than threshold. In general, it is set to 255, which is used for binary thresholding to create a binary image with pixel values of 0 or 255.

type

Specifies the type of thresholding to be applied on the image. You can use one of the following values.

 

cv2.THRESH_BINARY: Binary thresholding. Pixels greater than or equal to the threshold are set to maxval, and others are set to 0.

cv2.THRESH_BINARY_INV: Inverse binary thresholding. Pixels greater than or equal to the threshold are set to 0, and others are set to maxval.

cv2.THRESH_TRUNC: Thresholding with truncation. Pixels greater than or equal to the threshold are set to the threshold value, and others remain unchanged.

cv2.THRESH_TOZERO: Thresholding to zero. Pixels less than the threshold are set to 0, and others remain unchanged.

cv2.THRESH_TOZERO_INV: Inverse thresholding to zero. Pixels less than the threshold are set to 0, and others remain unchanged.

dst

It is optional array. If dst is specified, then the result of thresholding operation is stored here.

 

Example

threshold_value_used, threshold_image = cv.threshold(grayed_image, 150, 200, cv.THRESH_BINARY)

Find the below working application.

 

thresholding.py

import cv2 as cv

image = cv.imread('wolfs.jpeg')
cv.imshow('Wolfs', image)

grayed_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv.imshow('grayed_image', grayed_image)

threshold_value_used, threshold_image = cv.threshold(grayed_image, 150, 200, cv.THRESH_BINARY)
cv.imshow('binary image', threshold_image)

print(f'threshold_value_used : {threshold_value_used}')

# Wait for Infinite amount of time for a keyboard key to be pressed
cv.waitKey(0)

# Close the OpenCV windows
cv.destroyAllWindows()

Output

threshold_value_used : 150.0

 

 

Original image




Grayed image

 


 

Thresholding image




Inverse thresholded image

We can inverse the thresholding affect using the constant ‘cv.THRESH_BINARY_INV’. Inversed image looks like below.

 


As you observe the above image, black parts of thresholded image are changed to white and white parts of the thresholded image are changed to black.

 

 

inverse_thresholding.py

import cv2 as cv

image = cv.imread('wolfs.jpeg')
cv.imshow('Wolfs', image)

grayed_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv.imshow('grayed_image', grayed_image)

threshold_value_used, inverse_threshold_image = cv.threshold(grayed_image, 150, 200, cv.THRESH_BINARY_INV)
cv.imshow('binary image', inverse_threshold_image)

print(f'threshold_value_used : {threshold_value_used}')

# Wait for Infinite amount of time for a keyboard key to be pressed
cv.waitKey(0)

# Close the OpenCV windows
cv.destroyAllWindows()

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment