Visualization

Functions in instanseg.utils.visualization for displaying images and segmentations. They can also be imported from instanseg.utils.utils.

Displaying images

show_images(*img_list, clip_pct=None, titles=None, save_str=False, n_cols=3, axes=False, cmap='viridis', labels=None, dpi=None, timer_flag=None, colorbar=True, **args)

Designed to plot torch tensor and numpy arrays in windows robustly

display_as_grid(display_list, ncols, padding=2, left_titles=None, top_titles=None, right_side=None, title_height=20, fontsize=12)

Display a list of images in a grid layout.

display_colourized(mIF, random_seed=0)

Convert a multi-channel image to a colorized RGB display.

Drawing labels

save_image_with_label_overlay(im, lab, output_dir='./', base_name='image', clip_percentile=1.0, scale_per_channel=None, label_boundary_mode='inner', label_colors=None, alpha=1.0, thickness=3, return_image=False)

Save an image as RGB alongside a corresponding label overlay. This can be used to quickly visualize the results of a segmentation, generally using the default image viewer of the operating system.

Parameters:
  • im (ndarray) – Image to save. If this is an 8-bit RGB image (channels-last) it will be used as-is, otherwise it will be converted to RGB

  • lab (ndarray) – Label image to overlay

  • output_dir (str) – Directory to save to

  • base_name (str) – Base name for the image files to save

  • clip_percentile (float) – Percentile to clip the image at. Used during RGB conversion, if needed.

  • scale_per_channel (bool) – Whether to scale each channel independently during RGB conversion.

  • label_boundary_mode – A boundary mode compatible with scikit-image find_boundaries; one of ‘thick’, ‘inner’, ‘outer’, ‘subpixel’. If None, the lab is used directly.

  • alpha – Alpha value for the underlying image when using the overlay. Setting this less than 1 can help the overlay stand out more prominently.

Returns:

The input image with corresponding label overlay if lab is a torch Tensor, or nothing otherwise.

label_to_color_image(label_img, **kwargs)

Convert a labelled image to a color image using the Glasbey colormap. Label 0 -> black, label -1 -> white, other labels -> Glasbey colors.

Parameters:

label_img (np.ndarray) – 2D array of shape (H, W) with integer labels.

Returns:

RGB image of shape (H, W, 3), dtype=np.uint8

Return type:

np.ndarray

display_cells_and_nuclei(lab)

Display cells and nuclei labels as a colored overlay.

apply_cmap(x, fg_mask=None, cmap='coolwarm_r', bg_intensity=255, normalize=True)

Apply a colormap to an image, with a background mask. x and fg_mask should have the same shape, and be numpy arrays.