We can recalculate MorphologicalComponents
on each of the component and introduce new indexing: the $j$-th subcomponent of the $i$-th component temporarily becomes the $(i\cdot p+j)$-th component (where $p$ is the maximal number of subcomponents in one component), and the background component remains the zeroth component. Finally, we reindex the components.
(* Reconstructing your segmentized image *)
origImg = Import["https://i.sstatic.net/9sw8y.png"];
unique = DeleteDuplicates@Flatten[ImageData[origImg], 1];
rules = #[[1]] -> #[[2]] & /@ Transpose[{unique, Range@Length@unique}];
img = ImageData[origImg] /. Dispatch@rules;
img // Colorize
(* Recalculating morphological components *)
masks = ComponentMeasurements[img, "Mask"];
p = Max@Drop[Values@ComponentMeasurements[img, "Fragmentation"], 1] + 1;
tempComponents =
ParallelSum[(# + p i Unitize[#] #) &@
MorphologicalComponents[i /. masks], {i, 2, Length@masks}];
tempComponents // MinMax
(* {0, 2262} *)
(* Reindexing components *)
uniqueNew = DeleteDuplicates@Flatten[tempComponents, 1];
rulesNew = #[[1]] -> #[[2]] & /@
Transpose[{uniqueNew, Range@Length@uniqueNew}];
newComponents = tempComponents /. Dispatch@rulesNew;
newComponents // MinMax
(* {1, 562} *)
newComponents // Colorize
We can now easily get all the measures by ComponentMeasurements[]
:
ComponentMeasurements[newComponents, "Area"]
(* {1->493734., 2->348.5, 3->693.125, 4->661.75,
<<554>>, 559->449.5, 560->421.375, 561->341.125, 562->332.875} *)