Hi Johannes,
This sounds like a fun exercise. First, regarding skeletonization (a.k.a.
Topological Skeleton). In general, as you can see even in the wikipedia page, the topological skeleton is very sensitive to kinks in the external contour. In order to minimize this you can try smoothing the image, usually helps and play a bit with the skeletonization parameters.
Unforuntately the general rule is that it can be hard to extract a perfect line, as you've realized. You can also try a different skeletonization method such as doing different iterations of the Erode morphological operator. However, my experience is that in general this will give equally bad results.
Regarding the contour representation, yes, the Contour structure is a bit klunky. It comes from OpenCV and it takes into account that contours are usually extracted into a list, but can also have nested contours (i.e. holes within holes, etc). Whether the full hierarchy is computed depends on the Mode parameter of FindContours (to compute the full hierarchy, set it to Tree).
Anyway, HPrev/HNext and VPrev/VNext allows you to traverse through the hierarchy of contours (but not through the contour points themselves) . In general, I try to avoid doing this traversal by hand, and usually compute the BinaryRegionAnalysis / LargestBinaryRegion and just access the Contour property of each binary region.
If you just have a single Contour, then accessing the points in Python is trivial:
import clr
clr.AddReference("OpenCV.Net")
from OpenCV.Net import *
from System import Array
@returns(Array[Point])
def process(value):
if value.Area > 0:
points = value.Contour.ToArray[Point]()
else:
points = Array.CreateInstance(Point, 0)
return points
The tests are to make sure there is a valid contour present, which happens only when the region has a non-zero area.
Btw, I believe most algorithms for skeleton extraction use local neighborhood analysis, meaning that you start at the tip, look at the density of the neighborhood, and walk to the middle of the density, look again, another step, etc, etc.
Probably there are more experienced zebrafish people in the list that can advise better.
Hope this helps!