Updated April 18, 2023
Introduction to OpenCV Threshold
OpenCV is a technique which helps in processing images. The threshold in OpenCV helps in assigning pixel values. These pixel values are allocated to threshold values. These values are then compared with the threshold values and the image is segmented by setting this value to maximum if pixel value is greater than threshold and if less then it is set to 0. To segment the image this technique is most commonly used and helps in getting better results. Python also provides a function for setting this threshold value which we will use soon as we go ahead.
Syntax:
Syntax for using this function is as below:
cv.threshold(src, thresholdValue, maxValue, threshold type)
Parameters:
- src: This will be the source image which should be grayscale. Grayscale images are black and white images.
- thresholdValue: This will be the value of threshold which will be above the pixel value and below the pixel value. The threshold values will keep changing according to pixels.
- maxValue: This will be the maximum value which can be assigned to a pixel.
- threshold type: The threshold type is the technique or type which will be applied on the image.
We can also add the destination just like we have added the source. The image will be stored at this location once it is processed.
How Threshold Function Works in OpenCV?
In order to create binary images, the images must be segmented. This segmentation is done by using OpenCV threshold. This thresholding is simple thresholding and adaptive thresholding. The pixel value which is used there has to be a corresponding threshold value which should be the same.
The logic will remain same that if pixel value is smaller than threshold then it will be set to 0 and if it is greater than it will be set to maximum value. It can use thresholding techniques like THRESH_BINARY, THRESH_BINARY_INV, THRESH_TRUNC, THRESH_TOZERO, THRESH_TOZERO_INV. These techniques help in creating greyscale images.
These techniques works as below:
- cv.THRESH_BINARY: The intensity of pixels which is greater than threshold will be set to 255 else it will be black which is 0.
- cv.THRESH_BINARY_INV: In this case the intensity of pixels will be the inverse of THRESH_BINARY. That is 0 when pixel value is less than threshold else it will be white.
- cv.THRESH_TRUNC: When pixel intensity becomes greater than threshold value it will be truncated to threshold. After this the pixel values should be set to the value which will be same as threshold value and other values will be the same.
- cv.THRESH_TOZERO: All pixels having values less than threshold, the pixel intensity for these is set to zero.
- cv.THRESH_TOZERO_INV: This will work in the opposite way of above function.
Example of OpenCV Threshold
Let us have a look at an example and see the working of OpenCV and how it works with different thresholds.
The original image which we will process is as below:
Let us process this image and turn it into greyscale using OpenCV techniques.
Code:
# Python program example to understand threshold and its techniques
# importing necessary libraries
import cv2
import numpy as np
# specify the path where the image is and read it using imread
img1 = cv2.imread('eduCBA.JPG')
# cv2.cvtColor is used with the image to change it to grayscale image
imagenew = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
# use different thresholding techniques.
#pixels which will have value greater than specified pixel values will be set to 255
ret, thrsh1 = cv2.threshold(imagenew, 150, 255, cv2.THRESH_BINARY)
ret, thrsh2 = cv2.threshold(imagenew, 200, 255, cv2.THRESH_BINARY_INV)
ret, thrsh3 = cv2.threshold(imagenew, 125, 255, cv2.THRESH_TRUNC)
ret, thrsh4 = cv2.threshold(imagenew, 180, 255, cv2.THRESH_TOZERO)
ret, thrsh5 = cv2.threshold(imagenew, 160, 0, cv2.THRESH_TOZERO_INV)
# display the different images
cv2.imshow('Image after applying Binary Threshold', thrsh1)
cv2.imshow('Image after applying Binary Threshold Inverted', thrsh2)
cv2.imshow('Image after applying Truncated Threshold', thrsh3)
cv2.imshow('Image after applying Set to 0', thrsh4)
cv2.imshow('Image after applying Set to 0 Inverted', thrsh5)
# De-allocate any associated memory usage
if cv2.waitKey(0) & 0xff == 27:
cv2.destroyAllWindows()
titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
images = [imagenew, thrsh1, thrsh2, thrsh3, thrsh4, thrsh5]
for i in range(6):
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
The above code first imports the necessary libraries which are needed to process the image.
If you get an error for OpenCV library you can install it by using below command:
Code:
pip install opencv-python
Once this is installed you will get below message:
Output:
The code then goes to the path where the image is stored. It then reads this image by using the imread function. The cv2.cvtcolor is a function present in cv2 library. It will convert this image to gray. Now we will use the above specified thresholding techniques and then observe the changes which happen to the image once the pixels are rounded off to a particular threshold. We have applied all 5 threshold techniques. We have taken different pixel values of the image before applying the techniques to ‘imagenew’.
The last technique sets the threshold to 0. We call these functions by just using the cv2 function followed by the function name which gives us the needed result. Once we apply these, we can print the output by using the imshow function which is also present in the cv2 library. These outputs will be displayed in different windows as well as in the form of grid as we have used the plot library. We clear the memory after we have performed the necessary actions.
Output of original image will be now as you can observe below:
The above image as you can observe has a greyscale and has been changed as per different pixel and threshold values.
Conclusion
OpenCV is a deep learning algorithm which helps in processing images before they can be used for classifications using different models and algorithms. The threshold function helps in creating a grayscale image so that this image can have only two outcomes which will be black or white. This helps in processing better and applying the models in an efficient way.
Recommended Articles
We hope that this EDUCBA information on “OpenCV Threshold” was beneficial to you. You can view EDUCBA’s recommended articles for more information.