Coloring a map of India
Another idea is to dilate the image slightly to find 'connected' components.
img = Import["https://i.stack.imgur.com/6wQT9.jpg"];
mask = Binarize[Dilation[img, DiskMatrix[15]]];
comps = MorphologicalComponents[mask];
{chain2, chain1, mainland} = SortBy[Table[
Binarize[img*Image[1 - Unitize[comps - i]]], {i, 3}], Total];
Colorize[MorphologicalComponents[mainland, CornerNeighbors -> False]] +
ImageMultiply[chain1, Green] +
ImageMultiply[chain2, Red]
Edit
To color the island chains from within Colorize
:
mcomp = MorphologicalComponents[mainland, CornerNeighbors -> False];
max = Max[mcomp];
Colorize[mcomp + (max + 1)ImageData[chain1] + (max + 2)ImageData[chain2]]
Update 2: have all the components together and then visualize using simply Colorize:
Modify the MorphologicalComponents
matrix to assign the same label to members of a cluster:
replace = Thread[Alternatives @@ # -> First[#]] & /@ clusters;
MorphologicalComponents[imgbin] /. replace // Colorize
Update:
clusters = FindClusters[#[[2, 1]] -> # & /@
ComponentMeasurements[MorphologicalComponents[imgbin],
{"Centroid", "Count"}, #2 < 80 && (#[[1]] < 130 || #[[1]] > 350) &], 2][[All, All, 1]];
Colorize[MorphologicalComponents[imgbin],
ColorRules -> Join @@ (Thread /@ Thread[clusters -> {Red, Green}])]
Original answer:
clusters = FindClusters[#[[2, 1]] -> # & /@
ComponentMeasurements[imgbin, {"Centroid", "Count"}, #2 < 65 &], 2][[All, All, 1]];
Colorize[MorphologicalComponents[imgbin],
ColorRules -> Join @@ (Thread /@ Thread[clusters -> {Red, Green}])]
Note: Need further refinement to exclude the red elements on the continent.
A manual way (since OP has suggested that this question is not about clustering, more about how to deal with groups of labels) to do this would be to right click on the image, then get indices. Select the upper left corner and the bottom right corner of the bounding box of a group of components and then use ctrl+c to copy the indices. Paste it into the notebook. Do this for both groups. Then rearrange the indices for use in Part
, in the following way:
img = Import["https://i.stack.imgur.com/6wQT9.jpg"];
comps = MorphologicalComponents[Binarize[img]];
comps[[492 ;; 549, 76 ;; 125]] = comps[[492 ;; 549, 76 ;; 125]] /. Except[0, _Integer] -> 1000;
comps[[459 ;; 648, 498 ;; 593]] = comps[[459 ;; 648, 498 ;; 593]] /. Except[0, _Integer] -> 1001;
comps = ArrayComponents[comps];
Colorize[comps]
This code works also without ArrayComponents
. What ArrayComponents
does is that it reindexes the components so that they are consecutive integers rather than the 1000
and 1001
that we arbitrarily used in the code.
comps // Flatten // DeleteDuplicates
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, \ 19, 20, 21, 22, 23, 24, 25, 26, 27}
If I were to finish what OP started, I'd do it similarly:
binimg = Binarize[img];
scleft = SelectComponents[binimg, #Count < 80 && #Centroid[[1]] < 130 &];
scright = SelectComponents[binimg, #Count < 80 && #Centroid[[1]] > 350 &];
mc = MorphologicalComponents[ImageSubtract[binimg, ImageAdd[scleft, scright]]];
mc += 1000 ImageData[scleft];
mc += 1001 ImageData[scright];
Colorize[
mc,
ColorRules -> Join[
Table[i -> Green, {i, 25}], {
1000 -> Blue,
1001 -> Red
}]
]