Created
February 27, 2021 01:07
-
-
Save edward1986/bf3b28d36acfdd39f1d18eb06fe71c51 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Introduction to thresholding types | |
""" | |
# Import required packages: | |
import numpy as np | |
import cv2 | |
from matplotlib import pyplot as plt | |
def build_sample_image(): | |
"""Builds a sample image with 50x50 regions of different tones of gray""" | |
# Define the different tones. | |
# The end of interval is not included | |
tones = np.arange(start=50, stop=300, step=50) | |
# print(tones) | |
# Initialize result with the first 50x50 region with 0-intensity level | |
result = np.zeros((50, 50, 3), dtype="uint8") | |
# Build the image concatenating horizontally the regions: | |
for tone in tones: | |
img = np.ones((50, 50, 3), dtype="uint8") * tone | |
result = np.concatenate((result, img), axis=1) | |
return result | |
def show_img_with_matplotlib(color_img, title, pos): | |
"""Shows an image using matplotlib capabilities""" | |
# Convert BGR image to RGB | |
img_RGB = color_img[:, :, ::-1] | |
ax = plt.subplot(8, 1, pos) | |
plt.imshow(img_RGB) | |
plt.title(title) | |
plt.axis('off') | |
# Create the dimensions of the figure and set title and color: | |
fig = plt.figure(figsize=(5, 9)) | |
plt.suptitle("Simple thresholding types", fontsize=14, fontweight='bold') | |
fig.patch.set_facecolor('silver') | |
# Load the image and convert it to grayscale: | |
image = build_sample_image() | |
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | |
# Plot the grayscale images and the histograms: | |
show_img_with_matplotlib(cv2.cvtColor(gray_image, cv2.COLOR_GRAY2BGR), | |
"img tones of gray - left to right: (0,50,100,150,200,250)", 1) | |
# Apply cv2.threshold() with different thresholding values: | |
ret1, thresh1 = cv2.threshold(gray_image, 100, 255, cv2.THRESH_BINARY) | |
ret2, thresh2 = cv2.threshold(gray_image, 100, 220, cv2.THRESH_BINARY) | |
ret3, thresh3 = cv2.threshold(gray_image, 100, 255, cv2.THRESH_BINARY_INV) | |
ret4, thresh4 = cv2.threshold(gray_image, 100, 220, cv2.THRESH_BINARY_INV) | |
ret5, thresh5 = cv2.threshold(gray_image, 100, 255, cv2.THRESH_TRUNC) | |
ret6, thresh6 = cv2.threshold(gray_image, 100, 255, cv2.THRESH_TOZERO) | |
ret7, thresh7 = cv2.threshold(gray_image, 100, 255, cv2.THRESH_TOZERO_INV) | |
# Plot the images: | |
show_img_with_matplotlib(cv2.cvtColor(thresh1, cv2.COLOR_GRAY2BGR), "THRESH_BINARY - thresh = 100 & maxValue = 255", 2) | |
show_img_with_matplotlib(cv2.cvtColor(thresh2, cv2.COLOR_GRAY2BGR), "THRESH_BINARY - thresh = 100 & maxValue = 220", 3) | |
show_img_with_matplotlib(cv2.cvtColor(thresh3, cv2.COLOR_GRAY2BGR), "THRESH_BINARY_INV - thresh = 100", 4) | |
show_img_with_matplotlib(cv2.cvtColor(thresh4, cv2.COLOR_GRAY2BGR), "THRESH_BINARY_INV - thresh = 100 & maxValue = 220", 5) | |
show_img_with_matplotlib(cv2.cvtColor(thresh5, cv2.COLOR_GRAY2BGR), "THRESH_TRUNC - thresh = 100", 6) | |
show_img_with_matplotlib(cv2.cvtColor(thresh6, cv2.COLOR_GRAY2BGR), "THRESH_TOZERO - thresh = 100", 7) | |
show_img_with_matplotlib(cv2.cvtColor(thresh7, cv2.COLOR_GRAY2BGR), "THRESH_TOZERO_INV - thresh = 100", 8) | |
# Show the Figure: | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment