View Full Version : Getting area from a raster image using your image editor.
06-17-2008, 02:27 PM
Born of this thread...
about a free way to get the area of a part of a raster image - such as an island. here is a (and not necessarily the best) way of doing it.
If you have a map of a known area or you can chop out a rectangle of known area that covers an irregular shape in the middle then you can use this method.
What you will need is a black and white image of the bit of map you want to measure. You can usually flood fill or use some contrast / brightness foo to get you that image but if not then you will have to trace it - maybe in a new layer.
So, to start with I have my Modron island chain. Naturally I chose this because we can empirically calculate the area to check the result. The image is 1024x1024 and the circles have radii of 200, 100, & 50 pixels.
If the image was not square then it does not matter, you can resample it to 1024x1024. It makes no difference - its the black to white ratio thats required.
06-17-2008, 02:34 PM
Ok, so with three circles we know that this area in pixels should be:
200*200*3.1416 = 125664
100*100*3.1416 = 31416
50*50*3.1416 = 7854
164934 / (1024 * 1024) * 100 = 15.73% of area.
So this image is 15.73 percent white and knowing the original total map area we can deduce the area of the circles as Map Area * 0.1573.
Right, so lets see how we can do this with an image editor.
The idea is to find the average color of the image and divide it by 255 which should eventually give the 0.1573 value (give or take).
To do this you need to run a box filter over it. You can do this easily with PSP and I am sure Gimp / PS has the same capability. If not then ImageMagick has it and I will post the scripts to do it.
Taking that start image. Goto Effects / Geometric Effects / Pixellate and set the pixel size to 4. Then resample the image and set the scale to 25% BUT you must use nearest neighbor filtering. In PSP that is a Resize with resize type to "Pixel resize". You should get this image.
06-17-2008, 02:40 PM
You can keep doing that until you get to a single pixel. For simplicity here are some in between images and the final one boosted in size. Now that you have the final image you can get the color of it which you will see is a value of 40.
Now 40/255 = 0.1568 which is pretty close to the expected value. So take that value, multiply by whole map area and you have island area. Job done.
Here is a script to generate that process in ImageMagick.
convert start.png -filter box -resize 512x512 -sample 512x512 img512.png
convert img512.png -filter box -resize 256x256 -sample 512x512 img256.png
convert img256.png -filter box -resize 128x128 -sample 512x512 img128.png
convert img128.png -filter box -resize 64x64 -sample 512x512 img64.png
convert img64.png -filter box -resize 32x32 -sample 512x512 img32.png
convert img32.png -filter box -resize 16x16 -sample 512x512 img16.png
convert img16.png -filter box -resize 8x8 -sample 512x512 img8.png
convert img8.png -filter box -resize 4x4 -sample 512x512 img4.png
convert img4.png -filter box -resize 2x2 -sample 512x512 img2.png
convert img2.png -filter box -resize 1x1 -sample 512x512 img1.png
06-17-2008, 04:10 PM
Excellent!! If you happen to be using RobA's GIMP method for making your map, you'll have a Land Mask layer that's nothing but black and white that's perfect for this.
I just tried this on my world map and it seems to have worked well. Turns out I have approximately 32% land, 68% water.
06-17-2008, 06:11 PM
That is brilliant, redrobes!
The histogram in gimp gives the mean value of the selected channel (or the value):
In the case of your example image, 40.4/255 = 15.8%
No resizing required!
06-17-2008, 07:25 PM
Well, would ya know it, it does in PSP too ! So there, thats my tutorial over with. The bleedin' image editors have it built in. Heh - no extra tut widget for me then ;( LOL. Still, good to know that - I never use the histogram facilities.
06-17-2008, 08:20 PM
Right then to sum up - this tutorial is a complete waste of time :)
In Gimp you get the histogram window open for the main image and drag a histogram selection from the middle of it to the white end and it reads the 15.8% of white directly.
In PSP you open the histogram window and move your cursor to the mid point and it will read off the amount higher than this which is the same at 15.8%.
So take the whole image area and take 15.8% of it and thats the island area.
I certainly didn't know all of the histogram info before but I think we can let this tutorial quietly roll off the bottom now... :D
06-17-2008, 08:52 PM
Thanks for the effort redrobes. I appreciate it.
I'm amazed that my little question got such response and caused so many discoveries about our favourite software(s)
In my heart you'll always have an extra widget in my books :).
Powered by vBulletin® Version 4.2.3 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.