Coloring a map of India

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite
1












Below is a map of India (img).



enter image description here



and I can visualize it in the following way:



imgbin = Binarize@img;
MorphologicalComponents[imgbin] // Colorize


and the result is:



enter image description here



Edit



Following a manual approach, I can isolate the small components:



sc = SelectComponents[imgbin, #Count < 60 &];


However, this gives me



enter image description here



Now I find the coordinates of the centroids:



centroids = ComponentMeasurements[sc, "Centroid"];


This gives me




1 -> 523.425, 182.274, 2 -> 545.125, 182.563, 3 -> 521.089, 154.518, 
4 -> 96.2778, 154.722, 5 -> 101.5, 150.625, 6 -> 285.375, 146.667,
7 -> 111.7, 136., 8 -> 517.868, 133.947, 9 -> 91.8846, 134.885,
10 -> 99.3889, 135.167, 11 -> 95.0882, 122.382, 12 -> 531.136, 105.409,
13 -> 535.5, 94., 14 -> 541.4, 92.6, 15 -> 551.833, 93.8333,
16 -> 540.333, 86.5833, 17 -> 564., 63.5, 18 -> 557.929, 61.5,
19 -> 558.833, 55.8333, 20 -> 567.5, 27., 21 -> 575.689, 20.3491



From this information, I think, the grouping can be done. I need to choose two thresholds for the X coordinates of the centroids. Manually, I can set the thresholds as 100 and 200. Then, the components having centroids with x < 100 will be grouped into one, the component having the centroid with 100 < x < 200 will be grouped into another one (this component should be discarded as it is already considered as a component earlier) and finally the components having centroids with x > 200 will be grouped into the second one.




However, I wish to group some of the components as shown below:



enter image description here



That is, the components inside the circle at the bottom left should be grouped into a single component and visualized with a single color and the components inside the ellipse at the bottom right should be grouped into another single component and visualized with another unique color while keeping the rest of the map unchanged.



How can I do so?



Edit 2: I can now group the components desired in a very manual way as follows:



scleft = SelectComponents[
imgbin, #Count < 80 && #Centroid[[1]] < 130 &];
scright =
SelectComponents[imgbin, #Count < 80 && #Centroid[[1]] > 350 &];


and the components for the rest of the parts



mc = MorphologicalComponents[
ImageSubtract[imgbin, ImageAdd[scleft, scright]]];


Now how can I join these components - scleft, scright and mc and get the desired result?










share|improve this question























  • What have you tried so far ? Understand your original attempts will help us get started in helping you.
    – user6014
    3 hours ago










  • @user6014 I am not sure. Maybe group them by the components' centroid coordinates.
    – Majis
    3 hours ago










  • If you post code examples of your best attempt so far it will be a good starting point for someone to build off of and hopefully get you to the next step.
    – user6014
    3 hours ago










  • @user6014 see my edit.
    – Majis
    3 hours ago






  • 1




    Majis, may i suggest you re-visit your previous questions and check if any of the answers is worth accepting?
    – kglr
    1 hour ago














up vote
4
down vote

favorite
1












Below is a map of India (img).



enter image description here



and I can visualize it in the following way:



imgbin = Binarize@img;
MorphologicalComponents[imgbin] // Colorize


and the result is:



enter image description here



Edit



Following a manual approach, I can isolate the small components:



sc = SelectComponents[imgbin, #Count < 60 &];


However, this gives me



enter image description here



Now I find the coordinates of the centroids:



centroids = ComponentMeasurements[sc, "Centroid"];


This gives me




1 -> 523.425, 182.274, 2 -> 545.125, 182.563, 3 -> 521.089, 154.518, 
4 -> 96.2778, 154.722, 5 -> 101.5, 150.625, 6 -> 285.375, 146.667,
7 -> 111.7, 136., 8 -> 517.868, 133.947, 9 -> 91.8846, 134.885,
10 -> 99.3889, 135.167, 11 -> 95.0882, 122.382, 12 -> 531.136, 105.409,
13 -> 535.5, 94., 14 -> 541.4, 92.6, 15 -> 551.833, 93.8333,
16 -> 540.333, 86.5833, 17 -> 564., 63.5, 18 -> 557.929, 61.5,
19 -> 558.833, 55.8333, 20 -> 567.5, 27., 21 -> 575.689, 20.3491



From this information, I think, the grouping can be done. I need to choose two thresholds for the X coordinates of the centroids. Manually, I can set the thresholds as 100 and 200. Then, the components having centroids with x < 100 will be grouped into one, the component having the centroid with 100 < x < 200 will be grouped into another one (this component should be discarded as it is already considered as a component earlier) and finally the components having centroids with x > 200 will be grouped into the second one.




However, I wish to group some of the components as shown below:



enter image description here



That is, the components inside the circle at the bottom left should be grouped into a single component and visualized with a single color and the components inside the ellipse at the bottom right should be grouped into another single component and visualized with another unique color while keeping the rest of the map unchanged.



How can I do so?



Edit 2: I can now group the components desired in a very manual way as follows:



scleft = SelectComponents[
imgbin, #Count < 80 && #Centroid[[1]] < 130 &];
scright =
SelectComponents[imgbin, #Count < 80 && #Centroid[[1]] > 350 &];


and the components for the rest of the parts



mc = MorphologicalComponents[
ImageSubtract[imgbin, ImageAdd[scleft, scright]]];


Now how can I join these components - scleft, scright and mc and get the desired result?










share|improve this question























  • What have you tried so far ? Understand your original attempts will help us get started in helping you.
    – user6014
    3 hours ago










  • @user6014 I am not sure. Maybe group them by the components' centroid coordinates.
    – Majis
    3 hours ago










  • If you post code examples of your best attempt so far it will be a good starting point for someone to build off of and hopefully get you to the next step.
    – user6014
    3 hours ago










  • @user6014 see my edit.
    – Majis
    3 hours ago






  • 1




    Majis, may i suggest you re-visit your previous questions and check if any of the answers is worth accepting?
    – kglr
    1 hour ago












up vote
4
down vote

favorite
1









up vote
4
down vote

favorite
1






1





Below is a map of India (img).



enter image description here



and I can visualize it in the following way:



imgbin = Binarize@img;
MorphologicalComponents[imgbin] // Colorize


and the result is:



enter image description here



Edit



Following a manual approach, I can isolate the small components:



sc = SelectComponents[imgbin, #Count < 60 &];


However, this gives me



enter image description here



Now I find the coordinates of the centroids:



centroids = ComponentMeasurements[sc, "Centroid"];


This gives me




1 -> 523.425, 182.274, 2 -> 545.125, 182.563, 3 -> 521.089, 154.518, 
4 -> 96.2778, 154.722, 5 -> 101.5, 150.625, 6 -> 285.375, 146.667,
7 -> 111.7, 136., 8 -> 517.868, 133.947, 9 -> 91.8846, 134.885,
10 -> 99.3889, 135.167, 11 -> 95.0882, 122.382, 12 -> 531.136, 105.409,
13 -> 535.5, 94., 14 -> 541.4, 92.6, 15 -> 551.833, 93.8333,
16 -> 540.333, 86.5833, 17 -> 564., 63.5, 18 -> 557.929, 61.5,
19 -> 558.833, 55.8333, 20 -> 567.5, 27., 21 -> 575.689, 20.3491



From this information, I think, the grouping can be done. I need to choose two thresholds for the X coordinates of the centroids. Manually, I can set the thresholds as 100 and 200. Then, the components having centroids with x < 100 will be grouped into one, the component having the centroid with 100 < x < 200 will be grouped into another one (this component should be discarded as it is already considered as a component earlier) and finally the components having centroids with x > 200 will be grouped into the second one.




However, I wish to group some of the components as shown below:



enter image description here



That is, the components inside the circle at the bottom left should be grouped into a single component and visualized with a single color and the components inside the ellipse at the bottom right should be grouped into another single component and visualized with another unique color while keeping the rest of the map unchanged.



How can I do so?



Edit 2: I can now group the components desired in a very manual way as follows:



scleft = SelectComponents[
imgbin, #Count < 80 && #Centroid[[1]] < 130 &];
scright =
SelectComponents[imgbin, #Count < 80 && #Centroid[[1]] > 350 &];


and the components for the rest of the parts



mc = MorphologicalComponents[
ImageSubtract[imgbin, ImageAdd[scleft, scright]]];


Now how can I join these components - scleft, scright and mc and get the desired result?










share|improve this question















Below is a map of India (img).



enter image description here



and I can visualize it in the following way:



imgbin = Binarize@img;
MorphologicalComponents[imgbin] // Colorize


and the result is:



enter image description here



Edit



Following a manual approach, I can isolate the small components:



sc = SelectComponents[imgbin, #Count < 60 &];


However, this gives me



enter image description here



Now I find the coordinates of the centroids:



centroids = ComponentMeasurements[sc, "Centroid"];


This gives me




1 -> 523.425, 182.274, 2 -> 545.125, 182.563, 3 -> 521.089, 154.518, 
4 -> 96.2778, 154.722, 5 -> 101.5, 150.625, 6 -> 285.375, 146.667,
7 -> 111.7, 136., 8 -> 517.868, 133.947, 9 -> 91.8846, 134.885,
10 -> 99.3889, 135.167, 11 -> 95.0882, 122.382, 12 -> 531.136, 105.409,
13 -> 535.5, 94., 14 -> 541.4, 92.6, 15 -> 551.833, 93.8333,
16 -> 540.333, 86.5833, 17 -> 564., 63.5, 18 -> 557.929, 61.5,
19 -> 558.833, 55.8333, 20 -> 567.5, 27., 21 -> 575.689, 20.3491



From this information, I think, the grouping can be done. I need to choose two thresholds for the X coordinates of the centroids. Manually, I can set the thresholds as 100 and 200. Then, the components having centroids with x < 100 will be grouped into one, the component having the centroid with 100 < x < 200 will be grouped into another one (this component should be discarded as it is already considered as a component earlier) and finally the components having centroids with x > 200 will be grouped into the second one.




However, I wish to group some of the components as shown below:



enter image description here



That is, the components inside the circle at the bottom left should be grouped into a single component and visualized with a single color and the components inside the ellipse at the bottom right should be grouped into another single component and visualized with another unique color while keeping the rest of the map unchanged.



How can I do so?



Edit 2: I can now group the components desired in a very manual way as follows:



scleft = SelectComponents[
imgbin, #Count < 80 && #Centroid[[1]] < 130 &];
scright =
SelectComponents[imgbin, #Count < 80 && #Centroid[[1]] > 350 &];


and the components for the rest of the parts



mc = MorphologicalComponents[
ImageSubtract[imgbin, ImageAdd[scleft, scright]]];


Now how can I join these components - scleft, scright and mc and get the desired result?







image-processing visualization geography






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 47 mins ago

























asked 3 hours ago









Majis

1,333313




1,333313











  • What have you tried so far ? Understand your original attempts will help us get started in helping you.
    – user6014
    3 hours ago










  • @user6014 I am not sure. Maybe group them by the components' centroid coordinates.
    – Majis
    3 hours ago










  • If you post code examples of your best attempt so far it will be a good starting point for someone to build off of and hopefully get you to the next step.
    – user6014
    3 hours ago










  • @user6014 see my edit.
    – Majis
    3 hours ago






  • 1




    Majis, may i suggest you re-visit your previous questions and check if any of the answers is worth accepting?
    – kglr
    1 hour ago
















  • What have you tried so far ? Understand your original attempts will help us get started in helping you.
    – user6014
    3 hours ago










  • @user6014 I am not sure. Maybe group them by the components' centroid coordinates.
    – Majis
    3 hours ago










  • If you post code examples of your best attempt so far it will be a good starting point for someone to build off of and hopefully get you to the next step.
    – user6014
    3 hours ago










  • @user6014 see my edit.
    – Majis
    3 hours ago






  • 1




    Majis, may i suggest you re-visit your previous questions and check if any of the answers is worth accepting?
    – kglr
    1 hour ago















What have you tried so far ? Understand your original attempts will help us get started in helping you.
– user6014
3 hours ago




What have you tried so far ? Understand your original attempts will help us get started in helping you.
– user6014
3 hours ago












@user6014 I am not sure. Maybe group them by the components' centroid coordinates.
– Majis
3 hours ago




@user6014 I am not sure. Maybe group them by the components' centroid coordinates.
– Majis
3 hours ago












If you post code examples of your best attempt so far it will be a good starting point for someone to build off of and hopefully get you to the next step.
– user6014
3 hours ago




If you post code examples of your best attempt so far it will be a good starting point for someone to build off of and hopefully get you to the next step.
– user6014
3 hours ago












@user6014 see my edit.
– Majis
3 hours ago




@user6014 see my edit.
– Majis
3 hours ago




1




1




Majis, may i suggest you re-visit your previous questions and check if any of the answers is worth accepting?
– kglr
1 hour ago




Majis, may i suggest you re-visit your previous questions and check if any of the answers is worth accepting?
– kglr
1 hour ago










2 Answers
2






active

oldest

votes

















up vote
5
down vote













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])]


enter image description here



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])]


enter image description here



Note: Need further refinement to exclude the red elements on the continent.






share|improve this answer






















  • see my edit to the question.
    – Majis
    46 mins ago










  • @Majis, please see the update.
    – kglr
    26 mins ago

















up vote
4
down vote













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]] +
ColorReplace[chain1, White -> Green] +
ColorReplace[chain2, White -> Red]


enter image description here






share|improve this answer




















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "387"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f183689%2fcoloring-a-map-of-india%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    5
    down vote













    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])]


    enter image description here



    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])]


    enter image description here



    Note: Need further refinement to exclude the red elements on the continent.






    share|improve this answer






















    • see my edit to the question.
      – Majis
      46 mins ago










    • @Majis, please see the update.
      – kglr
      26 mins ago














    up vote
    5
    down vote













    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])]


    enter image description here



    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])]


    enter image description here



    Note: Need further refinement to exclude the red elements on the continent.






    share|improve this answer






















    • see my edit to the question.
      – Majis
      46 mins ago










    • @Majis, please see the update.
      – kglr
      26 mins ago












    up vote
    5
    down vote










    up vote
    5
    down vote









    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])]


    enter image description here



    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])]


    enter image description here



    Note: Need further refinement to exclude the red elements on the continent.






    share|improve this answer














    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])]


    enter image description here



    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])]


    enter image description here



    Note: Need further refinement to exclude the red elements on the continent.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 31 mins ago

























    answered 2 hours ago









    kglr

    165k8188388




    165k8188388











    • see my edit to the question.
      – Majis
      46 mins ago










    • @Majis, please see the update.
      – kglr
      26 mins ago
















    • see my edit to the question.
      – Majis
      46 mins ago










    • @Majis, please see the update.
      – kglr
      26 mins ago















    see my edit to the question.
    – Majis
    46 mins ago




    see my edit to the question.
    – Majis
    46 mins ago












    @Majis, please see the update.
    – kglr
    26 mins ago




    @Majis, please see the update.
    – kglr
    26 mins ago










    up vote
    4
    down vote













    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]] +
    ColorReplace[chain1, White -> Green] +
    ColorReplace[chain2, White -> Red]


    enter image description here






    share|improve this answer
























      up vote
      4
      down vote













      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]] +
      ColorReplace[chain1, White -> Green] +
      ColorReplace[chain2, White -> Red]


      enter image description here






      share|improve this answer






















        up vote
        4
        down vote










        up vote
        4
        down vote









        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]] +
        ColorReplace[chain1, White -> Green] +
        ColorReplace[chain2, White -> Red]


        enter image description here






        share|improve this answer












        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]] +
        ColorReplace[chain1, White -> Green] +
        ColorReplace[chain2, White -> Red]


        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 25 mins ago









        Chip Hurst

        19.3k15485




        19.3k15485



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f183689%2fcoloring-a-map-of-india%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            Installing NextGIS Connect into QGIS 3?

            One-line joke