はじめに
skimage.filtersのthreshold_otsuによるしきい値処理について説明する。
コード

解説
モジュールのインポート
画像データの読み込み
Epithelantha属の月世界の種子を用いる。rgb2grayによりグレースケール画像とする。

大津の2値化によるしきい値処理
threshold_otsu(image)でimageに大津の2値化を適用したときのしきい値を得ることができる。
そのしきい値でimage > threshのようにすればTrueとFalseのバイナリ画像が得られる。不等号を>から<にすれば、TrueとFalseが入れ替わるので、画像は白黒が反転する。
大津の2値化については下記が詳しい。

大津の二値化ってなんだ…ってなった. - Qiita
はじめにOpenCVを利用して二値化を行う際, 「とりあえずcv2.THRESH_OTSUやっとけばええやろ, ぽいー」って感じでテキトーに二値化してました.「とりあえずいい感じに動く」って認識…
ヒストグラムと画像の表示
左にオリジナル画像、中央にヒストグラム、右にしきい値処理した画像を示す。
ヒストグラムについては下記で説明した。

[matplotlib] 6.ヒストグラム
matplotlibでヒストグラムを作成することのできるplt.hist()について説明する。
異なるしきい値処理を一度に試す
try_all_thresholdで下記の方法でしきい値処理した結果を表示することができる。
skimage.filters.thresholding.threshold_isodata skimage.filters.thresholding.threshold_li skimage.filters.thresholding.threshold_mean skimage.filters.thresholding.threshold_minimum skimage.filters.thresholding.threshold_otsu skimage.filters.thresholding.threshold_triangle skimage.filters.thresholding.threshold_yen

参考
Thresholding — skimage 0.25.0 documentation
skimage.filters — skimage 0.25.1 documentation
skimage.filters — skimage 0.25.0 documentation
コメント