I want to cluster non-zero locations in a NumPy 2D array for MSER detection. Then I want to find the number of points in each cluster and remove those clusters which do not have number of points between some x and y (10 and 300).
I have tried clustering them by searching with neighbouring points but the method fails for concave-shaped non-zero clusters.
[[0, 1, 0, 0, 1],
[0, 1, 1, 1, 1],
[0, 0, 0, 0, 0],
[1, 1, 0, 1, 1],
[1, 0, 0, 1, 1]]
should output, for x=4 and y=5 (both included)
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 1, 1],
[0, 0, 0, 1, 1]]
I'm not sure I have understood your question correctly, but I think scikit-image's label
and regionprops
could get the job done.
In [6]: import numpy as np
In [7]: from skimage import measure, regionprops
In [8]: img = np.array([[0, 7, 0, 0, 7],
...: [0, 9, 1, 1, 4],
...: [0, 0, 0, 0, 0],
...: [2, 1, 0, 2, 1],
...: [1, 0, 0, 6, 4]])
...:
In [9]: arr = measure.label(img > 0)
In [10]: arr
Out[10]:
array([[0, 1, 0, 0, 1],
[0, 1, 1, 1, 1],
[0, 0, 0, 0, 0],
[2, 2, 0, 3, 3],
[2, 0, 0, 3, 3]])
In [11]: print('Label\t# pixels')
...: for region in measure.regionprops(arr):
...: print(f"{region['label']}\t{region['area']}")
...:
Label # pixels
1 6
2 3
3 4