4

Let's say you have pixel bitmaps that look something like this: example snake

From this I can easily extract a contour, which will be a concave polygon defined by a set of 2D points. The question is what is the fastest algorithm to pick, from the set of polygon points, the ones that are closest to the natural "endings" of the contour (i.e. the two tips at the end of the U in this case) - so there are $2$ points in the output.

Somehow it looks like it should be related to curvature, although the algorithm should support a large variety of possible shapes, including S, W and other largely curved shapes so I'm hesitant to set any kind of threshold on curvature.

I've tried convex hull methods as well as a couple of variations of the rotating calipers method but still I've found nothing that will convince me that I can reliably and quickly identify the endings/tips of any curved thin line. It's always impressive how humans can pick up on these natural features so fast!

glopes
  • 161
  • 5

3 Answers3

2

Run Flood Fill from any point and the farthest points in the both directions are your result. If you find that the distance in one of the directions is zero it means that it was one of them.

Exploiting the very same idea, if you try finding pixels with the least number of surrounding pixels and apply limited BFS Flood Fill to find out where it can go - at the endpoints there is only one way out. Also going by curvature and then applying the same idea would tell apart the extra bends from results.

Glorfindel
  • 754
  • 1
  • 9
  • 20
Evil
  • 9,525
  • 11
  • 32
  • 53
1

Compute the topological skeleton of the black pixels. This will form a curved line one pixel wide (i.e., it will be isomorphic to a line). Use the two endpoints of this line as your answer. You can find the endpoints by using depth-first search, or (for a more robust solution) by computing the tangent line at each point $p$ and checking for a neighbor connected to $p$ near both sides of the tangent line.

D.W.
  • 167,959
  • 22
  • 232
  • 500
1

Build a graph, with one vertex per black pixel, and an edge between two pixels if they are adjacent. Compute all-pairs shortest-path distances $d(x,y)$. For each vertex $x$, compute

$$f(x) = \max \{d(x,y) : y \in V\}.$$

Find the vertex (pixel) $x$ with the maximum score for $f(x)$. This will be one endpoint of the shape. Find the other vertex $y$ that maximizes $d(x,y)$. This will be the other endpoint of the shape.

D.W.
  • 167,959
  • 22
  • 232
  • 500