0

I have a raster containing 0 and 1 as a numpy array, all pixels with a 1 are connected forming a line. This line is 1 pixel thick at all places. Using a pathfinding algorithm I have a list of the coordinates of all points (column, row), including the start and end point. The shape of my line is a squared U/rectangle missing the top part.

I want to find the points where the direction of the line changes for a longer stretch. For example from going down to going to the right, or from going to the right to going up.

I have looked at Finding the point of a slope change as a free parameter- Python; however since I my points are all either connected horizontally, vertically or diagonally this does not work. Also my u-shape can be diagonally in the array, therefore I am looking for the point where the type of connectivity changes for more than 2 pixel. Below is an example of my data, I would like to find (5,3)/(6,4) and (6,7)/(5,8)

a = np.array([[0,0,1,0,0,0,0,0,1],
              [0,0,1,0,0,0,0,0,1],
              [0,0,0,1,0,0,0,0,1],
              [0,0,0,1,0,0,0,1,0],
              [0,0,0,1,0,0,0,0,1],
              [0,0,0,1,0,0,0,0,1],
              [0,0,0,0,1,1,1,1,0],
              [0,0,0,0,0,0,0,0,0]])

list_of_points = [(0,2),(1,2),(2,3),(3,3),(4,3),(5,3),(6,4),(6,5),(6,6),(6,7),(5,8),(4,8),(3,7),(2,8),(1,8),(0,8)]
Mai
  • 3
  • 1

1 Answers1

0

You will have 3 directions for the U shape: down, right and up. You can follow the line and the directions using the next rules where O is the current position, and X are possible next pixels:

  1. Down
...
.0.
XXX
  1. Right
..X
.0X
..X
  1. Up
XXX
.0.
...

And following the next algorithm (pseudocode)

while next move can be down:
    move down
// here ends down line

while next move can be right:
    move right
// here ends right line

while next move can be up:
    move up
// here ends up line

If the U shape can be diagonally like this:

.......
...1...
....1..
1....1.
.1.11..
..1....

you can define different matrices for what down, right and up mean. i.e.

// down
...
.0X
.XX

// right
.XX
.0X
...

// up
XX.
X0.
...