Napari - A multidimensional image viewer

Introduction

Napari is an open – source python tool which makes viewing with images easier especially n-dimensional images. It used in many domains especially for scientific purposes. With over 93 use cases showcased on its website, it is one of the most versatile image viewers for higher dimension. It also allows plugins to be installed on to extend it its capabilities. It has many options for image viewing , like sliders for viewing 3+ dimensions voltage, changing Colour cmaps ,adjusting contrast etc, with just one click. It uses vispy python library to view the image and interact with the image/shapes.

Some examples of how Napari can be used

  1. Annotating images or videos with napari points , labels or shapes
  2. Segmentation and bounding of elements of images, for example creating a rectangle around coins in a given “segement “ of the image
  3. Tracking the data points in the dataset .
Link in the official napari tutorials: https://napari.org/stable/tutorials/

Installation and Setup

  • Installation using PIP

    Run the following in a windows terminal. We can install napari using PIP (python installation package)

     python -m pip install "napari[all]"
    [all] indicates the default framework. If you want to use another framework you can replace all with something like pyside2.
    We then need to update our napari using:
     python -m pip install "napari[all] --upgrade"
    We can check if installation has succeeded by running
     python -m pip install "napari[all] --upgrade"

There are many other ways to install napari mentioned in the official guide below: Here

Running napari for the first time

We will be showing how to run it on a Jupyter Notebook.
We can run the following code the first start napari.
from skimage.data import cells3d

import napari

# create a `Viewer` and `Image` layer here
viewer, image_layer = napari.imshow(cells3d())

To run it in a normal python script you need to add:

 napari.run() 
Here is a short explaination for what the above code is doing.Here is an image processing tool in python. However, over here we have used it to only take some data.The "Viewer" is the napari viewer's GUI. "Image" refers to the image that we are showing which in this case is cells3d. For example Your window may look something like this: A Napari viewer open with cell3d image loaded in it. An advantage of using jupyter notebook or Ipython is you you can dynamically interact wil the viewer with code, i.e making changes code will change the viewer.

Links to FAQ's regarding installation and setup: Here

Key Features & Explaination

Multidimensional Viewing

Watch the below video to see how multidimensional images can be viewed using sliders in the napari viewer

The above image is a 4D array - hence 2 sliders are present . Each slider corresponds to going traversing through a particular dimension in an array. For every combination of values of the 2 sliders, we have a range for x and y (2D image). This helps to pack a lot related information together.


Interactive Image Viewing

In the video below we can see how easily we change the color maps , set constrast limits . We can also easily annotate the video using the various LAYERS present Napari viewer

Notice that while addig the points a new layer "Points" is created. Like adding the points layer allowed us to add points to the image there are many layers like shapes and labels which helps to annotate the images. Layers are one of the most useful features of napari.

We can also easily zoom , pan,transform the axis and change the order of the axes to be viewed of the image with a simple click


Fast Handling of large datasets

Napari can handle large datasets easily as it does not load the data all at once. Rather it loads the data as we change the parameters, for example as we change the slides it loads the data. This helps to process and view large datasets efficiently. It uses Dask for such large and complex data processing. It "lazily-loads" the data. It also has support for GPU acceleration. This is really helpful as most multi-dimensional files are huge , and viewing them normally requires more computation powers.


Plugins

We can install custom plugins for Napari. These plugins extend the usability for Napari.


Compatibility with other libraries

We can use many other scientific computing libraries of python like numpy and scipy with napari.


Using Python

Here are some examples on how we can use python to make changes in napari. Jupyter Notebook is used here.

The cells3d image data is being used over here.

Adding points to image

          
          # Adding Points Layer 
          #Random co-ordinates
          points=np.random.randint(0,256,size=(10,2))
    
          #addding layer in napari
          points_layer = viewer.add_points(points)
            
          #customising
          points_layer.face_color='white'
            

Result

What is viewer.layers? It contains the layer in the viewer


          viewer.layer

          Output:
        [<Image layer 'Image' at 0x21452e8ccb0>, <Points layer 'Points' at 0x21452f6ad20>, <Points layer 'points' at 0x21458b84a70>]

Changing Colormap and changing interpolation mode


           
    # Setting Colormap to 'viridis'
    viewer.layers[0].colormap = 'viridis'
    #Changing interpolation mode
    viewer.layers[0].interpolation3d='kaiser'

Colormap dictates how the numerical values present in the data are converted into a color. Interpolation in a very loose sense helps to changes the sharpness of the image

Result

Toggling the visibility

We can set the layers to be invisble to work at a single layer at a time

    #Turning the visibility off
    viewer.layers[1].visible=False
  

Result

Making a polygon / shape


  # Add a polygon defined by a list of points
  # Points are given as (x, y) tuples
  polygon = [(179,84), (179,139),(132,139),(132,84)] 
   # Four vertices of the polygon
 
  # Add the polygon shape to the viewer
  viewer.add_shapes([polygon], shape_type='polygon', edge_color='green',opacity=0.3)
 

Viewing 3D object - blobs

code source: napari website Code:

  from scipy import ndimage as ndi
from skimage import data

import napari

blobs = data.binary_blobs(
        length=128, volume_fraction=>0.1, n_dim=3
        )[::2].astype(float)
labeled = ndi.label(blobs)[0]

viewer = napari.Viewer(ndisplay=3)
viewer.add_image(blobs, name='blobs', scale=(2, 1, 1))
viewer.add_labels(labeled, name='blob ID', scale=(2, 1, 1))
 

Result:



If I set ndisplay=2 then:

Here ndisplay sets the no.of dimension of viewing in which the image is to be opened


Explaination of the code:

  • data.binary_blobs creates these blobs.
  • length shows thee dimension in this case is 128x128x128.
  • volume_fraction determines how much of the cubes is occupied by blobs.
  • n_dim represent the number of dimensions.[::2] slices the the generated array.For faster computatioon and generation.
  • This effectively reduces the image size scale by 2 (selects every second element).
  • ndi here is used to assign all the "blobs"
  • some label and the number of labels (hence [0] is done).
  • viewer.add_label assign colour to every label (creates a 'labels' layer)

Adding labels to image with program


        from skimage import data
        from skimage.filters import threshold_otsu
        from skimage.measure import label
        from skimage.morphology import remove_small_objects
        from skimage.segmentation import clear_border
        
        import napari
        
        image = data.coins()
        
        # apply threshold
        thresh = threshold_otsu(image)
        bw = image > thresh
        
        # remove objects near the image 
        cleared = remove_small_objects(clear_border(bw))
        
        # label the individual images
        label_image = label(cleared).astype(int)
        
        # start the viewer and rgb false since we are assigning color
        viewer = napari.view_image(image, name='data.coins', rgb=False)
        
        # add the labels
        label_layer = viewer.add_labels(label_image, name='color labels')
        napari.run()
      

In this code we have imported threshold_otsu, remove_small_objects and clear_border,label.

  • threshold_otsu splits the image into a binary one. This is used to create a threshold or a distinguisher in the image.
  • remove_small_objectshelps to filter out small objects. Here it is filtering around the border using the clear_border. We can also give it and argument above which size should it filter out
  • After creating a filter , we have removed small objects.
  • label assigns a number to each coin
  • We are finally adding the image and the labels layer ,which colorizes the integer given by label
Code from: https://napari.org/stable/gallery/add_labels.html#sphx-glr-gallery-add-labels-py

Results

Use Cases

1. Bioimaging & Microscopy Napari is widely used in cell segmentation, fluorescence imaging which includes applying colormaps to say track the movement of nuclei of cells and highlight using labels which cells are of interest. Time-based movement of cells can also be captured.

2. Deep Learning & AI in Imaging It is commonly used for training data for AI models. Colormaps again play a big role along with dimensional viewing. There are plugins present for AI as well.

3. Medical Imaging Researchers use Napari to visualize MRI, CT, and histological images. They can be highly useful to detect issues say I need to know where the issue is inside someone's lungs, I can use maximum intensity projection to view which aras might be affected. This can be done with things like this. Annotations like points or shapes can be used.


Conclusion

Overall Napari seems to be a powerful tool for image viewing and its further application. Its uses in the field of microsocopy and biology show this. However, I noticed a that there were minor bugs here and there. Another thing to note is that the full-power of napari can only utilised when used in collaboration with other python libraries like how scipy was used to label the blobs automatically , whereas if we were to only use napari we would have to do it by hand. Since ,Napari is a open-source tool it continue to be better , with a better library of plugins availbale for use.Its ability to load huge datasets smoothly makes applicable for practical purposes.

References and further reading

Further Reading:

Using ImageJ with napari: https://pmc.ncbi.nlm.nih.gov/articles/PMC10591722/
Use cases listed on website:https://napari.imagej.net/en/latest/Use_Cases.html
Video on using napari : https://youtu.be/VXdFOcBCto4?si=njwj8s2G16lvfs_v
Official Napari Github: https://github.com/napari/napari

References:

The Video on Napari mentioned above.
Official Napari website: "https://napari.org/stable/"
HTML:https://www.w3schools.com/html/