The best answer I could manage is to use the following:
- First I got the vertices using
MorphologicalGraph
function:
AbsoluteOptions[MorphologicalGraph[myBinMovie[[frame]]], VertexCoordinates]

- Sometimes, this would give me more than two vertices so I used an
If
function to find the furthest two vertices based on the EuclideanDistance
function:
myMaxDisVerts =Table[If[Length@myVertices[[frame, 1, 2]] == 2,
myVertices[[frame, 1, 2]],
myTempMaxDis =
Max /@ Table[
EuclideanDistance[myVertices[[frame, 1, 2, i]],
myVertices[[frame, 1, 2, j]]], {i, 1,
Length@myVertices[[frame, 1, 2]], 1}, {j, 1,
Length@myVertices[[frame, 1, 2]], 1}];
myVertices[[frame, 1, 2,
Flatten@Position[myTempMaxDis, Max[myTempMaxDis]]]]], {frame, 1,
Length@myVertices, 1}]
- Using these points, I then took the closest 70 pixels that were a value of 1 from the Binary image using the
ReplacePixelValue
function:
Table[ReplacePixelValue[
ConstantImage[0, ImageDimensions@myMaskedMovie[[1]]],
Flatten[Table[(NearestTo[myMaxDisVerts[[frame, j]],
70(*Determines size of head and tail masks*)][
test6[[frame]]]), {j, 1, Length@myMaxDisVerts[[frame]], 1}],
1] -> 1], {frame, 1, Length@myMaxDisVerts, 1}];

- I then took the centroid of these Binary images that made up the "head" and "tail" of the larvae using
ComponentMeasurements
and found their closest neighbors through each frame:
myNearestHTPosCentroid =
ComponentMeasurements[#, "Centroid"] & /@ myNearestHTPos;
- Plotted the centroid and the head and tail points joined points with the original movie:
myCentroidJoinedLarvaColMovie =
Table[Show[myMaskedMovie[[frame]],
ListLinePlot[{myCentroidPos[[
frame]], {myHeadAndTailTracks[[1, frame,
2, {2, 3}]]}, {myHeadAndTailTracks[[2, frame, 2, {2, 3}]]}},
PlotMarkers -> {myOrangeCircMark, myCyanCircMark,
myRedCircMark}],
ListPlot[{Flatten[myCentroidPos[[1 ;; frame]], 1],
myHeadAndTailTracks[[1, 1 ;; frame, 2, {2, 3}]],
myHeadAndTailTracks[[2, 1 ;; frame, 2, {2, 3}]]},
PlotStyle -> {Lighter[Orange, 0.3], Darker[Cyan, 0.005], Red},
Joined -> True]], {frame, 1, Length@myHeadAndTailTracks[[1]],
1}];

MorphologicalGraph
does a fairly good job to identify the ends of the larvae (tryShow[img, MorphologicalGraph[img]]
). But it is far from perfect... $\endgroup$SkeletonTransform
was also one of my first guesses. And indeed, there is too much artificial branching going on... =/ $\endgroup$