Labeled Image Functions

Labeled images are integer images where the values correspond to different regions. I.e., region 1 is all of the pixels which have value 1, region two is the pixels with value 2, and so on. By convention, region 0 is the background and processed differently.

Labeling Images

New in version 0.6.5.

The first step is obtaining a labeled function from a binary function:

import mahotas
import numpy as np
from pylab import imshow, show

regions = np.zeros((8,8), bool)

regions[:3,:3] = 1
regions[6:,6:] = 1
labeled, nr_objects = mahotas.label(regions)

imshow(labeled, interpolation='nearest')
show()

(Source code, png, hires.png, pdf)

_images/labeled-1.png

This results in an image with 3 values:

  1. background, where the original image was 0
  2. for the first region: (0:3, 0:3);
  3. for the second region: (6:, 6:).

There is an extra argument to label: the structuring element, which defaults to a 3x3 cross (or, 4-neighbourhood).

Borders

A border pixel is one where there is more than one region in its neighbourhood (one of those regions can be the background).

You can retrieve border pixels with either the borders() function, which gets all the borders or the border() (note the singular) which gets only the border between a single pair of regions. As usual, what neighbour means is defined by a structuring element, defaulting to a 3x3 cross.

API Documentation

The mahotas.labeled submodule contains the functions mentioned above. label() is also available as mahotas.label.

mahotas.labeled.borders(labeled, Bc={3x3 cross}, output={np.zeros(labeled.shape, bool)})

Compute border pixels

A pixel is on a border if it has value i and a pixel in its neighbourhood (defined by Bc) has value j, with i != j.

Parameters :

labeled : ndarray of integer type

input labeled array

Bc : structure element, optional

output : ndarray of same shape as labeled, dtype=bool, optional

where to store the output. If None, a new array is allocated

Returns :

border_img : boolean ndarray

Pixels are True exactly where there is a border in labeled

mahotas.labeled.border(labeled, i, j, Bc={3x3 cross}, output={np.zeros(labeled.shape, bool)}, always_return=True)

Compute the border region between i and j regions.

A pixel is on the border if it has value i (or j) and a pixel in its neighbourhood (defined by Bc) has value j (or i).

Parameters :

labeled : ndarray of integer type

input labeled array

i : integer

j : integer

Bc : structure element, optional

output : ndarray of same shape as labeled, dtype=bool, optional

where to store the output. If None, a new array is allocated

always_return : bool, optional

if false, then, in the case where there is no pixel on the border, returns None. Otherwise (the default), it always returns an array even if it is empty.

Returns :

border_img : boolean ndarray

Pixels are True exactly where there is a border between i and j in labeled

mahotas.labeled.bwperim(bw, n=4)

Find the perimeter of objects in binary images.

A pixel is part of an object perimeter if its value is one and there is at least one zero-valued pixel in its neighborhood.

By default the neighborhood of a pixel is 4 nearest pixels, but if n is set to 8 the 8 nearest pixels will be considered.

Parameters :

bw : ndarray

A black-and-white image (any other image will be converted to black & white)

n : int, optional

Connectivity. Must be 4 or 8 (default: 4)

Returns :

perim : ndarray

A boolean image

See also

borders
function This is a more generic function
mahotas.labeled.remove_bordering(labeled, rsize=1, output={np.empty_like(im)})

Remove objects that are touching the border.

Pass im as output to achieve in-place operation.

Parameters :

labeled : ndarray

Labeled array

rsize : int, optional

Minimum distance to the border (in Manhatan distance) to allow an object to survive.

output : ndarray, optional

If im is passed as output, then it operates inline.

Returns :

slabeled : ndarray

Subset of labeled

mahotas.labeled.label(array, Bc={3x3 cross}, output=None)

Label the array

Parameters :

array : ndarray

This will be interpreted as an integer array

Bc : ndarray, optional

This is the structuring element to use

output : ndarray, optional

Output array. Must be a C-array, of type np.int32

Returns :

labeled : ndarray

Labeled result

nr_objects : int

Number of objects

mahotas.labeled.labeled_sum(array, labeled)

Labeled sum. sum will be an array of size labeled.max() + 1, where sum[i] is equal to np.sum(array[labeled == i]).

Parameters :

array : ndarray of any type

labeled : int ndarray

Label map. This is the same type as returned from mahotas.label()

Returns :

sums : 1-d ndarray of array.dtype

mahotas.labeled.labeled_size(labeled)

Equivalent to:

for i in xrange(...):
    sizes[i] = np.sum(labeled == i)

but, naturally, much faster.

Parameters :labeled : int ndarray
Returns :sizes : 1-d ndarray of int

See also

mahotas.fullhistogram
almost same function by another name (the only

difference

Table Of Contents

Previous topic

Distance Transform

Next topic

Polygon Utitilities

This Page