Categories: Python

itkwidgets 互動式 Jupyter 影像視覺化工具使用教學與範例

介紹如何在 Jupyter 環境中使用 itkwidgets 模組,互動式顯示影像或網格等 3D 資料。

itkwidgets 是從 itk-vtk-viewer 所衍生出來的 Python 模組,可以讓使用者在 Jupyter 環境之下顯示各種影像分析相關的資料。

安裝 Node.js

NodeSource 的 GitHub 說明,安裝最新版本的 Node.js:

# 安裝 14 版 Node.js
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs

安裝 itkwidgets

itkwidgets 可以透過 pipconda 安裝:

# 透過 pip 安裝 itkwidgets
sudo pip3 install itkwidgets

# 透過 conda 安裝 itkwidgets
conda install -c conda-forge itkwidgets

若需要在 Jupyter Lab 中使用,需要再執行:

# 設定 Jupyter Lab
sudo jupyter labextension install @jupyter-widgets/jupyterlab-manager \
  jupyter-matplotlib jupyterlab-datawidgets itkwidgets

顯示影像與網格

itkwidgets 支援 NumPy、Dask、ITK、VTK、SimpleITK、pyvista、vtkplotter 等各種 2D、3D 影像與網格(mesh)資料格式。

以下是顯示 ITK 的影像與網格資料範例,範例資料是 Allen Brain Altas 的平均腦影像以及標註網格:

import itk
import itkwidgets

# 讀取網格資料
meshType = itk.Mesh[itk.F, 3]
meshReader = itk.MeshFileReader[meshType].New()
meshIO = itk.OBJMeshIO.New()
meshReader.SetMeshIO(meshIO)
meshReader.SetFileName("annotation/ccf_2017/structure_meshes/375.obj")
meshReader.Update()
mesh1 = meshReader.GetOutput()

meshType = itk.Mesh[itk.F, 3]
meshReader = itk.MeshFileReader[meshType].New()
meshIO = itk.OBJMeshIO.New()
meshReader.SetMeshIO(meshIO)
meshReader.SetFileName("annotation/ccf_2017/structure_meshes/698.obj")
meshReader.Update()
mesh2 = meshReader.GetOutput()

# 讀取 Volume 影像
image = itk.imread("average_template/average_template_25.nrrd", itk.US)

# 顯示 Volume 影像與網格
itkwidgets.view(image, geometries = [mesh1, mesh2])
itkwidgets 顯示影像與網格資料

關於更詳細使用說明與範例,可以參考 itkwidgets 的 GitHub 網站。另外亦可使用 help 查閱其使用說明:

# 查看使用說明
help(itkwidgets.view)
Share
Published by
Office Guide

Recent Posts

Python 使用 PyAutoGUI 自動操作滑鼠與鍵盤

本篇介紹如何在 Python ...

9 個月 ago

Ubuntu Linux 以 WireGuard 架設 VPN 伺服器教學與範例

本篇介紹如何在 Ubuntu ...

9 個月 ago

Linux 網路設定 ip 指令用法教學與範例

本篇介紹如何在 Linux 系...

9 個月 ago

Windows 使用 TPM 虛擬智慧卡保護 SSH 金鑰教學與範例

本篇介紹如何在 Windows...

10 個月 ago

Linux 以 Shamir’s Secret Sharing 分割保存金鑰教學與範例

介紹如何在 Linux 中使用...

11 個月 ago

Linux 以 Cryptsetup、LUKS 加密 USB 隨身碟教學與範例

介紹如何在 Linux 系統中...

11 個月 ago