1. 安裝 scikit-image#

1.1. 支援的平台#

  • x86 處理器上的 Windows 64 位元

  • x86 和 ARM (M1 等) 處理器上的 macOS

  • x86 和 ARM 處理器上的 Linux 64 位元

雖然我們不正式支援其他平台,您仍然可以嘗試從原始碼建置

1.2. 版本檢查#

若要查看是否已安裝 scikit-image 或檢查安裝是否成功,請在 Python shell 或 Jupyter Notebook 中執行以下程式碼

import skimage as ski
print(ski.__version__)

或者,從命令列執行

python -c "import skimage; print(skimage.__version__)"

(如果 python 無法執行,請嘗試 python3。)

如果已安裝 scikit-image,您將看到版本號碼,否則將會看到錯誤訊息。

1.3. 透過 pip 和 conda 安裝#

1.3.1. pip#

pip 安裝的先決條件:您必須能夠在命令列上使用 pip 來安裝套件。

我們強烈建議使用虛擬環境。虛擬環境會建立一個乾淨的 Python 環境,不會干擾現有的系統安裝,可以輕鬆移除,並且只包含您的應用程式所需的套件版本。

要安裝目前的 scikit-image,您至少需要 Python 3.10。如果您的 Python 版本較舊,pip 將會找到最新的相容版本。

# Update pip
python -m pip install -U pip

# Install scikit-image
python -m pip install -U scikit-image

需要一些額外的相依性才能存取 skimage.data 中的所有範例資料集。使用以下方式安裝它們

python -m pip install -U scikit-image[data]

若要安裝擴展 scikit-image 功能(包括例如平行處理)的可選科學 Python 套件,請使用

python -m pip install -U scikit-image[optional]

警告

請勿同時使用 sudopip 命令,因為 pip 可能會覆寫重要的系統函式庫。

1.3.2. conda#

我們建議使用 miniforge,這是一個使用 conda-forge 的最小發行版。它會安裝 Python 並提供虛擬環境。

設定好 conda 環境後,使用以下方式安裝 scikit-image

conda install scikit-image

1.4. 系統套件管理員#

使用套件管理員(aptdnf 等)來安裝 scikit-image 或其他 Python 套件並不是最佳選擇,因為您可能會取得較舊的版本。此外,也會變得難以安裝套件管理員未提供的其他 Python 套件。

1.5. 下載所有示範資料集#

我們的一些範例影像(位於 skimage.data 中)託管在網路上,並且預設不安裝。這些影像會在首次存取時下載。如果您希望下載所有示範資料集,以便可以離線存取,請確保已安裝 pooch,然後執行

python -c 'import skimage as ski; ski.data.download_all()'

1.6. 其他協助#

如果您仍有疑問,請透過以下方式聯繫我們

若要建議變更這些指示,請在 GitHub 上開啟一個問題

2. 為貢獻者安裝 scikit-image#

您的系統需要

  • C 編譯器、

  • C++ 編譯器,以及

  • scikit-image 支援的 Python 版本(請參閱 pyproject.toml)。

首先,在 GitHub 上 fork scikit-image 儲存庫。然後在本機複製您的 fork,並設定一個指向原始 scikit-image 儲存庫的 upstream 遠端

注意

我們在下方使用 git@github.com;如果您沒有設定 SSH 金鑰,請改用 https://github.com

git clone git@github.com:YOURUSERNAME/scikit-image
cd scikit-image
git remote add upstream git@github.com:scikit-image/scikit-image

以下所有命令都是在複製的 scikit-image 目錄中執行。

2.1. 建置環境設定#

設定一個專為 scikit-image 量身打造的 Python 開發環境。這裡我們提供兩個熱門環境管理員的指示:venv (pip) 和 conda (miniforge)。

2.1.1. venv#

# Create a virtualenv named ``skimage-dev`` that lives outside of the repository.
# One common convention is to place it inside an ``envs`` directory under your home directory:
mkdir ~/envs
python -m venv ~/envs/skimage-dev

# Activate it
# (On Windows, use ``skimage-dev\Scripts\activate``)
source ~/envs/skimage-dev/bin/activate

# Install development dependencies
pip install -r requirements.txt
pip install -r requirements/build.txt

# Install scikit-image in editable mode. In editable mode,
# scikit-image will be recompiled, as necessary, on import.
spin install -v

提示

上述程式碼會將 scikit-image 安裝到您的環境中,使其可供 IDE、IPython 等存取。這並非絕對必要;您也可以使用以下方式建置

spin build

在這種情況下,不會安裝函式庫,但可以透過 spin 命令存取,例如 spin testspin ipythonspin run 等。

2.1.2. conda#

我們建議使用 miniforge 安裝 conda,這是 Anaconda 的替代方案,沒有授權費用。

安裝 miniforge 之後

# Create a conda environment named ``skimage-dev``
conda create --name skimage-dev

# Activate it
conda activate skimage-dev

# Install development dependencies
conda install -c conda-forge --file requirements/default.txt
conda install -c conda-forge --file requirements/test.txt
conda install -c conda-forge pre-commit ipython
conda install -c conda-forge --file requirements/build.txt

# Install scikit-image in editable mode. In editable mode,
# scikit-image will be recompiled, as necessary, on import.
spin install -v

提示

上述程式碼會將 scikit-image 安裝到您的環境中,使其可供 IDE、IPython 等存取。這並非絕對必要;您也可以使用以下方式建置

spin build

在這種情況下,不會安裝函式庫,但可以透過 spin 命令存取,例如 spin testspin ipythonspin run 等。

2.2. 測試#

執行完整的測試套件

spin test

或執行一部分測試

# Run tests in a given file
spin test skimage/morphology/tests/test_gray.py

# Run tests in a given directory
spin test skimage/morphology

# Run tests matching a given expression
spin test -- -k local_maxima

2.3. 新增功能分支#

貢獻新功能時,請透過功能分支執行此操作。

首先,提取最新的原始碼

git switch main
git pull upstream main

建立您的功能分支

git switch --create my-feature-name

使用可編輯安裝,scikit-image 會根據需要重新建置自身。如果您是手動建置,請使用以下方式重新建置

.. code-block:: sh

spin build

重複的增量建置通常可以正常運作,但如果您發現建置問題,請使用以下方式從頭開始重新建置

spin build --clean

2.4. 平台特定注意事項#

Windows

在 Windows 上建置 scikit-image 是我們持續整合測試的一部分;步驟顯示在此Azure Pipeline 中。

Debian 和 Ubuntu

在函式庫編譯之前安裝合適的編譯器

sudo apt-get install build-essential

2.5. 完整需求列表#

建置需求

# Generated via tools/generate_requirements.py and pre-commit hook.
# Do not edit this file; modify pyproject.toml instead.
meson-python>=0.16
setuptools>=68
ninja>=1.11.1.1
Cython>=3.0.8
pythran>=0.16
numpy>=2.0
spin==0.13
build>=1.2.1

執行階段需求

# Generated via tools/generate_requirements.py and pre-commit hook.
# Do not edit this file; modify pyproject.toml instead.
numpy>=1.24
scipy>=1.11.2
networkx>=3.0
pillow>=10.1
imageio>=2.33,!=2.35.0
tifffile>=2022.8.12
packaging>=21
lazy-loader>=0.4

測試需求

# Generated via tools/generate_requirements.py and pre-commit hook.
# Do not edit this file; modify pyproject.toml instead.
asv
numpydoc>=1.7
pooch>=1.6.0
pytest>=7.0
pytest-cov>=2.11.0
pytest-localserver
pytest-faulthandler
pytest-doctestplus

文件需求

# Generated via tools/generate_requirements.py and pre-commit hook.
# Do not edit this file; modify pyproject.toml instead.
sphinx>=8.0
sphinx-gallery[parallel]>=0.18
numpydoc>=1.7
sphinx-copybutton
matplotlib>=3.7
dask[array]>=2022.9.2
pandas>=2.0
seaborn>=0.11
pooch>=1.6
tifffile>=2022.8.12
myst-parser
intersphinx-registry>=0.2411.14
ipywidgets
ipykernel
plotly>=5.20
kaleido==0.2.1
scikit-learn>=1.2
sphinx_design>=0.5
pydata-sphinx-theme>=0.16
PyWavelets>=1.6
pytest-doctestplus

開發人員需求

# Generated via tools/generate_requirements.py and pre-commit hook.
# Do not edit this file; modify pyproject.toml instead.
pre-commit
ipython
tomli; python_version < '3.11'

資料需求

只有在安裝以下套件後,才能使用完整的示範資料集。

# Generated via tools/generate_requirements.py and pre-commit hook.
# Do not edit this file; modify pyproject.toml instead.
pooch>=1.6.0

選用需求

您可以使用上述的基本需求來使用 scikit-image,但某些功能只有在安裝以下套件後才能使用。

  • Matplotlib 用於各種功能,例如繪圖、分割、讀取圖像。

  • Dask dask 模組用於平行化某些功能。

在更罕見的情況下,您可能還需要以下套件:

  • PyAMG pyamg 模組用於隨機漫步分割的快速 cg_mg 模式。

  • Astropy 提供 FITS 輸入/輸出功能。

  • SimpleITK 選用的輸入/輸出外掛程式,提供多種格式,包括生物醫學影像中使用的特殊格式。

# Generated via tools/generate_requirements.py and pre-commit hook.
# Do not edit this file; modify pyproject.toml instead.
SimpleITK
astropy>=5.0
cloudpickle>=0.2.1
dask[array]>=2021.1.0,!=2024.8.0
matplotlib>=3.7
pooch>=1.6.0
pyamg>=5.2
PyWavelets>=1.6
scikit-learn>=1.2

2.6. 貢獻者安裝協助#

請參閱上方的額外協助