Consider an image (img
) as
I want to create a binary mask which will exactly cover the foreground.
If I do the following
b = DeleteSmallComponents@FillingTransform@Binarize[img];
skeleton = SkeletonTransform[b];
pruned = Pruning[skeleton, 1, 1];
mask = InverseDistanceTransform[pruned]
I get
You can see that the head is not completely white as desired and this may be due to the fact that the bottom of the head is connected to the boundary of image. Secondly, near the neck (as indicated by the green line), some pixels are black which should be white.
How can I fix these two issues?
MorphologicalBinarize
instead ofBinarize
and also play with the second argument. $\endgroup$MorphologicalBinarize[img, {.1, .5}, CornerNeighbors -> False]
is a good starting point. $\endgroup$