- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to perform different simple thresholding of an image using Python OpenCV?
In simple thresholding, we define a threshold value and if a pixel value is greater than a threshold value, it is assigned a value (say 255), else it is assigned another value (say 0).
A simple thresholding can be applied using the function cv2.threshold(). It accepts four arguments− the source image, threshold value, the maxVal and the thresholding type.
OpenCV provides the following different types of thresholding −
cv2.THRESH_BINARY − In this thresholding, pixel value more than the threshold value is assigned to 255 else assigned to 0.
cv2.THRESH_BINARY_INV − It is the opposite case of cv2.THRESH_BINARY.
cv2.THRESH_TRUNC − In this thresholding, pixel value more than the threshold assigned to threshold, other pixels remain the same.
cv2.THRESH_TOZERO − In this thresholding, pixel value less than the threshold assigned to zero, other pixels remain the same.
cv2.THRESH_TOZERO_INV − Opposite of cv2.THRESH_TOZERO.
Syntax
cv2.threshold(img, thresh_val, max_val, thresh_type)
Parameters
img − Input gray scale image. It's a numpy.ndarray.
thresh_val − Threshold value used to classify the pixel values. If the pixel value is higher than the threshold value, it is assigned one value else another value.
max_val − Maximum value to be assigned to a pixel if the pixel value is more than (sometimes less than) the threshold value.
thresh_type − The type of thresholding to be applied.
It returns the global threshold value and the threshold image.
Let's understand different simple thresholdings with the help of some Python examples.
Input Image
We will use this image as the input file in the following examples.
Example 1
In this program, we apply Binary thresholding on the input image.
# Python program to illustrate # a simple thresholding type on an image # import required libraries import cv2 # read the input image as a gray image img = cv2.imread('floor.jpg', 0) # applying binary thresholding technique on the input image # all pixels value above 127 will be set to 255 _, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) # display the image after applying binary thresholding cv2.imshow('Binary Threshold', thresh) cv2.waitKey(0) cv2.destroyAllWindows()
Output
When you run the above program, it will produce the following output −
The above output shows the threshold image after applying Binary thresholding.
Example 2
In this program, we will apply different Simple thresholding on the input image.
# Python program to illustrate # simple thresholding type on an image # import required libraries import cv2 import matplotlib.pyplot as plt # read the input image as a gray image img = cv2.imread('floor.jpg',0)# applying different thresholding techniques on the input image ret1, thresh1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) ret2, thresh2 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV) ret3, thresh3 = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC) ret4, thresh4 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO) ret5, thresh5 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV) titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV'] images = [img,thresh1,thresh2,thresh3,thresh4,thresh5] for i in range(6): plt.subplot(2,3,i+1) plt.imshow(images[i], 'gray') plt.title(titles[i]) plt.axis("off") plt.show()
Output
When you run the above python program, it will produce the following output −
The above output shows the different images after applying different simple thresholding.
- Related Articles
- How to perform Otsu's thresholding on an image using Python OpenCV?
- How to perform adaptive mean and gaussian thresholding of an image using Python OpenCV?
- Performing binary thresholding on an image using OpenCV
- Performing truncate thresholding on an image using OpenCV
- Performing zero thresholding on an image using OpenCV
- How to perform image transpose using OpenCV Python?
- Performing inverse binary thresholding on an image using OpenCV
- Performing inverse zero thresholding on an image using OpenCV
- How to perform image translation using OpenCV in Python?
- How to perform image rotation in OpenCV using Python?
- How to perform bilateral filter operation on an image in OpenCV using Python?
- OpenCV Python – How to perform SQRBox filter operation on an image?
- OpenCV Python – How to perform bitwise NOT operation on an image?
- How to plot histograms of different colors of an image in OpenCV Python?
- How to resize an image in OpenCV using Python?
