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.
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)
This results in an image with 3 values:
There is an extra argument to label: the structuring element, which defaults to a 3x3 cross (or, 4-neighbourhood).
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.
The mahotas.labeled submodule contains the functions mentioned above. label() is also available as mahotas.label.
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
Bc : structure element, optional output : ndarray of same shape as labeled, dtype=bool, optional
|
|---|---|
| Returns : | border_img : boolean ndarray
|
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
i : integer j : integer Bc : structure element, optional output : ndarray of same shape as labeled, dtype=bool, optional
always_return : bool, optional
|
|---|---|
| Returns : | border_img : boolean ndarray
|
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
n : int, optional
|
|---|---|
| Returns : | perim : ndarray
|
See also
Remove objects that are touching the border.
Pass im as output to achieve in-place operation.
| Parameters : | labeled : ndarray
rsize : int, optional
output : ndarray, optional
|
|---|---|
| Returns : | slabeled : ndarray
|
Label the array
| Parameters : | array : ndarray
Bc : ndarray, optional
output : ndarray, optional
|
|---|---|
| Returns : | labeled : ndarray
nr_objects : int
|
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
|
|---|---|
| Returns : | sums : 1-d ndarray of array.dtype |
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 |