Image Thresholding Python OpenCV
In the previous articles, we have mentioned several image processing techniques using Python OpenCV. In this post, you will learn different image thresholding techniques using the Python OpenCV.
Image thresholding is of many types, like Simple Thresholding, Adaptive Thresholding, and Otsu's Thresholding. In this article, we will explain about Simple Thresholding.
Simple Thresholding Python OpenCV
Image thresholding is a simple form of image segmentation. It is a way to create a binary image from a grayscale or full-color image. It is processed by setting a threshold value on the pixel intensity of the original image. It is generally applied to grayscale images, but it can also be applied to color images.
In Python, OpenCV provides the cv.threshold() method for image thresholding. It has the following syntax-
cv2.threshold(image, threshold_value, max_val, thresholding_technique)
image- It is the source image, which should be a grayscale image.
threshold_value- It specifies the threshold value which is used to classify the pixel values.
max_val- It specifies the maximum value which is assigned to pixel values exceeding the threshold.
thresholding_technique- It is a type of thresholding technique.
These are the simple thresholding types-
- cv.THRESH_BINARY- If pixel intensity is greater than the set threshold, the value is set to 255, else set to 0.
- cv.THRESH_BINARY_INV- Inverted or opposite case of cv2.THRESH_BINARY.
- cv.THRESH_TRUNC- If pixel intensity value is greater than threshold, it is truncated to the threshold. The pixel values are set to be the same as the threshold.
- cv.THRESH_TOZERO- Pixel intensity is set to 0, for all the pixels with less than the threshold value.
- cv.THRESH_TOZERO_INV- Inverted or Opposite case of cv2.THRESH_TOZERO.
In thresholding, each pixel value is compared with the threshold value. The same threshold value is applied to every pixel. If the pixel value is smaller than the threshold, it is set to 0, otherwise it is set to a maximum value.
Image Thresholding of Grayscale images Python OpenCV
The following code represents the image thresholding of a grayscale image. For this, we have used the thresholding type 'cv2.THRESH_BINARY'.
# import modules
import cv2
import numpy as np
from matplotlib import pyplot as plt
# calling imread method to load image
img = cv2.imread('nature.jpg',0)
# applying thresholding techniques on the input image
ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
# displaying original and thresholding images
plt.subplot(221), plt.imshow(img, 'gray')
plt.subplot(222), plt.imshow(thresh1,'gray')
plt.show()
Here is another example of a grayscale image threshold in which we have applied different thresholding types.
# import modules
import cv2
import numpy as np
from matplotlib import pyplot as plt
# calling imread method to load image
img = cv2.imread('nature.jpg',0)
# applying different thresholding techniques on the input image
ret,thresh2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
ret,thresh3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC)
ret,thresh4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
ret,thresh5 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV)
# displaying original and thresholding images
plt.subplot(221), plt.imshow(thresh2,'gray')
plt.subplot(222), plt.imshow(thresh3, 'gray')
plt.subplot(223), plt.imshow(thresh4, 'gray')
plt.subplot(224), plt.imshow(thresh5, 'gray')
plt.show()
Image Thresholding of Color images Python OpenCV
We can also perform image thresholding of color images. Here is an example of the thresholding of color images with different thresholding types.
import cv2
import numpy as np
from matplotlib import pyplot as plt
# calling imread method to load image
img = cv2.imread('nature.jpg')
# applying different thresholding techniques on the input image
ret,thresh2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
ret,thresh3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC)
ret,thresh4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
# displaying original and thresholding images
plt.subplot(221), plt.imshow(img,'gray')
plt.subplot(222), plt.imshow(thresh2,'gray')
plt.subplot(223), plt.imshow(thresh3, 'gray')
plt.subplot(224), plt.imshow(thresh4, 'gray')
plt.show()
Related Articles
Python program to multiply two numbersMultiply all elements in list Python
Python program to count the occurrences of a word in a text file
Python program to map two lists into a dictionary
Python program to input week number and print week day
OpenCV bitwise and
How to capture a video in Python OpenCV and save
Python OpenCV Overlaying or Blending Two Images
Contour Detection using Python OpenCV
Harris Corner Detection using Python OpenCV
OpenCV hog
Face Recognition OpenCV Source Code
Canny Edge Detector OpenCV Python
Python NumPy: Overview and Examples
Image processing using Python Pillow
Python OpenCV Histogram Equalization
Python OpenCV Histogram of Color Image
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV ColorMap
Python OpenCV Gaussian Blur Filtering
Python OpenCV Overview and Examples