[[0 1 1 0 0 0] [0 1 1 0 1 0] [0 0 0 1 1 1] [0 0 0 0 1 0]]
The function label generates an array where each object is assigned a unique number:
input, structure=None, output=None) |
>>> a = array([[0,1,1,0,0,0],[0,1,1,0,1,0],[0,0,0,1,1,1],[0,0,0,0,1,0]]) >>> s = [[0, 1, 0], [1,1,1], [0,1,0]] >>> print label(a, s) (array([[0, 1, 1, 0, 0, 0], [0, 1, 1, 0, 2, 0], [0, 0, 0, 2, 2, 2], [0, 0, 0, 0, 2, 0]]), 2)
>>> a = array([[0,1,1,0,0,0],[0,1,1,0,1,0],[0,0,0,1,1,1],[0,0,0,0,1,0]]) >>> s = [[1,1,1], [1,1,1], [1,1,1]] >>> print label(a, s)[0] [[0 1 1 0 0 0] [0 1 1 0 1 0] [0 0 0 1 1 1] [0 0 0 0 1 0]]
>>> l, n = label([1, 0, 1, 0, 1]) >>> print l [1 0 2 0 3] >>> l = where(l != 2, l, 0) >>> print l [1 0 0 0 3] >>> print label(l)[0] [1 0 0 0 2]
Note: The structuring element used by label is assumed to be symmetric.
Given an array of labeled objects the find_objects functions can be used to generate a list of slices that for each object, give the smallest sub-array that fully contains the object:
input, max_label=0) |
>>> a = array([[0,1,1,0,0,0],[0,1,1,0,1,0],[0,0,0,1,1,1],[0,0,0,0,1,0]]) >>> l, n = label(a) >>> f = find_objects(l) >>> print a[f[0]] [[1 1] [1 1]] >>> print a[f[1]] [[0 1 0] [1 1 1] [0 1 0]]
>>> print find_objects([1, 0, 3, 4], max_label = 3) [(slice(0, 1, None),), None, (slice(2, 3, None),)]
The list of slices generated by find_objects is useful to find the position and dimensions of the objects in the array, but can also be used to perform measurements on the individual objects. Say we want to find the sum of the intensities of an object in image:
>>> image = arange(4*6,shape=(4,6)) >>> mask = array([[0,1,1,0,0,0],[0,1,1,0,1,0],[0,0,0,1,1,1],[0,0,0,0,1,0]]) >>> labels = label(mask)[0] >>> slices = find_objects(labels)
>>> print where(labels[slices[1]] == 2, image[slices[1]], 0).sum() 80
>>> print sum(image, labels, 2) 80.0
>>> print sum(image[slices[1]], labels[slices[1]], 2) 80.0
>>> print sum(image, labels, [0, 2]) [178.0, 80.0]
The measurement functions described below all support the index parameter to indicate which object(s) should be measured. The default value of index is None. This indicates that all elements where the label is larger than zero should be treated as a single object and measured. Thus, in this case the labels array is treated as a mask defined by the elements that are larger than zero. If index is a number or a sequence of numbers it gives the labels of the objects that are measured. If index is a sequence, a list of the results is returned.
input, labels=None, index=None) |
input, labels=None, index=None) |
input, labels=None, index=None) |
input, labels=None, index=None) |
input, labels=None, index=None) |
input, labels=None, index=None) |
input, labels=None, index=None) |
input, labels=None, index=None) |
input, labels=None, index=None) |
Send comments to the NumArray community.