2

For a given array (as the one suggested below) and a given value (here, 0), I would like to count how many 0 can be associate at a same convex hull.

array :

1 2 4 5 8 9 7
4 0 0 7 5 6 8
6 5 0 4 3 5 2
1 0 0 5 7 0 6
2 3 5 7 8 9 4

For the array suggested, the solution should get : [5, 1] because a first "pattern" of five 0 can be identify between the second and fourth line and the second/third columns. A second pattern with only one 0 exist.

Have you any idea about how to get that ? I know how to count the number of 0. Maybe I can do something with the mask option of numpy array ?

Best regards and thank's for your help,

3
  • I think that by "convex hull" you really mean "connected component". If I understand correctly, the first group of 5 zeros would not be a convex hull because the 5 lies between two elements of the group. Commented Jun 6, 2018 at 8:51
  • @Peter yes "connected component" is better for what I want to do. So yes, the is a pattern of five 0 connected together and a pattern of one 0. How can I quantify these connected component ? Commented Jun 6, 2018 at 8:55
  • I found some informations on internet with "connected component" keyword, thank's ! Commented Jun 6, 2018 at 8:57

1 Answer 1

2

What you're looking for is the "connected components" in the array. Scipy has a convenient label function for this:

import numpy as np
from scipy.ndimage.measurements import label

a = np.array([
    [1, 2, 4, 5, 8, 9, 7],
    [4, 0, 0, 7, 5, 6, 8],
    [6, 5, 0, 4, 3, 5, 2],
    [1, 0, 0, 5, 7, 0, 6],
    [2, 3, 5, 7, 8, 9, 4],])

labs, n_components = label(a==0)

component_sizes = [np.sum(labs==i) for i in range(1, n_components+1)]

print(component_sizes)

Prints [5, 1]

Sign up to request clarification or add additional context in comments.

1 Comment

Oh thank you so much. I was looking on this topic : stackoverflow.com/questions/46737409/… I was looking how to count the number of component on each labels ! Thank's for your answer, it's working like a charm ! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.