Skip to content Skip to sidebar Skip to footer

Isolate Greatest/smallest Labeled Patches From Numpy Array

i have a large numpy array and labeled it with the connected component labeling in scipy. Now i want to create subsets of this array, where only the biggest or smallest labels in s

Solution 1:

Here is the full code:

import numpy
from scipy import ndimage

array = numpy.zeros((100, 100), dtype=np.uint8)
x = np.random.randint(0, 100, 2000)
y = np.random.randint(0, 100, 2000)
array[x, y] = 1

pl.imshow(array, cmap="gray", interpolation="nearest")

s = ndimage.generate_binary_structure(2,2) # iterate structure
labeled_array, numpatches = ndimage.label(array,s) # labeling

sizes = ndimage.sum(array,labeled_array,range(1,numpatches+1)) 
# To get the indices of all the min/max patches. Is this the correct label id?
map = numpy.where(sizes==sizes.max())[0] + 1 
mip = numpy.where(sizes==sizes.min())[0] + 1

# inside the largest, respecitively the smallest labeled patches with values
max_index = np.zeros(numpatches + 1, np.uint8)
max_index[map] = 1
max_feature = max_index[labeled_array]

min_index = np.zeros(numpatches + 1, np.uint8)
min_index[mip] = 1
min_feature = min_index[labeled_array]

Notes:

  • numpy.where returns a tuple
  • the size of label 1 is sizes[0], so you need to add 1 to the result of numpy.where
  • To get a mask array with multiple labels, you can use labeled_array as the index of a label mask array.

The results:

enter image description here

enter image description here

enter image description here

Solution 2:

first you need a labeled mask, given a mask with only 0(background) and 1(foreground):

labeled_mask, cc_num = ndimage.label(mask)

then find the largest connected component:

largest_cc_mask = (labeled_mask == (np.bincount(labeled_mask.flat)[1:].argmax() + 1))

you can deduce the smallest object finding by using argmin()..

Post a Comment for "Isolate Greatest/smallest Labeled Patches From Numpy Array"