imageimage-processingscriptingimagemagickheightmap

How to divide a heightmap into several layers with ImageMagick?


Let's say I have got a grayscale heightmap that has colors ranging from A (rather dark) to B (rather white). The map is not normalized, so the lowest value isn't pitch black and the highest one probably also isn't fully white. Let's also say 0 means black and 1 means white. What I want to do is to divide the image into several seperate images based on height. Every image has a black background.

This would for example mean: Image 1 contains all the image content between 0 and 0.2, with anything above that being reduced to 0.2. Image 2 contains everything between 0.2 and 0.4, everything below 0.2 is black. Image 3 is the same between 0.4 and 0.6, and all this goes up to 1. You get the idea.

Do you know any way to automate this process with ImageMagick? If not, is there any other way I could do this?

Thanks


Solution

  • If you want to slice the image at different heights, then you can do that using a combination of -black-threshold and -white-threshold using percents rather than fractions in ImageMagick. The following will keep each range of gray levels and make the rest black. Black threshold keeps everything above a value and makes the rest black. White threshold keeps everything below a value and makes the rest white. So after the white threshold, we just change white to black.

    Unix syntax for IM 6. If using IM 7, change convert to magick

    Input: (Create a gradient image for demonstration)

    convert -size 256x256 gradient: grad.png
    

    enter image description here

    convert grad.png -write mpr:grad +delete \
    \( mpr:grad -white-threshold 20% -fill black -opaque white +write t1.png \) \
    \( mpr:grad -black-threshold 20% -white-threshold 40% -fill black -opaque white +write t2.png \) \
    \( mpr:grad -black-threshold 40% -white-threshold 60% -fill black -opaque white +write t3.png \) \
    \( mpr:grad -black-threshold 60% -white-threshold 80% -fill black -opaque white +write t4.png \) \
    \( mpr:grad -black-threshold 80% +write t5.png \) \
    null:
    

    Results:

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    ADDITION

    Based upon your comment, the code should be changed as follows:

    convert grad.png -write mpr:grad +delete \
    \( mpr:grad -white-threshold 20% -fill "gray(20%)" -opaque white +write t1.png \) \
    \( mpr:grad -black-threshold 20% -white-threshold 40% -fill "gray(40%)" -opaque white +write t2.png \) \
    \( mpr:grad -black-threshold 40% -white-threshold 60% -fill "gray(60%)" -opaque white +write t3.png \) \
    \( mpr:grad -black-threshold 60% -white-threshold 80% -fill "gray(80%)" -opaque white +write t4.png \) \
    \( mpr:grad -black-threshold 80% +write t5.png \) \
    null:
    

    Results:

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    enter image description here