Basic Image Thresholding in OpenCV

Anupriyam Ranjit
5 min readApr 26, 2019

--

Understanding classical methods for thresholding

Over the last couple of weeks, I have been researching into image processing as it would provide the basics of computer vision. This fascination with computer vision stems from the new utilization it could have in the world shortly with applications such as self-driving cars.

Thresholding

Simple Thresholding

Thresholding is when pixels above and below a specific threshold value are assigned new values. Below I have attached some code from the OpenCV tutorial on thresholding:

Here is some example code found on the OpenCV tutorial.

This image is the output for the code

By looking at the code above, we can analyze what is happening in the code. Let’s start with the function that reads the image; the first parameter opens the specific image and the second parameter tells OpenCV how to read the image.

img = cv2.imread(‘gradient.png’,0)

There are three options for the second parameter; -1, 0, and 1, which correspond to Unchanged, Grayscale, and Color respectively.

We will look at the next function in the code.

ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)

ret is retVal which is not used for global thresholding but instead used in Otsu’s Binarization which I will explain later in this article. The next item is thresh1 which is merely the name of the image after we have applied thresholding to it. On the right side, we use a threshold function which takes four parameters:

  1. The image itself given by the variable “img” which we have the previously defined.
  2. The threshold value of 127 so any pixel value above that will have a function applied to it.
  3. The max value of 255 as grayscale only goes from 0–255; 0 being black and 255 being white.
Grayscale

4. The final parameter is the type of thresholding applied to the image.

Now let’s analyze how the fourth parameter of cv2.threshold changes each image:

Here is the picture again for easier reference
  1. THRESH_BINARY: This merely turns any value above and below your threshold into the minimum possible and maximum potential value respectively.
  2. THRESH_BINARY_INV: This does the opposite of a THRESH_BINARY where any value above and below your threshold into the maximum possible and minimum potential value respectively.
  3. THRESH_TRUNC: Values lower than the threshold value are unchanged, but any value higher is set to the threshold value of 127.
  4. THRESH_TOZERO: Pixel values lower then the threshold value are set to the 0 and value above are unchanged.
  5. THRESH_TOZERO_INV: Pixel values less than the threshold value are fixed, and the value above is set to 0.

Adaptive Thresholding

Simple thresholding is very useful for basic tasks. However, it does have its problems, mainly that it sets a threshold value globally, meaning that it is constant for the entire image. This becomes a problem if there is some lighting on the image making some areas lighters then other. For example this image below.

Original Image
Original Image with a Global Thresholding of 80
Original image with an adaptive threshold applied to it

As seen above, the adaptive thresholding did a much better job than normal global thresholding.

Adaptive Mean Thresholding

This method calculates the threshold value at pixel(x,y) by calculating the mean pixel value for the blockSize which is then multiplied by the mean pixel value of the neighboring blockSize pixel(x,y) minus some constant C.

Gaussian Thresholding

This method that calculates the threshold value at pixel(x,y) is the Gaussian weighted sum of the neighborhood values minus some constant C.

Implementing Adaptive Thresholding

Otsu’s Binarization

In global thresholding, we set a thresholding value to a specific amount and hope it works. However, what if we automatically wanted to find the best thresholding value that would work for the image. In some instances, Otsu’s binarization can help.

This method is based on the assumption that the background and foreground create a bimodal distribution(two peaks) and the optimal threshold value is between those 2 peaks as seen in the figure above. Oh and before I forget the retVal shown as ret comes into play as it replaces the global thresholding value with the value determined by Ostu’s Binarization. This only work is Ostu’s binarization is used, if it is not used then it ret has the same value as what is in the code.

Conclusion

As pictured above, thresholding allows for the isolation of certain objects in an image which can be useful in many applications. Thresholding is only one of the many classical methods of image processing; there is also clustering, edge detection, and region growing just to name a few.

If you liked this article feel free to 👏👏 down below and

  • Follow me on Medium
  • Email me at anupriyamranjit8@gmail.com if you want to chat about anything related to machine learning, data science or just to say hi!

--

--