Begin My Journey of IMAGE


function of scipy.ndimage.measurements.label

# scipy.ndimage.measurements.label(input, structure=None, output=None)
>>> a = np.array([[0,0,1,1,0,0],
...               [0,0,0,1,0,0],
...               [1,1,0,0,1,0],
...               [0,0,0,1,0,0]])
>>> labeled_array, num_features = label(a)

>>> print(num_features)
4
>>> print(labeled_array)
array([[0, 0, 1, 1, 0, 0],
       [0, 0, 0, 1, 0, 0],
       [2, 2, 0, 0, 3, 0],
       [0, 0, 0, 4, 0, 0]])

the default structuring element is:

[[0,1,0],
 [1,1,1],
 [0,1,0]]

>>> s = generate_binary_structure(2,2)
>>> s = [[1,1,1],
         [1,1,1],
         [1,1,1]]
Label the image using the new structuring element:

>>> labeled_array, num_features = label(a, structure=s)
# Show the 2 labeled features 
(note that features 1, 3, and 4 from above 
are now considered a single feature):

>>> print(num_features)
2
>>> print(labeled_array)

array([[0, 0, 1, 1, 0, 0],
       [0, 0, 0, 1, 0, 0],
       [2, 2, 0, 0, 1, 0],
       [0, 0, 0, 1, 0, 0]])

留言

這個網誌中的熱門文章

AndrewNg's CNN notes(practical issues)

New findings in Numpy