Simplifying Image Thresholding with OpenCV: Introducing thresholdIntersection Function

Abdullah ERZIN
4 min readJun 28, 2023

--

Image thresholding is a fundamental technique in computer vision and image processing. It allows us to convert grayscale images into binary images by separating objects from the background based on pixel intensity values. However, working with OpenCV’s cv2.threshold function can sometimes be cumbersome, requiring multiple steps and parameters. In this blog post, we introduce a new function called thresholdIntersection that simplifies the thresholding process by providing a more intuitive interface. We'll explore the functionality of this function and demonstrate its usage with code examples

GitHub Repo and Source Codes

If you want to use the thresholdIntersection function or learn more, you can check our GitHub repository. Our repository has the function’s completed source code, sample projects, and more.

You can access the GitHub repository using the following link: GitHub Repo

This repo is an open community project where you have the source codes and the opportunity to contribute to the project. You can make improvements, fix bugs or add different features. You can use it in your own projects or study it for further learning purposes.

You can stay informed of future updates and contribute to the project by following our GitHub repo page.

Understanding the thresholdIntersection Function

The thresholdIntersection function is designed to streamline the thresholding process while providing flexibility in setting the lower and upper limits for thresholding.

One limitation of the cv2.threshold function is that it does not directly provide a way to obtain regions with middle pixel values, such as values in the range of 30 to 50 gray values. In contrast, your thresholdIntersection function addresses this limitation by combining thresholded images and allowing for the extraction of regions within a specified range of pixel values. Let's take a closer look at how it works:

import cv2
import numpy as np

def thresholdIntersection(image, grayImage, lowerLimit, upperLimit):
thresholdLower = cv2.threshold(grayImage, lowerLimit, 255, cv2.THRESH_BINARY_INV)[1]
thresholdUpper = cv2.threshold(grayImage, upperLimit, 255, cv2.THRESH_BINARY)[1]

nestedImage = cv2.add(thresholdLower, thresholdUpper)
thresholdNested = cv2.threshold(nestedImage, 0, 255, cv2.THRESH_BINARY_INV)[1]
nestedContours, _ = cv2.findContours(thresholdNested, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

return image, nestedContours

The thresholdIntersection function takes the following parameters:

  • image: The original image.
  • grayImage: The grayscale version of the original image.
  • lowerLimit: The lower limit for thresholding. If you want to assign a value of 0, use -1.
  • upperLimit: The upper limit for thresholding.

Now, let’s dive into the step-by-step process within the function:

  1. The thresholdLower variable is calculated by applying an inverted binary threshold to the grayImage, replacing all pixel values from the lower limit to -1 with 255.
  2. The thresholdUpper variable is calculated by applying a binary threshold to the grayImage, replacing all pixel values from the upper limit to 255 with 255.
  3. The nestedImage is obtained by combining the thresholdLower and thresholdUpper images using the cv2.add function from NumPy.
  4. An inverted binary threshold is applied to the nestedImage, replacing all values from 0 to 255 with 255, and the result is stored in thresholdNested.
  5. The contours within the thresholdNested image are found using the cv2.findContours function.
  6. Finally, the modified image and the obtained contours are returned by the function.

Simplifying Image Thresholding with thresholdIntersection

The thresholdIntersection function significantly simplifies the image thresholding process by encapsulating multiple steps within a single function call. Let's see how it can be used:

import cv2
# Load the image
image = cv2.imread('image.jpg')
# Convert the image to grayscale
grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply thresholding using the thresholdIntersection function
modifiedImage, contours = thresholdIntersection(image, grayImage, 100, 200)
# Display the modified image with contours
cv2.imshow('Modified Image with Contours', modifiedImage)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the above example, we load an image and convert it to grayscale. Then, we apply thresholding by calling the thresholdIntersection function with the lowerLimit set to 100 and the upperLimit set to 200. The function returns the modified image and the extracted contours, which can be further used for analysis or visualization.

Outputs

Threshold values between 50 and 150 in gray image
Threshold values between 50 and 150 in gray image
Threshold values between -1 and 255 in gray image
Threshold values between -1 and 255 in gray image
Threshold values between 0 and 255 in gray image
Threshold values between 0 and 255 in gray image

Conclusion

In this blog post, we introduced the thresholdIntersection function, which simplifies the image thresholding process using OpenCV. By encapsulating multiple steps within a single function call, this function provides a more intuitive interface for setting the lower and upper limits of thresholding. We discussed the inner workings of the function and demonstrated its usage with code examples. Now you can leverage this function to simplify your image thresholding tasks and focus on higher-level analysis. Happy coding!

Note: The code examples and explanations in this blog post were written in English. If you prefer to read the Turkish version, please refer to the Turkish version of this blog post on the my profile.

--

--