Thresholding with OpenCV

Thresholding is a technique that converts a grayscale or color image into a binary image, where each pixel is either black or white. Thresholding can be useful for image segmentation, edge detection, and other applications. In this blog post, I will show you how to perform different types of thresholding with OpenCV, a popular library for computer vision in Python.

Simple Thresholding

Simple thresholding is the simplest way of thresholding, where we use a global value as a threshold. If the pixel value is below the threshold, it is set to black; otherwise, it is set to white. We can use the cv2.threshold function to apply simple thresholding. The function takes four arguments: the source image, the threshold value, the maximum value, and the thresholding type. The function returns two values: the threshold value used and the thresholded image.

Here is an example of simple thresholding with OpenCV:import cv2 import numpy as np from matplotlib import pyplot as plt # Read the image and convert it to grayscale img = cv2.imread('ex2.jpg') img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Apply simple thresholding with a threshold value of 127 and a maximum value of 255 ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) # Show the original and thresholded images plt.subplot(121), plt.imshow(img, cmap='gray') plt.title('Original Image'), plt.xticks([]), plt.yticks([]) plt.subplot(122), plt.imshow(thresh, cmap='gray') plt.title('Thresholded Image'), plt.xticks([]), plt.yticks([]) plt.show()

The output of the code is:

Simple Thresholding

We can see that the thresholded image has only two colors: black and white. The pixels that are below 127 are set to black, and the pixels that are above 127 are set to white.

Adaptive Thresholding

Simple thresholding may not work well if the image has different lighting conditions in different regions. In that case, adaptive thresholding can help. Adaptive thresholding determines the threshold for each pixel based on a small region around it. This way, we can get different thresholds for different regions of the same image, which can improve the results for images with varying illumination.

To apply adaptive thresholding, we can use the cv2.adaptiveThreshold function. The function takes six arguments: the source image, the maximum value, the adaptive method, the thresholding type, the block size, and a constant value. The function returns the thresholded image.

The adaptive method can be one of the following:

  • cv2.ADAPTIVE_THRESH_MEAN_C: The threshold value is the mean of the neighborhood area minus the constant C.
  • cv2.ADAPTIVE_THRESH_GAUSSIAN_C: The threshold value is the weighted sum of the neighborhood area minus the constant C. The weights are a Gaussian window.

The block size determines the size of the neighborhood area. It must be an odd number.

Here is an example of adaptive thresholding with OpenCV:import cv2 import numpy as np from matplotlib import pyplot as plt # Read the image and convert it to grayscale img = cv2.imread('ex2.jpg') img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Apply simple thresholding with a threshold value of 127 and a maximum value of 255 ret, thresh1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) # Apply adaptive thresholding with a block size of 11 and a constant of 2 # Use the mean method and the binary thresholding type thresh2 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2) # Apply adaptive thresholding with a block size of 11 and a constant of 2 # Use the Gaussian method and the binary thresholding type thresh3 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) # Show the original and thresholded images titles = ['Original Image', 'Simple Thresholding', 'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding'] images = [img, thresh1, thresh2, thresh3] for i in range(4): plt.subplot(2, 2, i+1), plt.imshow(images[i], 'gray') plt.title(titles[i]), plt.xticks([]), plt.yticks([]) plt.show()

The output of the code is:

Adaptive Thresholding

We can see that the adaptive thresholding images are better than the simple thresholding image, especially for the regions that have different lighting conditions. The adaptive mean thresholding image has some noise, while the adaptive Gaussian thresholding image is smoother.

Otsu’s Thresholding

Otsu’s thresholding is another way of thresholding, where we do not need to specify the threshold value manually. Instead, the algorithm finds the optimal threshold value that minimizes the within-class variance of the pixel values. Otsu’s thresholding can be useful for images that have a bimodal histogram, where the pixel values are clustered into two distinct groups.

To apply Otsu’s thresholding, we can use the same cv2.threshold function as before, but with an extra flag: cv2.THRESH_OTSU. The function will then ignore the threshold value argument and use Otsu’s algorithm to find the optimal threshold. The function will return the optimal threshold value and the thresholded image.

Here is an example of Otsu’s thresholding with OpenCV:import cv2 import numpy as np from matplotlib import pyplot as plt # Read the image and convert it to grayscale img = cv2.imread('ex2.jpg') img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Apply simple thresholding with a threshold value of 127 and a maximum value of 255 ret1, thresh1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) # Apply Otsu's thresholding with a maximum value of 255 # The threshold value will be determined by the algorithm ret2, thresh2 = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) # Show the original and thresholded images titles = ['Original Image', 'Simple Thresholding', 'Otsu\'s Thresholding'] images = [img, thresh1, thresh2] for i in range(3): plt.subplot(1, 3, i+1), plt.imshow(images[i], 'gray') plt.title(titles[i]), plt.xticks([]), plt.yticks([]) plt.show() # Print the threshold values print('Simple Thresholding:', ret1) print('Otsu\'s Thresholding:', ret2)

The output of the code is:

Otsu's Thresholding

Simple Thresholding: 127.0
Otsu’s Thresholding: 121.0

We can see that the Otsu’s thresholding image is similar to the simple thresholding image, but with a slightly different threshold value. The Otsu’s algorithm found that 121 is the optimal threshold value for this image, which minimizes the within-class variance.

Conclusion

In this blog post, I have shown you how to perform different types of thresholding with OpenCV, such as simple thresholding, adaptive thresholding, and Otsu’s thresholding. Thresholding is a useful technique for image processing and computer vision, as it can help to segment, detect, and enhance images. I hope you have learned something new and useful from this post. If you have any questions or feedback, please feel free to leave a comment below. Thank you for reading!

Source: Conversation with Bing, 2/16/2024
(1) OpenCV: Image Thresholding. https://docs.opencv.org/3.4/d7/d4d/tutorial_py_thresholding.html.
(2) Image Thresholding in Python OpenCV – GeeksforGeeks. https://www.geeksforgeeks.org/image-thresholding-in-python-opencv/.
(3) OpenCV cv2.threshold() Function – Scaler Topics. https://www.scaler.com/topics/cv2-threshold/.
(4) Simple Thresholding with OpenCV and Python – Jeremy Morgan. https://www.jeremymorgan.com/tutorials/opencv/simple-thresholding/.
(5) github.com. https://github.com/alex-ta/ImageProcessing/tree/dfeb8b4d80bfc1cfda8ea0d39a38d090f7410d3b/test%20-%20Kopie.py.
(6) github.com. https://github.com/zkzk5214/Py_road/tree/a67edc1604d468a37ab967d35ab1dc1f7a4f7742/openCV%2F15_Threshold%2F15_2.py.
(7) github.com. https://github.com/samuelxu999/Research/tree/187965ff3c86a02c7f5cc853d4cdb77ec805a786/OpenCV%2Fpy_dev%2Fsrc%2Ftest_demo%2FImageProcessing%2FImageThresholding.py.
(8) github.com. https://github.com/jgj03/opencv_library_basics/tree/b10f5871c1d4c5169385275abab59712b9bca364/001_opencv_basics.py.

Author: John Rowan

I am a Senior Android Engineer and I love everything to do with computers. My specialty is Android programming but I actually love to code in any language specifically learning new things.

Author: John Rowan

I am a Senior Android Engineer and I love everything to do with computers. My specialty is Android programming but I actually love to code in any language specifically learning new things.

%d bloggers like this: