How do I locate the end points of a bridge-like structure in an image?
Below is a generalized representation.
Here you have a code example in Mathematica, probably not optimal:
f[i_] :=
Module[{t, i2, w, z, neighbours, i3, cRed},
(t = Thinning[ColorNegate@i, 15];
i2 = ImageData@Binarize[ DeleteSmallComponents[
ImageSubtract[t, Dilation[Erosion[t, 1], 1]], 100], .1];
For[w = 2, w < Dimensions[i2][[1]], w++,
For[z = 2, z < Dimensions[i2][[2]], z++,
If[i2[[w, z]] == 1 && i2[[w + 1, z + 1]] == 1,
i2[[w, z + 1]] = i2[[w + 1, z]] = 0];
If[i2[[w, z]] == i2[[w - 1, z - 1]] == 1,
i2[[w, z - 1]] = i2[[w - 1, z]] = 0];
If[i2[[w, z]] == i2[[w + 1, z - 1]] == 1,
i2[[w, z - 1]] = i2[[w + 1, z]] = 0];
If[i2[[w, z]] == i2[[w - 1, z + 1]] == 1,
i2[[w, z + 1]] = i2[[w - 1, z]] = 0];
]
];
neighbours[l_, k_, j_] :=
l[[k - 1, j]] + l[[k + 1, j]] + l[[k, j + 1]] + l[[k, j - 1]] +
l[[k + 1, j + 1]] + l[[k + 1, j - 1]] + l[[k - 1, j + 1]] +
l[[k - 1, j - 1]];
i3 = Table[
If[i2[[w, z]] ==1,neighbours[i2, w, z], 0],{w,2,Dimensions[i2][[1]]-1},
{z,2,Dimensions[i2][[2]]-1}];
cRed =
ColorNegate@Rasterize[Graphics[{Red, Disk[]}], ImageSize -> 15];
ImageCompose[
ImageCompose[i,
cRed, {#[[2]], Dimensions[i2][[1]] - #[[1]]} &@
Position[i3, 1][[1]]],
cRed, {#[[2]], Dimensions[i2][[1]] - #[[1]]} &@
Position[i3, 1][[2]]])];