Scikit Image - Using Napari



Scikit Image with 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 −

  • 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.
  • Overlay derived data − Napari enables you to overlay derived data such as points, polygons, segmentations, labels, and more.
  • 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.

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

To use Napari, you will need the following requirements −

  • Python > = 3.8 − Napari requires Python version 3.8 or higher. Make sure you have a compatible version installed on your system.
  • 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 −

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

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

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

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

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')

Output

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

Napari

Example 2

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')

Output

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

Napari
Advertisements