Segmentation

This module provides functions to segmentate an image into superpixels. It is largely based on the scikit-image toolbox. Apart from the regular segmentation functions it provides postprocessing functions to ensure connected segments in a regular grid. It also provides various visualization tools for segmented images.

Superpixels

flamingo.segmentation.superpixels.average_colors(img, segments)[source]

Average colors per superpixels

Returns an image where each pixel has the average color of the superpixel that it belongs to.

Parameters:
  • img (np.ndarray) – NxM or NxMx3 array with greyscale or colored image information respectively
  • segments (np.ndarray) – NxM matrix with segment numbering
Returns:

NxM or NxMx3 matrix with averaged image

Return type:

np.ndarray

flamingo.segmentation.superpixels.check_segmentation(segments, nx, ny)[source]

Checks if segmentation data is complete

Checks if the segmentation data indeed contains nx*ny segments and if the set of segment numbers is continuous.

Parameters:
  • segments (np.ndarray) – NxM matrix with segment numbering
  • ny (nx,) – Size of supposed segmentation grid
Returns:

Returns true if segmentation is valid and false otherwise

Return type:

bool

flamingo.segmentation.superpixels.get_contours(segments)[source]

Return contours of superpixels

Parameters:segments (np.ndarray) – NxM matrix with segment numbering
Returns:list of lists for each segment in segments. Each segment list contains one or more contours. Each contour is defined by a list of 2-tuples with an x and y coordinate.
Return type:list

Examples

>>> contours = get_contours(segments)
>>> plot(contours[0][0][0][0], contours[0][0][0][1]) # plot first contour of first segment
flamingo.segmentation.superpixels.get_segmentation(img, method='slic', method_params={}, extract_contours=False, remove_disjoint=True)[source]

Return segmentation of image

Parameters:
  • img (np.ndarray) – NxM or NxMx3 array with greyscale or colored image information respectively
  • method (str, optional) – Segmentation method to use, supported by scikit-image toolbox
  • method_params (dict, optional) – Extra parameters supplied to segmentation method
  • extract_contours (bool, optional) – Also extract contours of segments
  • remove_disjoint (bool, optional) – Ensure that the output contains connected segments only and that the superpixels form a more or less regular grid. In case the segmentation method does not provide both constraints, the constraint is ensured in a postprocessing step.
Returns:

NxM matrix with segment numbering

Return type:

np.ndarray

Examples

>>> img = argus2.rest.get_image(station='kijkduin')[0]
>>> segments = get_segmentation(img)
flamingo.segmentation.superpixels.get_superpixel_grid(segments, img_shape)[source]

Return shape of superpixels grid

Parameters:
  • segments (np.ndarray) – NxM matrix with segment numbering
  • img_shape (2-tuple or list) – Dimensions of image
Returns:

tuple containing M and N dimension of regular superpixel grid

Return type:

2-tuple

flamingo.segmentation.superpixels.shuffle_pixels(img)[source]

Shuffle class identifiers

Parameters:img (np.ndarray) – NxM matrix with segment numbering
Returns:NxM matrix with shuffled segment numbering
Return type:np.ndarray

Examples

>>> seg = get_segmentation(img)
>>> fig, axs = plt.subplots(1, 2)
>>> axs[0].imshow(seg)
>>> axs[1].imshow(shuffle_pixels(seg))

Postprocessing

flamingo.segmentation.postprocess.region_growing(mask, connectivity=8)[source]

Simple region growing algorithm

Parameters:
  • mask (np.ndarray) – Binary matrix indicating what pixels are within a region and what are not
  • connectivity (int, 4 or 8) – Number of neighbouring pixels taken into account
Returns:

  • list – List of 2-tuples with coordinates within a region
  • list – List of 2-tuples with coordinates at the edge of the region

flamingo.segmentation.postprocess.regularize(segments, nx, ny)[source]

Create a regular grid from a collection of image segments

The number of segments supplied is supposed to be larger than the number of segments in the target grid (nx*ny). A regular grid of size nx*ny over the image grid NxM is constructed. Subsequently, the segments are ordered based on size. the nx*ny largest segments are preserved and assigned to a single grid cell in the regular grid based on least squares fit. The smaller segments are added to the preserved segment that is closest based on their centroids.

Parameters:
  • segments (np.ndarray) – NxM matrix with segment numbering
  • ny (nx,) – Dimensions of the target superpixel grid
Returns:

NxM matrix with alternative segment numbering with segments in a regular grid

Return type:

np.ndarray

flamingo.segmentation.postprocess.remove_disjoint(segments)[source]

Remove disjoint regions in segmentation

Remove disjoint regions in segmentation by running a region growing algorihtm for each segment. Any segment that appears to consist out of multiple disconnected parts is splitted. The biggest part remains as placeholder of the existing superpixel. The smaller parts are joined with the neighbouring superpixels. If multiple neighbouring superpixels exist, the one that shares the largest border is chosen.

Parameters:segments (np.ndarray) – NxM matrix with segment numbering
Returns:NxM matrix with alternative segment numbering with connected segments
Return type:np.ndarray

Visualization

flamingo.segmentation.plot.get_image_data(fig, dpi=96, axis_only=True, transparent=True)[source]

Get binary image data

Parameters:
  • fig (matplotlib.figure.Figure) – Figure object containing axis object
  • dpi (int, optional) – Image resolution
  • axis_only (bool, optional) – Only include contents of axis
  • transparent (bool, optional) – Plot background transparent
Returns:

Binary image data

Return type:

str

flamingo.segmentation.plot.plot(img, segments, mark_boundaries=True, shuffle=False, average=False, slice=1, raw=False)[source]

Plot segmentation result

Parameters:
  • img (np.ndarray) – NxM or NxMx3 array with greyscale or colored image information respectively
  • segments (np.ndarray) – NxM matrix with segment numbering
  • mark_boundaries (bool, optional) – Draw boundaries in image
  • shuffle (bool, optional) – Shuffle segment numbering for more scattered coloring (ignored when average is used)
  • average (bool, optional) – Average colors per segment
  • slice (int, optional) – Use slice to reduce the image size
  • raw (bool, optional) – Return raw binary output
Returns:

Binary image data or 2-tuple with matplotlib.figure.Figure and matplotlib.axes.AxesSubplot objects

Return type:

str or 2-tuple

flamingo.segmentation.plot.plot_image(img, cmap='Set2', dpi=96, slice=0, transparent=True, raw=False)[source]

Get binary image data

Parameters:
  • img (np.ndarray) – NxM or NxMx3 array with greyscale or colored image information respectively
  • cmap (matplotlib.colors.Colormap, optional) – Colormap to determine colors for individual patches
  • dpi (int, optional) – Image resolution
  • slice (int, optional) – Use slice to reduce the image size
  • transparent (bool, optional) – Plot background transparent
  • raw (bool, optional) – Return raw binary output
Returns:

Binary image data or 2-tuple with matplotlib.figure.Figure and matplotlib.axes.AxesSubplot objects

Return type:

str or 2-tuple

Table Of Contents

Previous topic

Rectification

Next topic

Classification

This Page