Hi Max, somehow your problem keeps me occupied! Here come two more ideas which - I think - can deal with branches. As test image I am using this image:
The first option is to use FindShortestTour
and remove any resulting long jumps (the code should be self explaining):
testImg = << "testImg.txt";
pts = ImageValuePositions[testImg, 1];
ptso = pts[[Last@FindShortestTour[pts]]];
bigJumps = Select[MapIndexed[{First[#2], #1} &,
BlockMap[EuclideanDistance @@ # &, ptso, 2, 1]], Last[#] > 5 &];
splitIndx = First /@ bigJumps;
Set[ptso[[#]], "marker"] & /@ splitIndx;
ptso1 = SequenceSplit[ptso, {"marker"}];
Graphics[{Red, Thick, Line /@ ptso1}]
The other option is to "hijack" the function ImageGraphics
, which results in a tightly enclosing line:
ig = ImageGraphics[testImg, Method -> "Exact"];
igPts = Cases[Normal[ig], FilledCurve[__], Infinity][[2, 1, 1, 1, 1]];
Manipulate[Graphics[{Line[igPts[[;; n]]]}, Frame -> True,
PlotRange -> CoordinateBounds[igPts]], {n, 1, Length[igPts], 1}]
Does that help? Regards -- Henrik
Attachments: