Scikit-image 简明教程

Scikit Image - Using Napari

Scikit Image with Napari

Napari 是一个强大的 Python 库,用于 n 维图像的可视化、注释和分析。它提供了一个灵活且交互的环境,用于处理图像数据。以下是 Napari 的一些主要特征和能力 −

Napari is a powerful Python library for n-dimensional image visualization, annotation, and analysis. It provides a flexible and interactive environment for working with image data. Following are some key features and capabilities of Napari −

  1. Viewing and Exploring − Napari allows you to view and explore n-dimensional arrays on a canvas. It supports 2D, 3D, and higher-dimensional data, providing a responsive and interactive visualization experience.

  2. Overlay derived data − Napari enables you to overlay derived data such as points, polygons, segmentations, labels, and more.

  3. Annotation and Editing − Napari provides tools for annotating and editing the derived datasets. You can add, modify, or delete annotations interactively using a variety of tools. Napari integrates with standard data structures like NumPy or Zarr arrays, enabling efficient annotation and analysis workflows.

并且它是一个快速的、交互式的多维图像查看器,在 Python 中使用。此外,它提供了一个用户友好的界面、广泛的定制选项以及与其他科学 Python 库集成的能力。

And it is a fast, interactive viewer for multi-dimensional images in Python. Also, it provides a user-friendly interface, extensive customization options, and the ability to integrate with other scientific Python libraries.

Installation of Napari

要使用 Napari,你需要以下要求 −

To use Napari, you will need the following requirements −

  1. Python > = 3.8 − Napari requires Python version 3.8 or higher. Make sure you have a compatible version installed on your system.

  2. Ability to Install Python Packages − You should have the ability to install Python packages using either pip or conda-forge. These are package managers that allow you to easily install and manage Python libraries and dependencies.

此外,建议拥有以下内容 −

Additionally, it is recommended to have −

环境管理器(如 Conda)− 虽然不是严格必需的,但安装像 Conda 这样的环境管理器可能是非常有益的。Conda 允许你创建隔离的 Python 环境。它提供了一种方便的方法来管理你的 Python 环境,确保其与其他库和工具兼容。

Environment Manager (such as Conda) − While not strictly required, having an environment manager like Conda can be beneficial. Conda allows you to create isolated Python environments. It provides a convenient way to manage your Python environment and ensure compatibility with other libraries and tools.

Installing Napari using pip

要在你的命令提示符中使用 pip 安装 Napari,只需运行以下命令 −

To install Napari using pip, just run the following command in your command prompt −

python -m pip install "napari[all]"

Installing Napari using Conda-Forge

如果你系统中已使用 Anaconda 发行版,那么你可以直接从 conda-forge 通道安装 napari。以下是该命令 −

If you’re using the Anaconda distribution already in your system then you can directly install napari from the conda-forge channel. Following is the command −

conda install -c conda-forge napari

一旦安装了 Napari,你便可以在 Python 脚本中使用它。以下是几个基本的 Python 程序,演示如何将 Napari 库与 scikit-image 结合使用,以有效地执行数据可视化任务。

Once Napari is installed, you can use it in your Python scripts. Below are a few basic Python programs that demonstrate how to use the Napari library along with scikit-image to perform data visualization tasks effectively.

Example 1

以下示例演示如何使用 Napari 库中的 Viewer() 方法显示图像。

The following example demonstrates how to display an image using the Viewer() method in the Napari library.

import napari
from skimage import io

# Read an image
image = io.imread('Images/logo-w.png')

# Display the image using Napari
viewer = napari.Viewer()
viewer.add_image(image, name='Tutorialspoint')

执行上述程序时,你将得到以下输出:

On executing the above program, you will get the following output −

napari 1

Example 2

以下示例演示如何使用 scikit-image 对图像应用圆形掩码,并使用 Napari 库显示原始图像和掩码图像。

The following example demonstrates how to apply a circular mask to an image using scikit-image and display the original image and the masked images using the Napari library.

import napari
from skimage import io

# Load the image
image_path = 'Images_/Zoo.jpg'
image = io.imread(image_path)
image_copy = np.copy(image)

# Create circular mask
rows, cols, _ = image.shape
row, col = np.ogrid[:rows, :cols]
center_row, center_col = rows / 2, cols / 2
radius = min(rows, cols) / 2
outer_disk_mask = ((row - center_row)**2 + (col - center_col)**2 > radius**2)

# Apply mask to image
image[outer_disk_mask] = 0

# Display the image using Napari
viewer = napari.Viewer()
viewer.add_image(image_copy, name='Input Image')
viewer.add_image(image, name='Output masked Image')

执行上述程序时,你将得到以下输出:

On executing the above program, you will get the following output −

napari 2