Reputation: 2399
Here's my problem - I have a map of the world or some sort of region, like this:
I need to generate a "border points" table for this map of a region in order to generate imagemaps and dynamically highlight certain areas. All of the maps' regions will have borders of one color to define them (in the example image, white).
So far, I'm thinking of some sort of flood-fill based method - note that speed and efficiency are not that important, as the script is in no way intended to be used in real time.
Is there a better way to do this that I don't know of? Is my approach fundamentally wrong? Any suggestions would be appreciated!
Upvotes: 3
Views: 2552
Reputation: 2038
If the regions are completely isolated one from each other, looking at connected components would do the trick. In Mathematica it looks like:
First create a binary image from the world map:
regions = ColorNegate[Binarize[img, .9]]
Then compute the connected components:
components = MorphologicalComponents[regions, CornerNeighbors -> False];
Now you may extract properties for each of the components (masks, perimeters, etc.). Here I colorized each regions with a unique color:
Colorize[components]
To get the border of a given component, one can query for the binary mask of the component and then compute the perimeter. This gets all the masks:
masks = ComponentMeasurements[components, "Mask"];
As an example, get the border, or contour, of one region:
country = Image[masks[[708, 2]], "Bit"]
border = MorphologicalPerimeter[country]
Getting 2D positions for the border is just a matter of extracting the white pixels in the image:
pos = Position[ImageData[border], 1]
Upvotes: 4
Reputation: 5559
If possible; try to get the vector data behind your map from another source. I understand this doesn't answer your question, but for world borders (and many others) you can find them publicly on the internet (google for "world borders shapefile"). This will give you more precise data, allow you to zoom at any level, reproject your map, use google maps or other layers, etc. You can display the vector data nicely with libraries like openlayers etc, but then you're slowly moving towards more complex GIS stuff.
If all you really need is based on an image, your flood fill approach might work (if the borders are indeed completely closed).
Upvotes: 1