Usage Guide#
# only use this next line if in a notebook
%matplotlib ipympl
import matplotlib.pyplot as plt
import numpy as np
from mpl_image_segmenter import ImageSegmenter
from mpl_image_segmenter.example_images import color_image_stack, gray_image_stack
# load a sample image
gray_image_stack = gray_image_stack()
color_image_stack = color_image_stack()
example_color_image = color_image_stack[0]
example_gray_image = gray_image_stack[0]
Single class - Single Gray Image#
A single grayscale image (shape (Y, X)
) and only class in the segmentation mask is the simplest use case.
You can click and drag to select regions. Scroll to zoom and middle click to pan are provided by the mpl-pan-zoom library.
segmenter = ImageSegmenter(
example_gray_image,
figsize=(7, 7),
)
display(segmenter)
Erasing#
To start erasing instead of selecting set segmenter.erasing = True
. Run the following cell then try to lasso an area you have already selected
segmenter.erasing = True
Looking at the mask.#
The mask is accessible via ImageSegmenter.mask
. 0
is used as the value of unselected regions.
segmenter.mask
array([[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]])
Single class - Single Color Image#
If you have a RGB(A) image, i.e. with shape (Y, X, 3)
or (Y, X, 4)
then you must pass color_image=True
to the ImageSegmenter
constructor in order to have the shapes behave approriately.
ImageSegmenter(example_color_image, color_image=True)
Multiple classes#
You can use multiple classes in the mask with the classes
argument. It accepts either an integer, or an iterable class names as string.
You can change which class is currently being drawn on the mask using the .current_class
attribute. This can always be set to an integer
, or if you are using named classes you can also set it to the name of the class you want to draw.
Warning
The class index is NOT zero indexed. This is because class 0
is always the background.
colors
By default the tab10
color set will be used, and if that is not enough the xkcd color named colors will be used. Or you can pass a list of colors via the mask_colors
argument.
segmenter = ImageSegmenter(example_gray_image, classes = ["class1", "class2", "class3"])
# segmenter = ImageSegmenter(gray_image, classes = 3)
# set the class
segmenter.current_class = 1 # same as setting to `class1`
segmenter.current_class = "class2" # same as setting to 2
Image Stacks#
For both gray and color images you can pass in an array-like stack of images (i.e. shape (N, Y, X, [3])
and then switch which image is currently active using the image_index
property.
segmenter = ImageSegmenter(gray_image_stack)
segmenter
# change which image is displayed
segmenter.image_index = 2
# The mask will now be multidimensional as well
segmenter.mask.shape
(5, 512, 512)
pre-seedng a mask#
You can also load an existing mask. You will need to ensure that it does not have values greater than nclasses
and that it has the same shape as the image. There are currently no safegaurds for this and when there are exceptions in a matplotlib callback they can be hard to see in the notebook - so be careful!
If you run the below cell the image should show up with the premade mask already applied!
from mpl_image_segmenter.example_images import example_mask_stack
mask = example_mask_stack()
preloaded = ImageSegmenter(gray_image_stack, classes=3, mask=mask)
display(preloaded)
Styling#
imshow parameters#
You can modify the display of the image using any kwargs accepted the imshow command. For example set the colormap with the cmap
argument.
fig, axs = plt.subplots(1,2)
gray = ImageSegmenter(example_gray_image, cmap="gray", ax=axs[0])
plasma = ImageSegmenter(example_gray_image, cmap="plasma",ax=axs[1])
LassoSelector line#
You can change the appearance of the LassoSelector line using the props
kwarg. So to make the line very thick and red:
props = {"color": "red", "linewidth": 10}
seg = ImageSegmenter(
example_gray_image, figsize=(5, 5), cmap="gray", props=props
)
display(seg)