[PYTHON] Threshold processing (adaptive thresholded)

Execution environment

Google Colaboratory

Preparing to load images with Google Colaboratory

python


from google.colab import files
from google.colab import drive
drive.mount('/content/drive')

Loading the required libraries

python


import cv2 #opencv
import matplotlib.pyplot as plt 
%matplotlib inline

Image preparation

python


img = plt.imread("/content/drive/My Drive/Colab Notebooks/img/Lenna.bmp")
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

Adaptive threshold processing

The threshold value is obtained from the neighborhood and converted.

python


plt.figure(figsize=(9, 6), dpi=100,
           facecolor='w', linewidth=0, edgecolor='w')

plt.gray()

#Original image
plt.subplot(2,2,1)
plt.axis('off') 
plt.imshow(gray)

henkango = 255 #How to convert the value of the one that exceeds the threshold
blocksize = 11 #Neighborhood area size for threshold calculation(Odd after 3)
c = 16 #Subtracted value


#Adaptive Thresholded Processing: MEAN
plt.subplot(2,2,3)
plt.title("MEAN", fontsize=10)
dst = cv2.adaptiveThreshold(gray, henkango, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, blocksize, c)
plt.axis('off') 
plt.imshow(dst)

#Adaptive Thresholded Processing: GAUSSIAN_C
plt.subplot(2,2,4)
plt.title("GAUSSIAN", fontsize=10)
dst = cv2.adaptiveThreshold(gray, henkango, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, blocksize, c)
plt.axis('off') 
plt.imshow(dst)

image.png

Recommended Posts

Threshold processing (adaptive thresholded)