3

Say I have an array like this:

import numpy as np

arr = np.array([
   [1, 1, 3, 3, 1],
   [1, 3, 3, 1, 1],
   [4, 4, 3, 1, 1],
   [4, 4, 1, 1, 1]
])

There are 4 distinct regions: The top left 1s, 3s, 4s and right 1s.

How would I get the paths for the bounds of each region? The coordinates of the vertices of the region, in order.

For example, for the top left 1s, it is (0, 0), (0, 2), (1, 2), (1, 1), (2, 1), (2, 0)

(I ultimately want to end up with something like start at 0, 0. Right 2. Down 1. Right -1. Down 1. Right -1. Down -2., but it's easy to convert, as it's just the difference between adjacent vertices)

I can split it up into regions with scipy.ndimage.label:

from scipy.ndimage import label

regions = {}
# region_value is the number in the region
for region_value in np.unique(arr):
    labeled, n_regions = label(arr == region_value)
    regions[region_value] = [labeled == i for i in range(1, n_regions + 1)]

Which looks more like this:

{1: [
    array([
        [ True,  True, False, False, False],
        [ True, False, False, False, False],
        [False, False, False, False, False],
        [False, False, False, False, False]
    ], dtype=bool),  # Top left 1s region
    array([
        [False, False, False, False,  True],
        [False, False, False,  True,  True],
        [False, False, False,  True,  True],
        [False, False,  True,  True,  True]
    ], dtype=bool)  # Right 1s region
 ],
 3: [
    array([
        [False, False,  True,  True, False],
        [False,  True,  True, False, False],
        [False, False,  True, False, False],
        [False, False, False, False, False]
    ], dtype=bool)  # 3s region
 ],
 4: [
    array([
        [False, False, False, False, False],
        [False, False, False, False, False],
        [ True,  True, False, False, False],
        [ True,  True, False, False, False]
    ], dtype=bool)  # 4s region
 ]
}

So how would I convert that into a path?

Artyer
  • 31,034
  • 3
  • 47
  • 75

1 Answers1

0

a pseudo code idea would be to do the following:

scan multi-dim array horizontally and then vertically until you find True value (for second array it is (0,4))
output that as a start coord
since you have been scanning as determined above your first move will be to go right.
repeat until you come back:
    move one block in the direction you are facing.
    you are now at coord x,y
    check values of ul=(x-1, y-1), ur=(x-1, y), ll=(x, y-1), lr=(x,y)
    # if any of above is out of bounds, set it as False
    if ul is the only True:
         if previous move right:
             next move is up
         else:
             next move is left
         output previous move
         move by one
   ..similarly for other single True cells..
   elif ul and ur only True or ul and ll only True or ll and lr only True or ur and lr only True:
        repeat previous move
   elif ul and lr only True:
        if previous move left:
            next move down
        elif previous move right:
            next move up
        elif preivous move down:
            next move left:
        else:
            next move right
        output previous move
        move one
   elif ul, ur, ll only Trues:
        if previous move left:
            next move down
        else:
            next move right
        output previous move, move by one
   ...similarly for other 3 True combos...

for the second array it will do the following:

finds True val at 0,4
start at 0,4
only lower-right cell is True, so moves right to 0,5 (previous move is None, so no output)
now only lower-left cell is True, so moves down to 1,5 (previous move right 1 is output)
now both left cells are True, so repeat move (moves down to 2,5)
..repeat until hit 4,5..
only upper-left cell is True, so move left (output down 4)
both upper cells are true, repeat move (move left to 3,4)
both upper cells are true, repeat move (move left to 2,4)
upper right cell only true, so move up (output right -3)
..keep going until back at 0,4..

Try visualising all the possible coord neighbouring cell combos and that will give you a visual idea of the possible flows. Also note that with this method it should be impossible to be traversing a coord which has all 4 neighbours as False.

Landar
  • 278
  • 1
  • 10