はじめに
scipyのndimageのpercentile_filterを使って、画像を任意のパーセンタイル点で平滑化する方法について説明する。
コード&解説
モジュールのインポート
バージョン
画像の読み込み
サボテン すみれ丸の画像を、plt.imread()で読み込む。
グレースケール変換
skimage.color の rgb2grayでRGB画像をグレースケール画像に変換する。
変換した画像をcmap=”bone”で表示すると以下のようになる。

パーセンタイルフィルタ
パーセンタイルフィルタのパーセンタイルを10、サイズを10として処理すると、以下のような画像が得られる。高輝度部分である棘が目立たなくなり、棘なしサボテンのようになっていることがわかる。

percentileを変化させた場合
パーセンタイルを大きくすることで、画像がより鮮明になっていく様子がみてとれる。

sizeを変化させた場合
サイズを大きくすることで、画像がボケていく。

footprintを変化させた場合
footprintでフィルタをかける範囲を変更できる。ここでは、skimageのmorphology
を使用して、フィルタのかかる範囲を設定した。結果は以下のようになる。
Generate footprints (structuring elements) — skimage 0.25.1 documentation

参考
percentile_filter — SciPy v1.15.2 Manual
コメント