straditize.label_selection module

Module for the LabelSelection class

This module defines the LabelSelection class, a base class for the straditize.straditizer.Straditizer straditize.binary.DataReader classes. This class implements the features to select parts of an image and deletes them. The straditize.widgets.selection_toolbar.SelectionToolbar interfaces with instances of this class.

Disclaimer

Copyright (C) 2018-2019 Philipp S. Sommer

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.

Classes

LabelSelection

Class to provide selection functionalities for an image

class straditize.label_selection.LabelSelection[source]

Bases: object

Class to provide selection functionalities for an image

This class provides functionalities to select features in an image. A new selection can be started through enable_label_selection() method and selected parts can be removed through the remove_selected_labels() method.

A 2D boolean mask of the selected pixels can be accessed through the selected_part attribute.

This class generally assumes that the array for the selection is a 2D integer array, e.g. obtained from the skimage.morphology.label() function.

The selection of labels is handled through the colormap. The selection is displayed as a matplotlib image on the ax attribute of this instance. If the color for one label is equal to the cselect color, it is considered as selected. Additionally every cell that has a value greater than the original number of labels is considered to be selected.

Cells with a value of -1 are not selected and cells with a value of 0 cannot be selected.

Attributes

cselect

The RGBA color for selected polygons

cunselect

The RGBA color for unselected polygons

label_arrs

List of attribute names of arrays that should be modified if the labels are about to be removed.

remove_callbacks

Functions that shall be called before the labels are removed.

selected_labeled_part

The selected part as a 2D boolean mask

selected_labels

A list of selected labels in the selection array

selected_part

The selected part as a 2D boolean mask

Methods

copy_cmap(cmap_src, colors)

Copy a colormap with replaced colors

disable_label_selection([remove])

Disable the label selection

enable_label_selection(arr, ncolors[, img, …])

Start the selection of labels

get_default_cmap(ncolors)

The default colormap for binary images

highlight_small_selections([n])

Highlight labels that cover only a small portion of cells

pick_label(event)

Pick the label selected by the given mouseevent

remove_selected_labels([disable])

Remove the selected parts of the diagram

remove_small_selection_ellipses()

Remove the ellipes for small features

select_all_labels()

Select the entire array

select_all_other_labels()

Invert the selection

select_labels(selected)

Select a list of labels

unselect_all_labels()

Clear the selection

cid_select = None
static copy_cmap(cmap_src, colors)[source]

Copy a colormap with replaced colors

This function creates a method that has the same name and the same under, over and bad values as the given cmap_src but with replaced colors

Parameters
Returns

The new colormap

Return type

matplotlib.colors.Colormap

cselect = [1.0, 0.0, 0.0, 1.0]

The RGBA color for selected polygons

cunselect = [0.0, 0.0, 0.0, 0.0]

The RGBA color for unselected polygons

disable_label_selection(remove=None)[source]

Disable the label selection

This will disconnect the pick_event and remove the selection images

Parameters

remove (bool) – Whether to remove the selection image from the plot. If None, the _remove attribute is used

enable_label_selection(arr, ncolors, img=None, set_picker=False, **kwargs)[source]

Start the selection of labels

Parameters
  • arr (2D np.ndarray of dtype int) – The labeled array that contains the features to select.

  • ncolors (int) – The maximum of the labels in arr

  • img (matplotlib image) – The image for the selection. If not provided, a new image is created

  • set_picker (bool) – If True, connect the matplotlib pick_event to the pick_label() method

get_default_cmap(ncolors)[source]

The default colormap for binary images

highlight_small_selections(n=20)[source]

Highlight labels that cover only a small portion of cells

This method uses the skimage.morphology.remove_small_objects() to detect and highlight small features in the diagram. Each feature will be highlighted through an ellipsis around it.

label_arrs = []

List of attribute names of arrays that should be modified if the labels are about to be removed. The attributes might be callable and should then provide the array

pick_label(event)[source]

Pick the label selected by the given mouseevent

remove_callbacks = None

Functions that shall be called before the labels are removed. The keys must be the attributes in the label_attrs list, values must be list of function that accept too arguments, the array and the boolean mask highlighting the cells that will be set to 0

remove_selected_labels(disable=False)[source]

Remove the selected parts of the diagram

This method will call the callbackes in the remove_callbacks attribute for all the attributes in the label_arrs list.

Parameters

disable (bool) – If True, call the disable_label_selection() method at the end

remove_small_selection_ellipses()[source]

Remove the ellipes for small features

Removes the ellipses plotted by the highlight_small_selections() method

select_all_labels()[source]

Select the entire array

select_all_other_labels()[source]

Invert the selection

select_labels(selected)[source]

Select a list of labels

Parameters

selected (np.ndarray) – The numpy array of labels that should be selected

property selected_labeled_part

The selected part as a 2D boolean mask

property selected_labels

A list of selected labels in the selection array

property selected_part

The selected part as a 2D boolean mask

unselect_all_labels()[source]

Clear the selection