0

I have a 2D array like:

my_array = [[3, 3, 0, 0, 0],
            [3, 3, 0, 0, 0],
            [0, 0, 0, 1, 1],
            [0, 0, 0, 1, 1],
            [0, 0, 0, 1, 1],
            [0, 0, 0, 0, 0,]]

I would like to check the starts and ends of a portion of the array with the same values (different from 0). So in the example above I would like to get something like:

result = [(0,0,1,1), (2,3,4,5)]

Where each tuple on the list is (i_start, j_start, i_end, j_end)

Is there any function that achieves something similar?

Alberto Castaño
  • 186
  • 5
  • 16
  • The likely answer is "no". And I think your example is wrong: your second block seems to correspond to a 2x2 rectangle, but your array contains a 3x2 rectangle. What if the contiguous area is not rectangle-shaped? The problem is highly ill-defined. – Andras Deak -- Слава Україні Jun 10 '17 at 18:10
  • `[(i_start, i_end), (j_start, j_end)]` is more intuitive. Also, are the areas rectangular? You'll have to write your own code to do this. – cs95 Jun 10 '17 at 18:13
  • The closest thing I can think of is [looking for bounding boxes of objects in an image](https://stackoverflow.com/questions/9689173/shape-recognition-with-numpy-scipy-perhaps-watershed). Consider your array to be an image, 0 is the background, 1 and 3 are the objects inside. – Andras Deak -- Слава Україні Jun 10 '17 at 18:15

0 Answers0