注記
完全なサンプルコードをダウンロードするにはこちらへ またはBinder経由でブラウザでこの例を実行するには。
多クラスOtsu閾値処理#
多クラスOtsu閾値[1]は、入力画像のピクセルをいくつかの異なるクラスに分割するために使用される閾値処理アルゴリズムであり、各クラスは画像内のグレースケールの強度に従って得られます。
多クラスOtsuは、目的のクラスの数によって決定される複数の閾値を計算します。デフォルトのクラス数は3です。3つのクラスを取得するには、アルゴリズムは2つの閾値を返します。それらは以下のヒストグラムで赤線で示されています。

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from skimage import data
from skimage.filters import threshold_multiotsu
# Setting the font size for all plots.
matplotlib.rcParams['font.size'] = 9
# The input image.
image = data.camera()
# Applying multi-Otsu threshold for the default value, generating
# three classes.
thresholds = threshold_multiotsu(image)
# Using the threshold values, we generate the three regions.
regions = np.digitize(image, bins=thresholds)
fig, ax = plt.subplots(nrows=1, ncols=3, figsize=(10, 3.5))
# Plotting the original image.
ax[0].imshow(image, cmap='gray')
ax[0].set_title('Original')
ax[0].axis('off')
# Plotting the histogram and the two thresholds obtained from
# multi-Otsu.
ax[1].hist(image.ravel(), bins=255)
ax[1].set_title('Histogram')
for thresh in thresholds:
ax[1].axvline(thresh, color='r')
# Plotting the Multi Otsu result.
ax[2].imshow(regions, cmap='jet')
ax[2].set_title('Multi-Otsu result')
ax[2].axis('off')
plt.subplots_adjust()
plt.show()
スクリプトの総実行時間: (0分0.714秒)