such questions show up occasionally.
last time, someone wanted to measure snakes, fish, or microscopic worms.
the usual approach:
- morphological thinning (with a good thinning method!), resulting in a binary image
- trace those lines, resulting in a graph. it’s ok to trace from one pixel to all its 8 neighbors. you can collapse “lines” in a second step.
- make sure the graph is a tree (i.e. no cycles). some forks may cause you trouble at the pixel level, appearing as “cycles” of several adjacent pixels. either deal with it at the pixel level somehow, or accept it and instead collapse cycles that span a small area.
- find the longest path in this tree
- hope that it’s a good representation of the midline
I would not recommend working with contours here. no need for approxpolyDP at all. even if you have a contour of a thin thing, you’d have to figure out how to calculate a midline in this contour. that would require you to match every contour point to another contour point “half a circumference” away but spatially closest. that could be made to work but I haven’t explored that, so I would not pick that approach first.