SLIC (scikit-image に含まれる)のインストールと動作確認(スーパーピクセル)(Python を使用)(Windows 上)

scikit-image に実装されているスーパーピクセルを試す.

関連する外部ページ

スーパーピクセルに関する参考記事: https://scikit-image.org/docs/stable/auto_examples/segmentation/plot_segmentations.html

元画像

SLIC

felzenszwalb

quickshift

watershed

前準備

Python 3.12 のインストール

Pythonのインストールを行い、Pythonのプログラムを実行する環境を整える。扱う環境は、Windows搭載パソコンである。金子研究室では、Python 3.12.10を推奨する。

[Windows での Python 3.12 のインストール手順を見るには、ここをクリック]

Windows での Python 3.12 のインストール

以下のいずれかの方法でPython 3.12をインストールする。Pythonがインストール済みの場合、この手順は不要である。

方法 1:winget によるインストール

インストールコマンドの実行方法

管理者権限コマンドプロンプトを起動する(手順:Windowsキーまたはスタートメニュー → cmd と入力 → 右クリック → 「管理者として実行」)。そして、コマンド全体をコマンドプロンプトにコピー&ペーストする。

--scope machine を指定することで、システム全体(全ユーザー向け)にインストールされる。このオプションの実行には管理者権限が必要である。インストール完了後、コマンドプロンプトを再起動するとPATHが反映される。

REM Python 3.12 をシステム領域にインストール
winget install --id Python.Python.3.12 -e --scope machine --silent --accept-source-agreements --accept-package-agreements --override "/quiet InstallAllUsers=1 PrependPath=1 Include_test=0 Include_pip=1 Include_launcher=1 InstallLauncherAllUsers=1 TargetDir=\"C:\Program Files\Python312\""
if not "%ERRORLEVEL%"=="0" ( color 0c & echo Python 3.12 のインストールに失敗しました & ping 127.0.0.1 -n 6 >nul & color )

REM Python と Scripts を PATH 先頭に追加
powershell -NoProfile -Command "$p='C:\Program Files\Python312'; $s=\"$p\Scripts\"; if(Test-Path $p){$k=[Microsoft.Win32.Registry]::LocalMachine.OpenSubKey('SYSTEM\CurrentControlSet\Control\Session Manager\Environment',$true); $c=$k.GetValue('Path','',[Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames); $t=$k.GetValueKind('Path'); $new=$c; if((';'+$new+';') -notlike \"*;$p;*\"){$new=$p+';'+$new}; if((';'+$new+';') -notlike \"*;$s;*\"){$new=$s+';'+$new}; if($new -ne $c){$k.SetValue('Path',$new,$t)}; $k.Close()}"

REM 現在のセッションにも反映(システムPATHを再取得して連結)
for /f "usebackq tokens=2,*" %A in (`reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path`) do set "PATH=%B"

REM pip / wheel の更新
python -m pip install --no-user -U pip wheel
if not "%ERRORLEVEL%"=="0" ( color 0c & echo pip / wheel の更新に失敗しました & ping 127.0.0.1 -n 6 >nul & color )

方法 2:インストーラーによるインストール

  1. Python公式サイト(https://www.python.org/downloads/)にアクセスし、「Download Python 3.x.x」ボタンからWindows用インストーラーをダウンロードする。
  2. ダウンロードしたインストーラーを実行する。
  3. 初期画面の下部に表示される「Add python.exe to PATH」にチェックを入れてから「Customize installation」を選択する。このチェックを入れ忘れると、コマンドプロンプトから python コマンドを実行できない。
  4. 「Install Python 3.xx for all users」にチェックを入れ、「Install」をクリックする。

インストールの確認

コマンドプロンプトで以下を実行する。

python --version

バージョン番号(例:Python 3.12.x)が表示されればインストール成功である。「'python' は、内部コマンドまたは外部コマンドとして認識されていません。」と表示される場合は、インストールが正常に完了していない。

SLIC (scikit-image) のインストール

  1. 以下のコマンドを管理者権限コマンドプロンプトで実行する (手順:Windowsキーまたはスタートメニュー → cmd と入力 → 右クリック → 「管理者として実行」)。
  2. 次のコマンドを実行する.

    python -m pip install -U scikit-image matplotlib
    

スーパーピクセルの実行(scikit-image の SLIC,Python を使用)

SLIC

SLIC を行う Python プログラムを実行する.

  1. 以下の Python プログラムを実行する.Matplotlib を使うので,Jupyter QtConsoleJupyter ノートブック (Jupyter Notebook) の利用が便利である.
    %matplotlib inline
    import matplotlib.pyplot as plt
    import warnings
    warnings.filterwarnings('ignore')   # Suppress Matplotlib warnings
    import skimage.data
    import skimage.color
    import skimage.filters
    import skimage.util
    import skimage.segmentation
    img = skimage.util.img_as_float( plt.imread("d:\lena_std.jpg") )
    plt.imshow(img)
    
    a = skimage.segmentation.slic(img, start_label=1)
    plt.imshow( a )
    
    plt.imshow( skimage.segmentation.mark_boundaries(img, a) )
    

felzenszwalb

felzenszwalb を行う.

以下の Python プログラムを実行する.Matplotlib を使うので,Jupyter QtConsoleJupyter ノートブック (Jupyter Notebook) の利用が便利である.

%matplotlib inline
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')   # Suppress Matplotlib warnings
import skimage.data
import skimage.color
import skimage.filters
import skimage.util
import skimage.segmentation
img = skimage.util.img_as_float( plt.imread("d:\lena_std.jpg") )
plt.imshow(img)

a = skimage.segmentation.felzenszwalb(img)
plt.imshow( a )

plt.imshow( skimage.segmentation.mark_boundaries(img, a) )

quickshift

quickshift を行う.

以下の Python プログラムを実行する.Matplotlib を使うので,Jupyter QtConsoleJupyter ノートブック (Jupyter Notebook) の利用が便利である.

%matplotlib inline
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')   # Suppress Matplotlib warnings
import skimage.data
import skimage.color
import skimage.filters
import skimage.util
import skimage.segmentation
img = skimage.util.img_as_float( plt.imread("d:\lena_std.jpg") )
plt.imshow(img)

a = skimage.segmentation.quickshift(img)
plt.imshow( a )

plt.imshow( skimage.segmentation.mark_boundaries(img, a) )

watershed

watershed を行う.

以下の Python プログラムを実行する.Matplotlib を使うので,Jupyter QtConsoleJupyter ノートブック (Jupyter Notebook) の利用が便利である.

%matplotlib inline
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')   # Suppress Matplotlib warnings
import skimage.data
import skimage.color
import skimage.filters
import skimage.util
import skimage.segmentation
img = skimage.util.img_as_float( plt.imread("d:\lena_std.jpg") )
plt.imshow(img)

a = skimage.segmentation.watershed( skimage.filters.sobel( skimage.color.rgb2gray( img ) ), markers=250 )
plt.imshow( a )

plt.imshow( skimage.segmentation.mark_boundaries(img, a) )