Hi, I'm Ramu. This also implements the familiar binarization in image processing.
Binarization is the process of converting an image into a monochrome image with only two colors, black and white. This is usually done for grayscale images. Also, when binarizing, a standard value called a threshold is determined. Pixels below the threshold are replaced with white, and pixels with pixel values above the threshold are replaced with black.
binarization.py
import numpy as np
import cv2
import matplotlib.pyplot as plt
plt.gray()
def binarization(img):
#Image copy
dst = img.copy()
#Grayscale
gray = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY)
#Threshold
th = 128
#Binarization
idx = np.where(gray < th)
gray[idx] = 0
idx = np.where(gray >= th)
gray[idx] = 255
return gray
#Image reading
img = cv2.imread('../assets/imori.jpg')
#Binarization
mono = binarization(img)
#Save image
cv2.imwrite('result.jpg', mono)
#Image display
plt.imshow(mono)
plt.show()
The image on the left is the input image, and the image on the right is the output image. Like the grayscale image, there is no gray and it is only white and black.
If you have any questions, please feel free to contact us. imori_imori's Github has the official answer, so please check that as well. ..
Recommended Posts