How can I extract each cell (= patch that is not crossed by the contour) from an image such as

enter image description here

I thought SelectComponents (e.g. SelectComponents[img, #Width > 1 &]) could do it but I did not manage. I need to keep track of the position of the cell also, so the output could be a few hundreds images of the same size as the original image, with white everywhere except for the cells.


1 Answer 1


Here is a start:

img = ColorConvert[Import["https://i.sstatic.net/8SREb.png"], "Grayscale"];
MorphologicalComponents[Binarize[img, .7]] // Colorize

Mathematica graphics

If you want some insight, why this works, then look at the binary image. You'll see that each white patch is surrounded by a black line.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.