シンプルなしきい値処理を行って単純な二値化をしてみます。
threshold
cv.threshold(src, thresh, maxval, type[, dst])
src – 入力画像(グレースケール)
thresh – しきい値
maxval – しきい値の最大値( THRESH_BINARYと、THRESH_BINARY_INVで使われる値 )
type – しきい値の種類
サンプルコード
import cv2
import numpy as np
import matplotlib.pyplot as plt
image = cv2.imread("image.png", 0)
plt.subplot(2, 3, 1)
plt.title('original')
plt.imshow(image, cmap='gray')
ret, thresh = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
plt.subplot(2, 3, 2)
plt.title('BINARY')
plt.imshow(thresh, cmap='gray')
ret, thresh = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY_INV)
plt.subplot(2, 3, 3)
plt.title('BINARY_INV')
plt.imshow(thresh, cmap='gray')
ret, thresh = cv2.threshold(image, 127, 255, cv2.THRESH_TRUNC)
plt.subplot(2, 3, 4)
plt.title('TRUNC')
plt.imshow(thresh, cmap='gray')
ret, thresh = cv2.threshold(image, 127, 255, cv2.THRESH_TOZERO)
plt.subplot(2, 3, 5)
plt.title('TOZERO')
plt.imshow(thresh, cmap='gray')
ret, thresh = cv2.threshold(image, 127, 255, cv2.THRESH_TOZERO_INV)
plt.subplot(2, 3, 6)
plt.title('TOZERO_INV')
plt.imshow(thresh, cmap='gray')
plt.show()