# Extract individual white parts of binarized image

Suppose we have an image, like so:

and I managed to create its MorphologicalGraph by using

p1 = Binarize[*the image*];
p2 = Thinning[Pruning[Thinning[DeleteSmallComponents[ColorNegate[DeleteSmallComponents[p1, 300]], 300]]]]
MorphologicalGraph[p2]


Which gave the result of

If we overlay the two, we can see how most of it lines up. Is there a way to extract the white parts in the image individually? I'm thinking of using MorphologicalGraph to extract the enclosed spaces, but any method / tip that can achieve extracting the white parts individually is perfect and greatly appreciated.

For example, each of those numbered is a part that is to be extracted.

One approach is to use:

MorphologicalComponents[Erosion[img, 1]] // Colorize


Then you can access the individual colored sections using ComponentMeasurements. For example:

ComponentMeasurements[comp // Colorize, {"Image", "Count", "Mean"}, All, "Dataset"]


gives a long list of all the segments and how large they are. You can sort them by many different properties. Here is a piece of the output:

You're looking for MorphologicalComponents[*the image*]. This function groups connected areas of white pixels and assigns them a single integer. For example;

mc = MorphologicalComponents[Binarize[*the image*]]


Then

Graphics[Flatten@
Table[Style[Text[mc[[i, j]], {i, j}], 8], {i, 300, 350}, {j, 300,
350}], ImageSize -> 8 72]


gives the following subset of the mc data

As you can see, each cluster of white pixels now has a unique number, such as 81 in the case of the upper left corner of this graphic. The 0s correspond to the black areas. You can use SelectComponents[mc, -criteria-] to search for large, small, round, etc features. Below I choose the 10 largest.

Colorize@SelectComponents[mc, "Area", -10]


To see which integers are assigned to which cluster of white pixels, use the following;

centroids = ComponentMeasurements[mc, "Centroid"];


where centroids[[1]] gives 1 -> {674.491, 672.}

Show[
Colorize@mc,
Graphics[ {White,
Table[ Text[centroids[[i, 1]], centroids[[i, 2]]], {i, Length@centroids}]}]
]