Mastering OpenCV’s Image Thresholding for Precise Segmentation

Introduction to Image Thresholding

Image thresholding, a crucial aspect of image segmentation, plays a pivotal role in various image preprocessing tasks. Leveraging the power of the OpenCV module, this article dives into the realm of image thresholding techniques and their implementation.

Unveiling OpenCV’s Arsenal of Thresholding Techniques

OpenCV offers several image thresholding techniques, each suited to different scenarios:

1. Simple Thresholding

Simple Thresholding, also known as Binary Thresholding, sets a standard threshold value. Pixel values are compared with this threshold, and if they’re lower, they’re set to 0; otherwise, they’re set to the maximum value.

Loading Images into the Working Environment

To start, you’ll need to load an image into the working environment using the imread() function of the OpenCV package. The loaded image can be visualized with the imshow() function.

img = cv2.imread('/content/drive/MyDrive/Colab notebooks/Image thresholding techniques in opencv/img_thresh.jpeg') plt.imshow(img) plt.show()

To obtain the original image (as imshow() loads grayscale by default), use cv2.COLOR_BGR2RGB():

orig_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) plt.imshow(orig_img) plt.show()

General Syntax of Simple Image Thresholding

The syntax for simple image thresholding is:

cv2.threshold(source, thresholdValue, maxVal, thresholdingTechnique)

  • cv2.THRESH_BINARY: If the pixel intensity is greater than the threshold, the image is segmented to the upper limit of 255; otherwise, it’s set to 0.
Visualizing Thresholding Results

Now, let’s visualize the results:

rect, thresh = cv2.threshold(orig_img, 127, 255, cv2.THRESH_BINARY) plt.imshow(thresh) plt.show()

2. cv2.THRESH_BINARY_INV

This is the inverse of the threshold binary function, where pixels greater than the threshold are segmented to black (0), and those lower are set to white (255).

rect, thresh1 = cv2.threshold(orig_img, 127, 255, cv2.THRESH_BINARY_INV) plt.imshow(thresh1) plt.show()

3. cv2.THRESH_TRUNC

This function truncates pixel intensity values higher than the threshold to the threshold value and segments the rest towards the black region.

rect, thresh2 = cv2.threshold(orig_img, 127, 255, cv2.THRESH_TRUNC) plt.imshow(thresh2) plt.show()

4. cv2.THRESH_TOZERO

For pixels with intensities lower than the threshold, this function sets them to 0, segmenting the image towards the darker side.

rect, thresh3 = cv2.threshold(orig_img, 127, 255, cv2.THRESH_TOZERO) plt.imshow(thresh3) plt.show()

5. cv2.THRESH_TOZERO_INV

This function is the opposite, segmenting pixels with intensities higher than the threshold towards the darker region.

rect, thresh4 = cv2.threshold(orig_img, 127, 255, cv2.THRESH_TOZERO_INV) plt.imshow(thresh4) plt.show()

Diving Deeper into Adaptive Thresholding

Adaptive thresholding, similar to simple thresholding, calculates thresholds for smaller image regions, adapting to different lighting conditions. OpenCV offers two adaptive thresholding techniques:

Mean Adaptive Thresholding

This technique computes means of pixel value blocks and subtracts a constant. It is excellent for handling variations in lighting conditions.

Syntax for Mean Adaptive Thresholding

cv2.adaptiveThreshold(source, maxVal, adaptiveMethod, thresholdType, blockSize, constant)

  • cv2.ADAPTIVE_THRESH_MEAN_C: Computes means of pixel blocks and subtracts a constant.
Applying Mean Adaptive Thresholding

thresh5 = cv2.adaptiveThreshold(img_grey, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2) plt.imshow(thresh5) plt.show()

Gaussian Thresholding

Similar to Mean Adaptive Thresholding, this technique computes Gaussian weights for neighboring pixels and subtracts a constant for segmentation.

Syntax for Gaussian Thresholding

cv2.adaptiveThreshold(source, maxVal, adaptiveMethod, thresholdType, blockSize, constant)

  • cv2.ADAPTIVE_THRESH_GAUSSIAN_C: Computes Gaussian weights for pixel blocks and subtracts a constant.
Applying Gaussian Thresholding

thresh6 = cv2.adaptiveThreshold(img_grey, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) plt.imshow(thresh6) plt.show()

The Magic of Otsu’s Thresholding

Otsu’s thresholding automatically determines the threshold value for image segmentation, making it ideal for noisy images. It first requires converting the image to grayscale.

Normal Otsu’s Thresholding

This technique works on grayscale images, automatically selecting a threshold.

rect1, thresh7 = cv2.threshold(noise_img_grey, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) plt.imshow(thresh7) plt.show()

Gaussian Filter-based Otsu’s Thresholding

This technique requires blurring the grayscale image with a Gaussian filter.

blur_noise_img = cv2.GaussianBlur(noise_img_grey, ksize=(7,7), sigmaX=0) rect, thresh8 = cv2.threshold(blur_noise_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) plt.imshow(thresh8) plt.show()

In Summary

Image thresholding techniques are integral for image segmentation. OpenCV offers a range of methods to choose from, each with its strengths. This article has provided a comprehensive overview of these techniques and their application using OpenCV, giving you the tools to master image segmentation.