Wolfram Language

Open live version

Color a Map by Country

Color individual countries in the image of a map. This example appears in Stephen Wolframs video Introduction to the Wolfram Language.


code

MorphologicalComponents[\!\(\* GraphicsBox[ TagBox[RasterBox[CompressedData[" 1:eJztnF2O7LYRhdWBH7KNPGQBWYKzBGcFNpC8BnACBN597kx3qymyfk79kJS6 eQDbM62qYp2vKYmU7vVffvv3L//607Zt//nzj3/98uv/fv7991//+MdPP375 +x///edvX4f+dtu2v/74Z2lpaWlpidePG8XtodmtnE+3F5oFqdat1oL0UgPn IxkxJjk4HwmJNKoR+soocL27DnOj+WhNpC+x1hekXYL3xeghyfyC9C3Z/IL0 Jc09xui9Ien2QUpvjAlxD0J6V0yw/Q+mhLv/XEgG92Dc1y+zXeXKNEegsMfP 8PB9/WXINEWgONOCAI2bK8G1FqFDUu3DgXOVQqhkU+fAg3e36pbi2KJ7Dr0F BAcf6dygIZAY98Cwg2kwSoT0uPPzR/Wh0a3iGSAJ9OTmFZfqyPB6YzYjnh3U POqPOQqPMxNSANF3MuqROwIPNA9ShJDFZDR/IqMoog+AFEdUvn1z5qNPJKZA apA5TUbz0UcSExjlELoNgDR8Q1wMnYOoO6Sd0mhE6H07w2O4wLPx0YzizhJL QWvRUYxSnSWWwhbsQyAlO0sshTwonaSgM4vHcIFJRK4FacKp9hw16M3gMVxg HqQkSiMgDaDENJaE6b0hJWEaAqk7JaEx7nC6x3CBQZDYT4MeBz1PGsSIHz1m MkrpBJD0VsqonjaD2UMgyeNHXAYpzYekddJG9jQayp0Jae/Ab9PgNJA6jdHx CbLbp8FqIPMEkIKU/NmzIemNHA6EMLmT8by+kIQ+6tdwLp+h5MmQkD6qQxFM 1aBoMcOIPRkZIIWuLcehmy7ADsAhsiFJbbTHfJheWYcGAExTIWFtkM92zZQa FkdmSi4+SjomrA3mcZMJE8nhsAKTN0XwMLfsyQS2wT1vsp0ESmGxHL5322ud A5JhMrGB9Y1OeFZDt80M04eR/F0BbUnZkq06kg41O+rCyAkJoMQaJ0dlovkT nj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w9nFwUJQ/TRkMyabaG/FiREYUizDYxQFNLs/scoBml294OE7dE+ m1GlNZFoGbl8JKMApNmdD9RihGgxArQYAVqQEC1GgBYjQIsRosUI0IIEaDFC tBgBWowALUaIFiRAixGgRQjRAgRoAQK0+CBagBAtPpgWoKWlpaUll/4PK4+e hg== "], {{0, 305}, {293, 0}}, {0, 1}, ColorFunction->GrayLevel], BoxForm`ImageTag["Bit", ColorSpace -> Automatic, Interleaving -> None], Selectable->False], BaseStyle->"ImageGraphics", ImageSizeRaw->{293, 305}, PlotRange->{{0, 293}, {0, 305}}]\)] // Colorize

how it works

MorphologicalComponents gives an array in which each pixel of an image is replaced by an integer index representing the connected foreground image component in which the pixel lies. Colorize generates an image from the array, using colors for positive integers and black for non-positive integers.

MorphologicalComponents[\!\(\* GraphicsBox[ TagBox[RasterBox[CompressedData[" 1:eJztnF2O7LYRhdWBH7KNPGQBWYKzBGcFNpC8BnACBN597kx3qymyfk79kJS6 eQDbM62qYp2vKYmU7vVffvv3L//607Zt//nzj3/98uv/fv7991//+MdPP375 +x///edvX4f+dtu2v/74Z2lpaWlpidePG8XtodmtnE+3F5oFqdat1oL0UgPn IxkxJjk4HwmJNKoR+soocL27DnOj+WhNpC+x1hekXYL3xeghyfyC9C3Z/IL0 Jc09xui9Ien2QUpvjAlxD0J6V0yw/Q+mhLv/XEgG92Dc1y+zXeXKNEegsMfP 8PB9/WXINEWgONOCAI2bK8G1FqFDUu3DgXOVQqhkU+fAg3e36pbi2KJ7Dr0F BAcf6dygIZAY98Cwg2kwSoT0uPPzR/Wh0a3iGSAJ9OTmFZfqyPB6YzYjnh3U POqPOQqPMxNSANF3MuqROwIPNA9ShJDFZDR/IqMoog+AFEdUvn1z5qNPJKZA apA5TUbz0UcSExjlELoNgDR8Q1wMnYOoO6Sd0mhE6H07w2O4wLPx0YzizhJL QWvRUYxSnSWWwhbsQyAlO0sshTwonaSgM4vHcIFJRK4FacKp9hw16M3gMVxg HqQkSiMgDaDENJaE6b0hJW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Sz6peTkXomujvoZzUgLyTu4IKkHy3evLnG 7+g0Ijck+smA1ezVIdlvfkU55+g9nUYUgdS+IrG5pUqeUn5I7XNsq9urMBLW QhqjipJ5b3udieRYVTNBbw3JvkGrbVHxroG72owpDIkq4xm4k78c+SGVi8cF SWZEhHvG7e0zJC8k9rKNGv4ASNUqyeH4Qox8kOq8+PnW2WVUDkhN1oKkM4pf lPpaTJAVkvSYxOL4nSHRCQ7DbwxJOz994/ZzlyULJOV2ONtKP1GejZu6++ez jfSUDEmLfUGa0vwwCZC0yALSjM4HioK0lf9V9RU31cIAMZCIIyyjD4VEfC5N ow+EhJ9pbw9nFwUJQ/TRkMyabaG/FiREYUizDYxQFNLs/scoBml294OE7dE+ m1GlNZFoGbl8JKMApNmdD9RihGgxArQYAVqQEC1GgBYjQIsRosUI0IIEaDFC tBgBWowALUaIFiRAixGgRQjRAgRoAQK0+CBagBAtPpgWoKWlpaUll/4PK4+e hg== "], {{0, 305}, {293, 0}}, {0, 1}, ColorFunction->GrayLevel], BoxForm`ImageTag["Bit", ColorSpace -> Automatic, Interleaving -> None], Selectable->False], BaseStyle->"ImageGraphics", ImageSizeRaw->{293, 305}, PlotRange->{{0, 293}, {0, 305}}]\)] // Colorize