注記
完全なサンプルコードをダウンロードするには、こちらへ。または、Binder経由でブラウザでこの例を実行できます。
NiblackとSauvolaの閾値処理#
NiblackとSauvolaの閾値は、特にテキスト認識[1]、[2]において、背景が一様でない画像に役立つ局所閾値処理技術です。画像全体に対して単一のグローバル閾値を計算する代わりに、ピクセルを中心としたウィンドウ(周囲のピクセルを含む)の平均と標準偏差を考慮した特定の式を使用して、各ピクセルに対して複数の閾値が計算されます。
ここでは、これらのアルゴリズムを使用して画像をバイナリ化し、一般的なグローバル閾値処理技術と比較します。パラメータwindow_size
は、周囲のピクセルを含むウィンドウのサイズを決定します。

import matplotlib
import matplotlib.pyplot as plt
from skimage.data import page
from skimage.filters import threshold_otsu, threshold_niblack, threshold_sauvola
matplotlib.rcParams['font.size'] = 9
image = page()
binary_global = image > threshold_otsu(image)
window_size = 25
thresh_niblack = threshold_niblack(image, window_size=window_size, k=0.8)
thresh_sauvola = threshold_sauvola(image, window_size=window_size)
binary_niblack = image > thresh_niblack
binary_sauvola = image > thresh_sauvola
plt.figure(figsize=(8, 7))
plt.subplot(2, 2, 1)
plt.imshow(image, cmap=plt.cm.gray)
plt.title('Original')
plt.axis('off')
plt.subplot(2, 2, 2)
plt.title('Global Threshold')
plt.imshow(binary_global, cmap=plt.cm.gray)
plt.axis('off')
plt.subplot(2, 2, 3)
plt.imshow(binary_niblack, cmap=plt.cm.gray)
plt.title('Niblack Threshold')
plt.axis('off')
plt.subplot(2, 2, 4)
plt.imshow(binary_sauvola, cmap=plt.cm.gray)
plt.title('Sauvola Threshold')
plt.axis('off')
plt.show()
スクリプトの実行時間合計:(0分0.347秒)