Image thresholding is a technique used in image processing to separate image into two or more parts based on pixel intensity values. When we use grayscale image, the process involves setting a threshold value for the pixel
intensities, and then classifying the pixels as either "foreground" or
"background" based on their intensity values.
This technique is useful for various image processing tasks such as object detection, segmentation, and feature extraction.
OpenCV provides several thresholding methods that can be used to convert a grayscale image to a binary image. In this tutorial, you will briefly learn how to use some of the most commonly used OpenCV thresholding methods in Python. The tutorial covers:
- Binary thresholding
- Otsu thresholding
- Adaptive thresholding
- Source code listing
We'll start by loading the required libraries.
import cv2 import matplotlib.pyplot as plt
In this tutorial, we need an image file to apply the thresholding technique. The following code shows how to load an image and display it in a plot. The file in this tutorial is originally a color image and it will be converted into the grayscale.
file = "/Users/user/Desktop/sign.jpg"
# Load an image
img = cv2.imread(file, cv2.IMREAD_GRAYSCALE)
plt.imshow(img)
plt.show()
# Apply Gaussian blur to remove noise
img = cv2.GaussianBlur(img, (5, 5), 0)
Binary thresholding is a commonly used technique for image thresholding. Threshold value is manually determined to separate
foreground and background regions of an image. OpenCV provides a
built-in function called cv2.threshold()
that can be used to perform binary thresholding. Below code shows how to apply binary and binary inverse thresholding and display the output images.
# Apply binary thresholding
_, thresh_bin = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
_, thresh_bin_inv = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
# visualize in a graph
plt.subplots_adjust(wspace=0.2, hspace=0.2)
plt.subplot(1, 2, 1)
plt.imshow(thresh_bin, cmap='gray')
plt.title("Binary Thresholding")
plt.subplot(1, 2, 2)
plt.imshow(thresh_bin_inv, cmap='gray')
plt.title("Binary Inverse Thresholding")
plt.show()
cv2.threshold()
function to perform Otsu threshodling.
# Apply Otsu thresholding
_, thresh_otsu = cv2.threshold(
img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
_, thresh_otsu_inv = cv2.threshold(
img, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# visualize in a graph
plt.subplots_adjust(wspace=0.2, hspace=0.2)
plt.subplot(1, 2, 1)
plt.imshow(thresh_otsu, cmap='gray')
plt.title("Otsu Thresholding")
plt.subplot(1, 2, 2)
plt.imshow(thresh_otsu_inv, cmap='gray')
plt.title("Otsu Inverse Thresholding")
plt.show()
Here, the cv2.THRESH_BINARY+cv2.THRESH_OTSU
flag indicates that the function determines the optimal threshold
value automatically. The same method applies for inverse version.
cv2.adaptiveThreshold()
function to perform adaptive thresholding.
# Apply adaptive thresholding
thresh_adaptive = cv2.adaptiveThreshold(
img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
thresh_adaptive_inv = cv2.adaptiveThreshold(
img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 11, 2)
# visualize in a graph
plt.subplots_adjust(wspace=0.2, hspace=0.2)
plt.subplot(1, 2, 1)
plt.imshow(thresh_adaptive, cmap='gray')
plt.title("Adaptive Thresholding")
plt.subplot(1, 2, 2)
plt.imshow(thresh_adaptive_inv, cmap='gray')
plt.title("Adaptive Inverse Thresholding")
plt.show()
In this tutorial, we've briefly learned how to perform thresholding with OpenCV in Python. The full source code is listed below.
import cv2
from matplotlib import pyplot as plt
# define an image path
file = "/Users/user/Desktop/sign.jpg"
# Load an image
img = cv2.imread(file, cv2.IMREAD_GRAYSCALE)
# Apply Gaussian blur to remove noise
img = cv2.GaussianBlur(img, (5, 5), 0)
# Apply binary thresholding
_, thresh_bin = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
_, thresh_bin_inv = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
# visualize in a graph
plt.subplots_adjust(wspace=0.2, hspace=0.2)
plt.subplot(1, 2, 1)
plt.imshow(thresh_bin, cmap='gray')
plt.title("Binary Thresholding")
plt.subplot(1, 2, 2)
plt.imshow(thresh_bin_inv, cmap='gray')
plt.title("Binary Inverse Thresholding")
plt.show()
# Apply adaptive thresholding
thresh_adaptive = cv2.adaptiveThreshold(
img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
thresh_adaptive_inv = cv2.adaptiveThreshold(
img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 11, 2)
# visualize in a graph
plt.subplots_adjust(wspace=0.2, hspace=0.2)
plt.subplot(1, 2, 1)
plt.imshow(thresh_adaptive, cmap='gray')
plt.title("Adaptive Thresholding")
plt.subplot(1, 2, 2)
plt.imshow(thresh_adaptive_inv, cmap='gray')
plt.title("Adaptive Inverse Thresholding")
plt.show()
# Apply Otsu thresholding
_, thresh_otsu = cv2.threshold(
img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
_, thresh_otsu_inv = cv2.threshold(
img, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# visualize in a graph
plt.subplots_adjust(wspace=0.2, hspace=0.2)
plt.subplot(1, 2, 1)
plt.imshow(thresh_otsu, cmap='gray')
plt.title("Otsu Thresholding")
plt.subplot(1, 2, 2)
plt.imshow(thresh_otsu_inv, cmap='gray')
plt.title("Otsu Inverse Thresholding")
plt.show()
No comments:
Post a Comment