Jump to content

Novel method on preparing a heightmap from topographic and bathymetric images using ImageJ.


viridis
 Share

Recommended Posts

Been trying to master the Heightmap import from the editor, found quite limited in input options as Palaxin's noted in the Height guide.
As I have some background on scientific imaging software I started playing around with elevation data here and there and found that there is need for more control on horizontal scaling (the map) related to height scaling. Aswell as (maybe) simpler way of preparing image data for heightmap import in the 0ad editor.

ImageJ is an open-source image processing and analysis software written in Java. It is a multi-purpose piece of software very well suited when working with (very) different types of images, specially for scientific analysis. It has the advantage of allowing the extension of software via macros (very simple scripting language for repeated processes) and plugins (Java coding), so anyone can share any piece of code for automating the preparation of geospatial data for the (great but currently limited) heightmap import in the 0ad editor.

One of the problems I found first is when I try to prepare a very small map (e.g. 128*128 pixels) having the height values from 0 to 255 (8-bit per pixel) the elevation (z-axis) has too much range whereas the map surface (x,y axis) has too little. Moreover, even if you don't have this problem, you may find in the need of controlling the z-axis scale independently from x,y (to obtain not too steep mountains or not too shallow cliffs, etc. depending on the case). This is precisely the problem I am going to address below.

Preparing your system

  1. Install ImageJ from its download website (I recommend using your distribution repository if you use GNU/Linux or the platform independent download for other system if you already have Java already installed as ~99,99% people does :rolleyes:, if it does not work refer to the documentation or try your platform download ).
  2. Download elevation and/or bathymetry data. For simplicity I've selected NASA Blue Marble image series  as with this data source you have elevation and bathymetry on the same scale so both maps fit perfectly. I'll use the C1 area that includes the Mediterranean Sea as can be seen here. And download both the topography and bathymetry. Remember using right-click/save-link-as... cause the images are huge and you don't want to crash your browser. Select in both the same areas. I've picked C1.

Basic map preparation

  1. Start ImageJ, you should see something similar to this:
    01-ImageJ.png.76974b4af524aa937deb167926372da7.png
    It's a tiny window for a great program don't let you misunderstand this: size in this case DO NOT matter.
  2. Open the elevation file, either using menu File/Open... or drag and drop.
  3. It is quite big, I strongly recommend you downsample (scale) it before continuing. So click menu Image/Scale... or just press Ctrl+E. I'll pick 128x128 px for showing you the problem with this kind of tiny sizes. I recommend "create a new window" option as I have experienced some weird problems if not. Just close the source image after finishing. If you believe your machine and patience can stand huge images skip scaling. If you prefer cropping than scaling try yourself, this is beyond the scope of this basic tutorial. Just try menu Plugins/Macros/Record... before cropping and guess how to repeat exactly the same crop in the bathymetry image. Further on this: write me or comment this and I will try to post and advanced tutorial on this.
    02-Scale.png.7af4ddc818db6a91833b4715e262810f.png
  4. Maybe you've seen an error in the log: <Out of Memory>. If so you will have to close ImageJ and start it from the command line with the memory option. In my GNU/Linux it is -x (I guess is the same in other systems, but maybe with whole path before the command in windows):
    imagej -x 2048

    As 768 MB is currently the default limit, and I have 6GB I guess 2GB will suffice (2048 MB).  Of course do not reserve too much memory or your system might crash (you can see the free available memory in your task manager or sort-of). Now you can repeat 2, 3 steps until you achieve your wanted image. Close the big one, you want to preserve your memory. If you want to check the available memory ImageJ ships with a tiny monitor: menu Plugins/Utilities/Monitor Memory. You'll see a new tiny window and if you click on it the program will force garbage to be collected and reduce the memory used. This is very handy, if you find the memory usage is growing too much just click the monitor. Also you can click the status bar of the main window and there will appear the program info including its memory usage.
    03-monitor-mem.png.081a10bcdb349fd8acf3e9a88cfd6e55.png

  5. Save your file. Either using menu File/Save... or hitting Ctrl+S. The default tiff file will suffice and preserve our original 8-bit pixel depth data.

  6. Repeat steps 2-5 with the bathymetry data. You should end up having two (not so big) images with the desired data.

  7. Now the magic comes. As I've mentioned above as NASA data uses the same scale and regions for all maps, now we have both elevation and bathymetry data from the very same region. Let's join that. We will use the image calculator. Think of it as a per-pixel calculator, so having 2 opened images of the same size from the same region you can combine it's data and finish having a bathymetry+elevation image. If you hover your mouse over any of the images you will see the value of the pixel under the mouse in the status bar, such as:

    x=23, y= 46, value = 125

    note that x,y coordinates might be expressed in pixels or in inches or any length measure. Remember the value is the 8-bit value (0-255) for each pixel. If you explore a bit, you'll note the elevation image store 0 in the sea level whereas in the bathymetry the earth (above water) level is stored as 255. This gives us the clue to guess the kind of calculation we should choose. Now click menu Process/Image Calculator:04-image-calculator.png.ce94f0e7d11247d93bce2de9838c30c6.png
    So if we add the two images we will have both bathymetry data (from 0 to 255) and elevation data (from 256 to 511) merged into a new image. Please, note that if you do not use 32-bit result you will obtain an 8-bit data with the result of data loss (because with 8 bits you can store up to 256 values). Create a new window is also recommended option always.

    bathymetry + elevation = result

    06-batymetry.png.51b6eb40be1fe44e5129ddd0d386f57c.png +  05-elevation.png.6f59e7dde2ae3d9609b69f757ab2e111.png = 5990c9051aecd_07-elevationbathymetry.png.824f2c12fe1c40fba44e9ed2444bdbfe.png

  8. Now, when you start thinking you have the perfect image to import on the 0ad editor, you save it (tiff will maintain all your precious data), then you just need to change the image type to RGB (menu Image/type/RGB color), then save it (menu File/Save as/BMP). Go to the 0ad editor import heightmap and get a horrid result:
    08-imported-result.thumb.jpg.bffd1c07abe42d00a961bffb84c2d9f2.jpg
    But this is not the end of the tutorial. Remember the z-axis (or height) scaling problem? Here you can see it. Let's address that.

  9. Ok, let's return to ImageJ and close anything but the result window (or reopen the tiff saved in 8). Click menu  Plugins/3D/3D Surface Plot)
    09-3D-surface-plot.thumb.jpg.8da1a45c4bb97eb9b699986e11829712.jpg
    There goes the result. The appearance is similar to the map generated by the editor, but you have now 3D superpowers. Have a look and play around: above you can change the visualization type (drawing as a mesh, dots, etc. and colors). At the right you can see Scale (zoom the visualization) and the most important z-Scale. Change the z-Scale value until you like the result and take note of the z-scale value (in my case 0.11).
    10-happy-with-result.thumb.jpg.45fd98d4323ba3975443bfe35cee35a1.jpg

  10. Return to the result window (elevation+bathymetry). Click menu Process/Math/Multiply... 
    11-multiply-z-scale.png.e5c0d17e48a9e1d7e4fa497abd741684.png
    Type your desired value, OK and  voilá! The image might have appear to become invisible, but the data just have changed (each pixel value has been multiplied by 0.11)

  11. You can save this tiff with another filename, such as "corrected" or -0.11 or as you want. Then repeat step 8 (type RGB save as BMP) and import it in the editor.

 

THIS IS INCOMPLETE. SORRY FOR THE INCONVENIENCE BUT I'LL TRY TO FINISH THE TOPIC WHENEVER POSSIBLE.

Edited by viridis
incomplete
  • Like 2
  • Thanks 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...