ImageMagick v6 Examples --
Displacement Maps

Index
ImageMagick Examples Preface and Index
Distortion and Displacement Lookup Maps
Absolute Distortion Lookup Maps
Relative Displacement Lookup Maps
2 Dimensional Displacement

Rather than distorting images according to some fixed mathematical principle, we instead use special mapping images to define a free form method of distorting an image.

This allows you for example to map a image to produce any position or effect, from preparpared complex maths, to linear morphing effects, to ripples, and lensing effects. It is can even go so far as to map images into folded curtains or onto 3 dimentional objects using an appropriate pre-prepared maps.


Distortion and Displacement Maps

While the various distortion operators described above work well, you are restricted to just the various distortions that have been provided, using specific mathematical equations.

However there are times where you want to design your own distortion in a more freeform and less mathematical way. For example to generate a more complex distortion, such a mapping an image into a particular shape, or with a particular complex lensing effect, that is easier to draw than to define mathematically. Sometimes you just want to be able to repeat your distortion over a large number of images and avoid having to re-calculate the distortion over and over.

The solution is to pre-calculate your distortion, and save it as special look-up table (LUT) in the form of a grayscale image.

That is for each output pixel, we look-up the LUT, then use that value to look-up the color from the source image. That is three steps are needed.
  1. Look-up up each destination pixel in the LUT
  2. Map the LUT value to the source image location (two methods)
  3. Look-up the color from the source image

This image can be quickly applied repeatedly very quickly over many different images, without needing to re-calculate it all the time. That is you do all your calculations into a set of look-up table images, applying all the heavy calculations, distortions and other image warping effects. Then you can apply the pre-calculated distortion maps to any number of images, as many times as you like, quickly and easilly.

As a image is used for the 'lookup table' of the distortion, you can create, or modify a the distortion map using a image editor, such as 'Gimp' or 'PhotoShop', giving you the freedom to do some really fancy and complex distortions.


There are two major ways of defining a distortion LUT.

One method is where the values (or colors) of the LUT represent the pixel coordinate to look-up from the source image. That set of values represent the absolute coordinates of the source pixel to place in this position on the destination image. See next section on Absolute Distortion Maps.

The other method seems harder, but is actually more versatile. In this case the LUT image represent coordinate offset, relative to the current position. Gray mean no offset; White a maximum positive offset, and Black a maximum negative offset. That is the values represent a displacement from from the current position to the lookup coordinate of the source image to use. See Displacement Maps (Relative Lookup Maps) below.

Both methods have advantages and disadvantages, as you will see.


Absolute Distortion Lookup Maps

Creating an Absolute Distortion LUT Map is the simpler of the two methods to both understand, create distortion LUT maps for, and to apply. However, as you will see, it has a very serious drawbacks making them less practical than a Relative Displacement Map.

Before proceeding you need to understand that distort maps, like just about all the distortion methods we have seen, are applied as a Reverse Pixel Mapping. That is we don't map a source pixel to a destination coordinate, but use it to lookup the source image from the destination image coordinates.

What this means is that for each pixel in the destination image, we look-up the color of the pixel from the source image, using the distortion method being applied. In this case the method is to look-up the source coordinate from the provided Look-up Table Image.

Unfortunately at this time IM can not directly handle such distortion maps. However we can use the FX, General DIY Operator to do do all three steps.

First lets start with some basic definitions, of what the LUT will mean.

Any 'black' pixel in the LUT image (value 0.0) will be thought of as the left-most pixel or '0' X coordinate of the source image, while anything that is 'white' in the LUT (value 1.0), is to be thought of as the right-most pixel (the width of the source image).

Note that this LUT will only look-up the X or horizontal position of color in the source image. It will not change the height or Y positions of colors.

So lets try this with a simple plain gray-scale horizontal gradient for the LUT.

  convert koala.gif \( -size 75x75 gradient: -rotate 90 \) \
          -fx 'p{v*w,j}'      distort_noop.gif
[IM Output]  + [IM Output] ==> [IM Output]

Note that this did not make any real changes in mapping the source image to the destination image. That is because the X coordinate that we looked up from the distortion map, was the same position we were looking up the color for.

However by simply flipping the gradient over the look-up of the pixels is also flipped, creating a mirror image. That is white is on the left and 'black' is on the right, and a horizontal gradient across the image.

  convert koala.gif \( -size 75x75 gradient: -rotate -90 \) \
          -fx 'p{v*w,j}'      distort_mirror_x.gif
[IM Output]  + [IM Output] ==> [IM Output]

If we take the original gradient and compress it using a contrast enhancement operator, we can get a much more useful distortion.

  convert -size 75x75 gradient: -rotate 90 \
          -sigmoidal-contrast 8,50%      map_compress.gif
  convert koala.gif  map_compress.gif -fx 'p{v*w,j}'  distort_compress.gif
[IM Output]  + [IM Output] ==> [IM Output]

Notice that the sides of the distortion is stretched while the center is compressed.

We can expand this to two dimensions by using 2 distortion maps, one to adjust the X coordinate, the other for the Y coordinate.

  convert map_compress.gif -rotate 90 map_compress_y.gif
  convert koala.gif  map_compress.gif map_compress_y.gif \
          -fx 'p{u[1]*w,u[2]*h}'   distort_compress_2D.gif
[IM Output]  + [IM Output] [IM Output] ==> [IM Output]

As you can see the above recreated a variation of the implosion method, though only by compressing the images along the X and Y axis (simultaneously), rather than radially as the Implode operator does.

The NO-OP Distortion Map revisited

Before we get any further I would like to just step back a moment a have another look at the the 'noop' example above. This actually will blur the image slightly, as the formula as I have outlined is actually not exactly correct.

Basically to do a perfect 'copy' of the original image, you need map a 'white' (or 1.0) value from the LUT to the exact right-most (or bottom-most) pixel. This pixel is located at width or height of the source image minus 1.

The following command shows a correct "-fx" absolute distortion LUT mapping formula, and a simple method of testing that 'no-op' distortion map for accuracy.

  convert -size 75x75 pattern:gray50 \( gradient: -rotate 90 \) gradient: \
          -fx 'p{u[1]*(w-1),u[2]*(h-1)}'    distort_fx_check.gif
[IM Output]

The image used in the test above is a gray scale 'checks' pattern that will highlight any failure in a no-op distortion, whether that distortion is caused by the mapping image, or inaccuracies of the LUT mapping formula. Either case will appear as a blurring of the pixels into a grey color either across the whole image, or in bands or spots. If the formula above is correct the exact same original checker board pattern (as shown) should be reproduced.

However it should be pointed out these slight inaccuracies are unimportant when you are actually applying a map with an actual distortion. As such you can usually ignore this minor adjustment in the LUT mapping, unless such inaccuracies can become important, such as if you have undistorted areas in the LUT map you are applying that you want to preserve as accurately as possible.

Problems with Distortion Maps

The two maps we generated can, as in the previous example, be thought of as two separate distortions, one in the X direction and the other in the Y direction. In fact this is quite a valid thing to do.

However you can also view the two maps as representing a single distortion of both X and Y absolute coordinates. This is how you must picture a distortion involving rotation. For example...

  convert -size 75x75 gradient: -background black -rotate 45 \
          -gravity center -crop 75x75+0+0 +repage  map_rot45_x.png
  convert map_rot45_x.png  -rotate 90              map_rot45_y.png
  convert koala.gif  map_rot45_x.png   map_rot45_y.png \
          -fx 'p{u[1]*w,u[2]*h}'      distort_rot45.gif
[IM Output]  + [IM Output]  + [IM Output] ==> [IM Output]

And we now have another way of rotating any image.

The biggest problem with this technique is that by creating our distortion maps using rotates, we introduced some oddly colored pixels along the sides of the diagonal edges. In the last example this caused some random pixels to be added in a line along the bottom right corner of the image.

These 'random' colors are anti-aliasing values that the rotate introduced to produce a 'better' image. However for distortion maps, anti-aliased edge pixels can cause a real problem.

Now we can try to better define the colors at the edges of the rotated LUT images. In this case we can generate a larger gradient image, then cropping the rotation down to the correct size.

  convert -size 100x20 xc:white xc:black -size 115x75 gradient: \
          +swap -append   -rotate 45 \
          -gravity center -crop 75x75+0+0 +repage   map_rot45b_x.png
  convert map_rot45b_x.png  -rotate 90              map_rot45b_y.png
  convert koala.gif  map_rot45b_x.png   map_rot45b_y.png \
          -fx 'p{u[1]*w,u[2]*h}'      distort_rot45_better.png
[IM Output]  + [IM Output]  + [IM Output] ==> [IM Output]

In this way all the pixels in the LUT are defined properly, including all the edges of the rotated LUT gradients.

However that is not the only problem. We have in the process fully defined all pixels, including the pixels in the corners of the rotated image. These pixels are technically undefined. They have no meaning to the final distorted image.

At this represents the biggest problem with using an LUT to specify the absolute coordinates to get from the source image. You have no way of specifying what IM should do in these undefined areas. You can't just specify an extra color as these are just a simple gray-scale image that uses the full range of values for the LUT.

Set a Undefined Pixel Border

There are a number of ways to solve this 'undefined' pixel problem. The first and probably the simplest is to add an extra pixel or border around the edge of the input image. Thus any Lookup that is clipped to the border of the image will recieved this 'undefined' color.

That is we enlarging the original image (and thus the Distortion LUT image as well) by adding a border of a suitable 'undefined' color.

  convert -size 100x20 xc:white xc:black -size 117x77 gradient: \
          +swap  -append -rotate 45 \
          -gravity center -crop 77x77+0+0 +repage  map_rot45f_x.png
  convert map_rot45f_x.png  -rotate 90             map_rot45f_y.png
  convert koala.gif -bordercolor white -border 1x1 \
          map_rot45f_x.png   map_rot45f_y.png  -fx 'p{u[1]*w,u[2]*h}' \
          distort_rot45_final.gif
[IM Output]  + [IM Output]  + [IM Output] ==> [IM Output]

This is similar to the use of Virtual Pixels or assigning a background color for a simple Image Rotation distortion.

So lets have a look at a particular example using this technique.

Hourglass Distortion Map

Now I wanted a one dimensional distortion map, that scaled each row of the image differently based on that rows height. Sort of producing a real carnival fun house mirror distortion that makes fat people look very thin. In other words a sort of hourglass distortion.

This is quite a complex LUT image, and after a lot of fiddling I came up with the following expression to generate the height variable, but horizontally linear gradient map.

  convert -size 100x100 xc:  -channel G \
          -fx 'sc=.15; (i/w-.5)/(1+sc*cos(j*pi*2/h)-sc)+.5' \
          -separate  map_hourglass.png
[IM Output]

When generating gray-scale gradients, you can make the -fx operator 3 times faster, simply by asking it to only generate one color channel only, such as the 'G' or green channel in the above example. This channel can then be Separated to form the required gray-scale image. This can represent a very large speed boost, especially when using a very complex "-fx" formula.

The 'sc' is the scaling factor for the hourglass (value ranges from 0 to 0.5), and allows you to adjust the magnitude of the distortion.

Now lets apply this map to the built-in "rose:" image.

However that the 100x100 pixel map does not match the 70x46 pixel image. This complicates things, as we will need to scale the current pixel in the source image by the appropriate amount to match the distortion map we give, to look-up the location of that pixels color.


  convert rose:   map_hourglass.png \
          -fx 'p{ v.p{i*v.w/w,j*v.h/h}*w,  j}'  distort_hourglass.png
[IM Output]

If you look at this carefully the pixels X coordinate 'i' is multiplied by the width of the distortion map image 'v.w', and divided by the original images width 'w', to produce 'i*v.w/w. The same thing happens for the pixels Y coordinate, 'j*v.h/h'. This re-scales the pixel coordinate in the destination image to match the distortion LUT image. The looked up coordinate is then scaled by multiplying the LUT value with the source images width, to become the X coordinate for the color look-up.

If you have both an X and a Y distortion map, then you will have to repeat the scaled look-up for the Y map.

Of course we have the same 'edge' distortions we saw previously, so lets add a some transparent pixels to the edge of the source image.

  convert rose: -matte -bordercolor none -border 1x0  map_hourglass.png \
          -channel RGBA   -fx 'p{ v.p{i*v.w/w,j*v.h/h}.g*w, j}' \
          -shave 1x0 distort_hourglass2.png
[IM Output]

Note the use of the "-channel" setting, as well as restricting the look-up to just the green ('v.p{}.g') channel of the distortion map. This is required when transforming images with transparency.

This distortion map could be made even better by using a non-linear gradient so the image remains rectangular, with just the middle parts pushed inward. Anyone like to give this a go? Mail Me

Set Undefined Pixels using a Mask

A more general way of solving the 'undefined pixel' problem is to define a map of what pixels are actually a valid defined result in the distortion. In other words a masking image.

For example...

  convert -size 75x75 xc:white -background black -rotate 45 \
          -gravity center -crop 75x75+0+0 +repage  map_rot45b_m.png
  convert distort_rot45_better.png map_rot45b_m.png \
          +matte -compose CopyOpacity -composite   distort_rot45_masked.png
[IM Output]  + [IM Output] ==> [IM Output]

Now we have three images involved with the distortion map, and the results is getting complex indeed. Of course in a typical situation you probably will not need to go that far, but for the general case you do.

Unified Distortion Image

You may however have noticed that all three maps are all grey scale images. This means it is quite reasonable to merge all the maps into a single distortion map image.

For example, lets map the 'X distortion map' to the 'red' channel, the 'Y map' to the 'green', and the mask to the 'alpha' or transparency channel, which makes it easier to handle.

  convert map_rot45b_x.png map_rot45b_y.png \( map_rot45b_m.png -negate \) \
          +matte -channel RGA -background black -combine  map_rot45u.png
[IM Output] [IM Output] [IM Output] ==> [IM Output]

The 'blue' channel in the Combined Channel Image is not defined, so takes its value from the current "-background" color, which I preset to 'black' or a value of zero in the above.

Now lets apply this unified distortion map to our koala image. This unfortunately requires two image processing steps, one to distort the image, and one to mask the result.

  convert koala.gif -matte   map_rot45u.png \
          \( -clone 0,1  -fx 'p{v.r*w,v.g*h}' \
             +clone -compose Dst_In -composite \) -delete 0,1 \
          distort_rot45_unified.png
[IM Output] [IM Output] ==> [IM Output]

There is still an unused channel (blue) in the 'unified distortion map' image. One logical use for it is as a means to add highlights and shadows to the distorted image. (See Overlay highlights).

So let use You can see this tecnique applied in the Spherical Distortion Map example below.

Spherical Distortion Map

In the previous Hourglass Distortion Map example, I generated a gradient which was horizontally scaled by a cosine curve. With a little more work you can generate a spherical shape instead...

  convert -size 100x100 xc:  -channel R \
          -fx 'yy=(j+.5)/h-.5; (i/w-.5)/(sqrt(1-4*yy^2))+.5' \
          -separate  +channel     sphere_lut.png
[IM Output]

Note however that the above is not strictly accurate. The compressed gradient remains a liner gradient, just compressed to fit within a circle. A more accurate representation would probbaly require the creation of a non-linear gradient.

Now this mapping also has some large areas which would be classed as invalid, so will need some type of masking to define what pixels will be valid and invalid in the final image. A simple circle will do in this case.

  convert -size 100x100 xc:black -fill white \
          -draw 'circle 49.5,49.5 49.5,0'    sphere_mask.png
[IM Output]

And to complete we also need a shading highlight, such as developed in Overlay Highlights, for use by Overlay or Hardlight composition...

  convert sphere_mask.png \
          \( +clone -blur 0x20 -shade 110x21.7 -contrast-stretch 0% \
             +sigmoidal-contrast 6x50% -fill grey50 -colorize 10%  \) \
          -composite sphere_overlay.png
[IM Output]

Remember the above shading will only matter within the bounds of the sphere object, so the fact that the shade overflows those bounds is not important. Actually if you like to try and come up with a better spherical shading, that produces a even better ball like image, would love to see it.

So lets apply all three images: X coordinate LUT, Overlay Shading, and Transparency Mask; to an actual image of the right size (for simplicity).

  convert lena_sm.jpg      sphere_lut.png    -fx 'p{ v*w, j }' \
          sphere_overlay.png   -compose HardLight  -composite \
          sphere_mask.png -alpha off -compose CopyOpacity -composite \
          sphere_lena.png
[IM Output] ==> [IM Output]

This particular example shows the most powerful aspect of a Absolute Distortion Map. You can define a gradient on any freeform object (not nessarially mathematically), so that any image can be mapped onto that object, whether it be curves, wrinkles, folds, etc. Simply put, once you have the object mapping worked out you can map any image onto its surface.

Then to make it more realistic looking, you can overlay a second mapping, to add highlights, shadows, edges, and other features.

Of course as all three images are grayscale, you can combine them into a single Unified Distortion Map image, for easy storage. In this case I'll make it a more spherical distortion by re-using the X coordinate distortion LUT for the Y coodinate as well.

  convert sphere_lut.png   \( +clone -transpose \) \
          sphere_overlay.png   \( sphere_mask.png -negate \) \
          -channel RGBA  -combine    spherical_unified.png
[IM Output]

It is a rather pretty map. But if trying to interprete it, remember that: the 'red' and 'green' channels are the X and Y coodinate LUT, 'blue' is the high light and shadow effects overlay, and the transparency channel holds the invalid pixel mask for the final image.

And here I apply the complete 2-dimensional unified LUT to another image...

  convert mandrill_grid_sm.jpg   spherical_unified.png  \
          \( +clone  -channel B -separate +channel \) \
          \( -clone 0,1    -fx 'p{ v.r*w, v.g*h }' \
             -clone 2   -compose HardLight -composite  \
             -clone 1  -alpha on  -compose DstIn -composite \
          \) -delete 0--2       spherical_mandrill.png
[IM Output] ==> [IM Output]

So now you can easily apply this distortion to lots of images. without needing all the original calculations and image processing used to generate the map.

Circular Arc Distortion Map

Just to show just what is really possible by using positional distortion maps, here is an absolute distortion LUT, similar to what is provided by the 'Arc' Distortion Method above.

Basically instead of calculating the coordinate mappings for each and every pixel in each and every image being distorted, we save those calculated coordinates into the two X and Y coordinate gray-scale LUT maps.

That is we pre-calculate the whole distortion into a simpler look-up table image, allowing it to be applied over, and over, and over, without needing further square roots or trigonometric functions.


  convert -pointsize 30 -font Candice label:Anthony -trim +repage \
          -gravity center -resize 95x95 -crop 100x100+0+0\! \
          -flatten text_image.jpg
  convert -size 100x100 xc: -channel G  -fx 'atan(j/(i+.5))*2/pi' \
          -separate   -flip -flop       map_p_angle.png
  convert -size 100x100 xc: -channel G  -fx '1-hypot(i,j)/(w*1.6)' \
          -separate   -transverse       map_p_radius.png
  convert text_image.jpg   map_p_angle.png map_p_radius.png \
              -fx 'p{u[1]*w,u[2]*h}'    distort_p_curved.jpg
[IM Output]
Color Source
 +
 
[IM Output]
Angle - X Map
 +
 
[IM Output]
Radius - Y Map
==>
 
[IM Output]
Curved Text

Of course generating that distortion map was difficult, but once it has been done once, using any means you like (even artistically using a image editor like "Gimp" ), you can then reuse it on a huge number of images.

Polar Distortion Map

Sometimes you may need the destination image to be defined by the distortion map, rather than the source image, just to make things work correctly.

For example, if we want to map some text into a circle (also known as a polar transform), you really need to be able to use an image that is about 3 to 4 times long than it is high (high aspect ratio) or the result will not be very readable.

To do that we place the distortion map images before the color source image, so that the first (X map) image will be used to set the size of the final result, rather than the input source image.

  convert -size 100x100 xc:  -channel G \
          -fx 'atan2(i-w/2,h/2-j)/pi/2 + .5' \
          -separate  map_p_angular.png
  convert -size 100x100 xc:  -channel G \
          -fx 'rr=hypot(i-w/2,j-h/2); (.5-rr/70)*1.2+.5' \
          -separate  map_p_radial.png
  convert -font Candice -gravity center -size 200x50 \
                                label:'Around  the  World'    text.jpg
  convert map_p_angular.png map_p_radial.png text.jpg \
                 -fx 'u[2].p{ u*u[2].w, v*u[2].h }' distort_p_circle.jpg
[IM Output]
Angular - X Map
 +
 
[IM Output]
Radial - Y Map
 +
 
[IM Output]
Color Source
==>
 
[IM Output]
Circled Text

Essentially the color source image can now be any size or aspect ratio, and things will be handled correctly, however you may need to adjust the generation of the distortion map to handle the source images aspect ratio correctly.

In generating the above maps, the value '70' controls the final size of the circle, along which the mid-line is placed. The '1.2' value on the other hand controls the vertical scaling of the image into the circle, allowing you to adjust the height of the distorted text.

Remember this "-fx" expression requires the distortion maps to be given first, and the color source to be given as the third (index 2) image. However this will also mean that any meta-data that is stored in the source image will also be lost.

The problem with this distortion map is that there is a very sharp disjunction of colors in the 'X map' (caused by a asymptote in the mathematics). This line must remain sharp when you do any color look-up, or resizing of the map, to produce a larger image. That is you will need to ensure that any resize or interpolated look-up of this map does not produce a grey look-up color along that asymptotic line.

If you do generate grey look-ups along this line, you will get a line of colored pixels (looked up from the middle of the image) in your final result.

Because of this it is recommend you always generate this distortion map at the size you need for the final image, and never use any scaling technique previously shown.

You can use this for other effects too, like a circular checkerboard...

  convert map_p_angular.png map_p_radial.png \
          -size 150x90 pattern:checkerboard \
          -fx 'u[2].p{ u*u[2].w, v*u[2].h }'   distort_check_circle.gif
[IM Output]

Try some of the other Built-In Patterns that IM provides with the above for other interesting effects.

The above clearly shows the limits of image distortions using "-fx". Near center of the image, the radial lines are becoming aliased as pixel merging of a large areas into a single pixel does not take place. On the other hand the edges of the image, particularly the corners, show an appropriate blurring the radial lines.

The cause is that "-fx" (and most older distortion methods) only so simple unscaled interpolated look-ups of the colors in the source image. This means that as the image scales smaller the source image pixels are not merged together to produce the correct color for the destination pixel.

This is not a problem for areas of enlargement (as in the corners) only of extreme compression (center). As such one solution is the use of Super Sampling, but all this does is postpone the problems to a higher level of compression.

The same asymptotic line in the distortion map also produces a sharp color change along that line in the above example. Compare that line with the other radial lines which get very fuzzy around the edge of the image due to interpolated look-up.

This may be a problem when generating a circular pattern with a tileable image (such as the above), and may require some special handling to avoid visible differences in that part of the image.

To avoid this it may be better to distort the top half of the image separately to the bottom half so as to avoid the asymptotic region.


Relative Lookup Displacement Maps

As you can see creating a Absolute Distortion Map is reasonably easy to create and use. However it has a serious problem when a distortion has 'undefined' regions, or areas where the distortion goes 'outside' the normal bounds of the source image.

A more serious problem is that you are always dealing with gradents, which define the absolute coordinates for the color lookup. No part of the mapping image is simple, or clean, or easy to modify or edit by hand. Special techniques and mathematics is needed in there creation and use. That that generally means there is very little in the way of 'artistic' development involved.

There is however another method of using a Lookup Table, to specify the coordinates in which to get the final color. By using a Relative Displacement Map.

Instead of the 'map' defining the exact coordinate in which to lookup each pixels color from the source image, it instead defines a offset or displacement relative to the current position.

Now an offset could be a positive or negative value, and a negative value requires a little trickiness to encode into a color value. So what they do is define 'pure gray' and being a 0 displacement of the coordinate (no change). They then make 'black' mean a maximum negative displacement, and 'white' to mean a maximum positive displacement.

This can be hard to describe so lets look at an example. First we create a test image to 'displace'.

  convert -font Candice -gravity center -size 150x50 \
                                           label:'Anthony'    label.jpg
[IM Output]

Now I'll use some 'magick' to create a 'pure gray' image which includes a some 'pure white' area and a 'pure black' area.

  echo "P2 5 1 255\n 127 0 127 255 127" |\
                convert - -scale 150x50\! +matte   displace_map.jpg
[IM Output]

Now to use this image as a 'displacement map' we get the 'gray value' from the displacement map, and add it either (or both) the X and Y coordinate. That is we 'displace the lookup by a relative amount from the current position.

The 'value' is handled in a special way, so that a 'pure-gray' will mean a zero displacement of the lookup point (just the Y coodinate in this case) but a 'maximum displacement' is used for a 'white' (positive) or 'black' (negative) value.

For example, lets apply the displacement map to our "label" image.

  convert label.jpg  displace_map.jpg  -virtual-pixel Gray \
          -fx 'dy=10*(2v-1); p{i,j+dy}'   displaced.jpg
[IM Output]

As you can see the various sections of the image looked like they 'moved' according to the color of the displacement map. A 'white' region will add the given 'displacement value' to the lookup point, so in that area each pixel looks up the source image '10' pixels 'south-ward' (positive Y direction). As a result it looks as if the source image moved upward.

Remember it is the lookup that is displaced, NOT the actual image itself, which is why it appeared to move upward or a negative direction for white.

A similar effect was also seen for the areas with a 'black' displacement. The source image appeared to move downward, because the lookup displacement was done in a negative direction. Think about it carefully.

You will also notice that the 'displaced lookup' can actually look beyond the normal image bounds allowing you to use a Virtual Pixel setting to control these out of bounds pixels. In the above I just requested that a gray pixel be returned.

The 'maximum displacement' value '10' in the above example is very important, and is the maximum relative distance any part of the source image appears to move, for a 'pure white' or 'pure black' displacement value in the mapping image. You can not displace the lookup, and thus the input image anything further than this value.

Other shades of gray between the maximum white or black values and the central no-displacement 50% gray value, will displace the lookup by an appropriate amount. As such a 25% gray value will displace the lookup by 1/2 the displacement value in the negative direction, while a 75% gray will displace by 1/2 that value in the positive direction.

This value is a key difference between a Absolute Distortion Map and Relative Displacement Map. You can increase or decrease the relative displacements, making the image more or less distorted, simply by changing the displacement value, without needing to change the displacement map at all.

Also as a 'zero-displacement' is just a solid 50% or pure gray, and not a complex gradient, you can start with a simple gray image, and artistically lighten or darken areas to generate the desired displacements. You can do this simply by drawing shapes or areas, rather then needing a complex and exact mathematical formula.

And finally as all the displacements are relative, wild values such as produced by edge-effects does not produce wild or random pixel colors. In fact as you will see smoothing or blurring Displacement Maps is actually a good thing as it removes the disjoint or discontinuous 'cutting' effect you can see in the above example.

In summery A displacement map are much more controllable, and artistic, , providing localized displacements without the need for complex and exacting mathematics, and are very forgiving with regard to mistakes, edge effects, or even displacement map blurring.

It is ideal for simple 'displacement' type distortions such as when generating effects such as water, waves, distorting mirrors, the bending of light, lens-like effects, or frosted or bubbles glass effects.

On the other hand highly mathematical distortions such as 'polar', rotational, and 'perspective' distortions, or other real-world 3-d type mappings, are not easily achieved. That is not to say it is impossible, as later we will show that you can in fact convert between the two styles of maps, just more difficult.

Composite Displacement Operator

In the last example the DIY FX Operator was used to do the displacement mapping. Naturally this is a very slow operator.

However the IM "composite" command does allowed the use of a special type of image composition method "-displace" to do Displacement Mapping in a faster way.

Here is how you you use it...

  composite {displacement_map}   {input_image} \
            -displace {X}x{Y}  {result}

Note the order. The 'displacement_map' is given before the destination image being modified.

The 'X' and 'Y' values define the direction and 'maximum displacement' that will be used for 'white' and 'black' colors in the given displacement map. You can define either one, or both values, so as to allow you to displace in any particular direction.

For example here is the same Y displacement example we has above, but this time displaced using the "composite" command.

  composite displace_map.jpg label.jpg  -virtual-pixel Gray \
            -displace 0x10  displaced_y.jpg
[IM Output]

If your image does not produce the same result, probably in this case no change from the input image, then you have older IM that dates from before version 6.2.8. Upgrade as soon as possible. See Displacement Map Bugs for details.

If you must use a IM version before this, always use the two map image form of the operator, supplying both a overlay (X displacement) image and a mask (Y displacement) image. See below for a 2 displacement command synopsis.

The "composite" "-displace" operator also allows you to position the overlay/mask displacement map images over a larger input or background image, according to "-geometry" "-gravity" settings, the areas not overlaid by the displacement map will be left, as is. However the displacement maps offsets can still reference areas outside the overlaid part of the image, duplicating them.

However due o a quirk in the way in which the "composite" "-tile" setting works, you may not get quite the results you expect. It is thus not a recommend setting to use with Displacement Mapping.

Simple Displacement Examples

A displacement map of raw areas of colors, without any smooth transition will generaly produce disjoint (discontinuous) displacements between the different areas in the resulting image, just as you saw above.

You can in fact produce a displacement map that 'fractures' as if you were looking into a cracked mirror, using this technique. For example see the Fractured Mirror below.

You can produce nicer and smoother results if the colors smoothly flowed from one area to another. For example, by bluring the displacement map you generate a wave like transtion between the displaced areas...

  convert displace_map.jpg  -blur 0x10   dismap_wave.jpg
  composite dismap_wave.jpg label.jpg -displace 0x10 displaced_wave_y.jpg
[IM Output]
[IM Output]

Rather than displace the image in a Y direction you can also use a map to displace the image in a X direction resulting in a sort of compression wave.

  composite dismap_wave.jpg label.jpg  -displace 10x0  displaced_wave_x.jpg
[IM Output]

By using the same displacement map for both X and Y directions, we can add both a compression wave as well as an amplitude wave.

  composite dismap_wave.jpg label.jpg -displace 10x10 displaced_wave_xy.jpg
[IM Output]

Note that the image is still displaced in a single direction, resulting in the above image being stretched on the downward slope, and squeezed together on the upward slope. That is the distortion is at a angle or 'vector' with some horizontal as well as vertical components.

You can see that this effect is remarkably like it is underwater, with the image being distorted by gental ripples on the water's surface.

A distortion map however can contain multiple copies of the original image, just as you can in a reflected or refracted images...

  echo "P2 3 1 255\n 255 127 0 " | convert - -scale 150x50\! dismap_copy.jpg
  composite dismap_copy.jpg label.jpg  -displace 66x0  displaced_copy.jpg
[IM Output]
[IM Output]

You can also create mirrored flips or flops of parts of the image, by using gradients. For example here you can use a linear displacement map, to copy pixels from one side of the image to the other.

  convert -size 50x150 gradient: -rotate -90  +matte  dismap_mirror.png
  composite dismap_mirror.png label.jpg  -displace 150x0  displaced_mirror.jpg
[IM Output]
[IM Output]

Can figure out how this displacement map works?   As a hint figure out what displacement the left most and the right most edges have, then see how the rest of the image fits into that.

However as you are again using a gradient image you loose the simplicity of displacement maps. As such mirrors are either better done using a direct Flip Operation on the image, or by using a Absolute Distortion Map instead.

Note that by flipping the gradient over, you shrink the image.

  convert -size 50x150 gradient: -rotate 90  +matte  dismap_shrink.png
  composite dismap_shrink.png label.jpg  -displace 150x0  displaced_shrink.jpg
[IM Output]
[IM Output]

The above also demonstrates a particular problem that displacement maps have.

When an area (or all) of an image gets compressed by more than 50%, you will start to generate Aliasing Effects. this is particularly noticable in the staircased 'aliased' edges that is clearly visible.

As previously discussed, one solution to this is to Super Sample the number of pixels being used to generate each output pixel. To do that we enlarge both the image and displacement map, then resize the resulting image back to its more normal size. This will allow more pixels to take part in the setting of a specific pixel in the result, and thus produce better image.

For example...

  convert dismap_shrink.png  label.jpg -resize 200% miff:- |\
    composite  -   -displace 400x0  miff:- |\
      convert  -   -resize 50%   displaced_resized.jpg
[IM Output]

A much better and smoother result.

Graphing a gradient

Directly resulting from the above examples was an idea that by using Y displacements of a simple line, you can generate a graph of the colors of a displacement map.

For example here I generate a mathematical sinc() function ( which is defined as 'sin(x)/x'), and graph that gradient by using it as a displacement map...

  convert -size 121x100 xc: -fx 'sin(i*24/w-12)/(i*24/w-12)/1.3+.2' \
                                                      gradient_sinc.gif
  convert -size 121x100 xc: -draw 'line 0,50 120,50'     graph_source.gif
  composite gradient_sinc.gif graph_source.gif \
                                       -displace 0x49   displace_graph.gif
[IM Output]  + [IM Output] ==> [IM Output]

As you can see it works, though I wouldn't like to use it for mathematical plots. Better to use a proper graphing package. This technique however is useful as a dirty method of plotting the intensities of a row or column of pixels in an image.

What it does do is show how large differences in displacements can easily produce a discontinuity or non-smooth result. Basically as only each individual pixel in the "graph source" is only looked at one at a time, without averaging, a large difference in the displaced lookup from one pixel to the next, can produce a large color change in the result.

The moral is that displacement work best not only with smooth displacement maps, but also with displacing images that contains large areas or shades of color. It does not work so well for sharp thin lines.

Of course you can improve things by again Super Sampling the distort map...

  convert  gradient_sinc.gif graph_source.gif -resize 400% miff:- |\
    composite  -   -displace 0x196  miff:- |\
      convert  -   -resize 25%   displace_graph_2.gif
[IM Output]

The result is a lot better, though not as good as what can be achieved using a graphing package. Still only ImageMagick was used in its creation.

Here is another version of the same graph, but this time using a solid color, which works a lot better than displacing a thin line.

  convert -size 121x50 xc:white xc:black -append \
          gradient_sinc.gif  +swap   -resize 400% miff:- |\
    composite  -   -displace 0x196  miff:- |\
      convert  -   -resize 25%   displace_graph_3.gif
[IM Output]

Area Displacement (Linear)

Lets try a more logical displacement problem. Moving a area of an image in a straight line from one location to another.

As we have seen a 'pure gray' image will not cause any displacement, while a 'white' color will cause a positive lookup displacement from the source image.

For example lets create such an image....

  convert -size 75x75 xc:gray50 -fill white \
          -draw 'circle 37,37 37,20'  dismap_spot.jpg
[IM Output]

Now when we apply this image the contents of the area marked should have a copy of whatever appears in the direction of the given displacement value. So lets try a displacement value of X+10 and Y+10 or '10x10'...

  composite dismap_spot.jpg koala.gif -displace 10x10  displace_spot.png
[IM Output]  + [IM Output] ==> [IM Output]

As you can see the contents of the marked area now contains a copy of the image that was +10,+10 pixels to the South-East. Basically a image of the koala's 'tail'. In other words, within the circle the image was displaced North-East, or -10,-10 pixels.

Remember the displacement is of the lookup, so the source image is shifted by a negative amount due to the Reversed Pixel Mapping. The image displaces in the reverse direction!

Note also that it is the image within the area marked that is moved. You are not displacing the image marked, but displacing the image INTO the area marked.

And finally not the sharp discontinuity at the edges of the circle. Areas inside the marked area is moved, while the areas outside remain exactly as it was.

These are the facts, so it is worth repeating.
Displacement is in the reverse to the displacement value.
Only the areas marked not gray will be displaced.
Sharp color changes produce sharp image discontinuities.

So lets try something more practical. Lets move the center between the nose and eyes of the koala, located at '32,22', to the center of our white (full positive displacement) circle at '37,37'. That needs a displacement value of '-5,-15' (remeber it is a reversed direction)...

  composite dismap_spot.jpg koala.gif -displace -5x-15  displace_head.png
[IM Output]

And there we have a nicely centered copy of the central part of the koalas head.

But the image is still 'disjoint', and using a negative value is not very nice. The solution is to use a black spot instead, but also to blur the edges of that spot. Also lets make it larger to encompass more of the koala's head. So here is out 'positive movement spot' image...

  convert -size 75x75 xc:gray50 -fill black \
          -draw 'circle 37,37 37,17'  -blur 0x5  dismap_area.jpg
[IM Output]

You do not want to blur the image too much or the center of the spot will nolonger be a flat black color. Alturnativally you could just Normalize, Reverse Level Adjust the image to ensure that the drawn area is black, and surrounding parts are perfect grays. You will see this done a lot in later examples.

Now lets repeat that last 'head' displaceing using our black 'fuzzy spot' displacement map.

  composite dismap_area.jpg koala.gif -displace 5x15  displace_area.png
[IM Output]

As you can see we nove move the image +5,+15 into the 'fuzzy' area, but this time the border of the area is smoother and connected to the rest of the image.

Of course the ears on the edge of the circle was distorted by the fuzzy edge, and the body of the koala compressed as well, but it is still a lot better than what we had before.

To prevent the 'tearing' of the image you see on the trailing side, or leaving copies of the displaced part, you want to expand that spot, or make a more complex gradient type of displacement image.

For example supose you want to move the koalas head from its starting position at '32,22', to the center of the image at '37,37', or a movement of +5,+15 pixels, but you want to adjust the whole image to this change, to give a much smoother effect.

To do this you will want the maximimum diplacement of black (a positive image displacement) at '37,37' and displacing by a value of +5,+15. but you also want the make sure the rest of the image remains intact by 'pinning' the corners at 50% gray. That is perfect for a Shepards Interpolated Sparse Gradient.

  convert -size 75x75 xc:  -sparse-color  Shepards \
          '37,37 black   0,0 gray50  74,74 gray50  0,74 gray50  74,0 gray50' \
          dismap_move.jpg
  composite dismap_move.jpg koala.gif -displace 5x15  displace_move.png
[IM Output]  + [IM Output] ==> [IM Output]

As you can see you get a larger area of displacement spread is spread over the whole image. The result is a much more smoothly chnaging image than the tighter 'spot' method used before.

This is actually exactly equivelent to the Shepards Distortion but only for a one moving control point. It is also the exact same method used in Fred Weinhaus script 'shapemorph', but with some animation.

In summery: For small localized displacements a 'blurred spot' displacements can be used. But for larger displacements over a longer distance, a larger smooth gradient displacement map should be used to prevent tearing or duplicating the source image.

Under Construction

Simple Displacement Morphing

Modifing the Size of Displacement Vectors
Two Image Morphing
Random 1D Displacements

Rippled Water Reflections

As mentioned before displacement maps are especially useful for generating water and glass like distortions.

[IM Output] For this example I generated a small image by Cropping a flower image. Now I want to make it look like it is sitting on some rippled water.

To generate ripples, I need a sine wave gradient of the same size which I can generate using the Evaluate Sin Function. The number '8' represents the number of 'waves' that will be added to the gradient.

  convert -size 150x80 gradient:  -evaluate sin 8  wave_gradient.png
[IM Output]

Now lets distort that image using the an angled displacement vector, not just a simple vertical or horizontal distortion, so as to give it more emphesis.

  composite wave_gradient.png  flower.jpg -displace 5x5 flower_waves.png
[IM Output]

Now that does not seem very interesting, but what if you flip that image compress it vertiaclly and append it to the original...

  convert flower_waves.png -flip \
          flower.jpg  +swap -append  flower_waves_2.png
[IM Output]

Unfortuantally it still looks rather artifical. The reason is that the reflection looks the same at both the top and bottom of the image. It has no sense of 'depth' to it. The reflection is also the same brightness as the original image which is rarely the case.

To make it more realistic you need to use ripple pattern that varies in intensity.

The following uses some fancy Gradient Mathematics to 'attenuate' the wave gradient we were using above.

That is we made the wave pattern linearly smaller as it goes from top to the bottom. This trickiness ensures that the waves finishes at the pure-gray or 'no displacement' color at the bottom of the image (which is later flipped).

  convert -size 150x80 gradient: \
          \( wave_gradient.png \
             +clone -compose multiply -composite \) \
          \( -clone 0 -negate -evaluate divide 2 \
             -clone 1 -compose plus -composite \) \
          -delete 0-1      waves_decreasing.png
[IM Output]

So lets apply this gradient, to form a new reflection of the flower. I also darkened the reflected image slightly to represent some light being lost into the water itself, making it seem more like a water reflection.

  composite waves_decreasing.png  flower.jpg \
            -displace 8x8 miff:- |\
    convert miff:-   -flip   +level 0,80% \
            flower.jpg  +swap -append   flower_in_water.png
[IM Output]

Note that as the distorted image is Flipped to form a reflection, the image will have less 'ripples' at the top of the 'water' closest to where it joins the original image, than at the bottom. This gives the distortion a sense of distance from the viewer.

You can make it even more realistic by distorting the wave displacement maps with a slight rotation, arc, or just with 'random' displacements. This will give the waves a more natural look. Though it is better to do it before it is 'attenuated' so that the 'depth' is added afterward.

Try it, experiment, and let me know what you come up with.

Animated Ripples -
  Using -function Sinusoid with phase changing
End with a polar-depolar version

2-Dimentional Displacement Mapping

So far all the displacement maps have only displace the image in one direction only. Though that direction can be at any angle by setting the appropriate 'XxY' displacement value or 'vector'.

However you can produce a much more complex displacement by using two separate displacement for both the X and Y directions separatally. By doing this each part of the image can move in completely different directions to each other.

However the "composite" command to this is is a litle more complex.

  composite {X displacement} {input image} {Y displacement}+ \
            -displace {X}x{Y}    {result+}

Note the input image order. It is caused by the need to abuse the "composite" option handling, and historical reasons. It is vital you get this correct.

I am hoping to implement a nicer "convert" method for displacement mapping soon

Before IM v6.4.4 using 2 separate displacement maps for separate X and Y displacements was a hit or miss affair. It sometimes worked, and sometimes did not. It is not recommended to attempt to even try to use it on IM's older than this version.

Fractured Mirror

You can create a 'fractured mirror' look to an image by generating random areas of X and Y displacements.

  convert   dragon.gif -sparse-color voronoi '  \
                  %[fx:rand()*w],%[fx:rand()*h]  red
                  %[fx:rand()*w],%[fx:rand()*h]  lime
                  %[fx:rand()*w],%[fx:rand()*h]  black
                  %[fx:rand()*w],%[fx:rand()*h]  yellow
               ' -interpolate integer -implode 1     mirror_areas.gif
  convert   mirror_areas.gif -channel R  -separate   mirror_dismap_x.gif
  convert   mirror_areas.gif -channel G  -separate   mirror_dismap_y.gif

  composite mirror_dismap_x.gif  dragon.gif  mirror_dismap_y.gif +matte \
            -background white -virtual-pixel background -displace 7 \
                                                        mirror_displaced.gif

  convert   mirror_areas.gif -edge 1 -threshold 20% \
            -evaluate multiply .7 -negate               mirror_cracks.gif
  composite mirror_displaced.gif  mirror_cracks.gif -compose multiply \
                                                        mirror_cracked.gif
[IM Output] ==> [IM Output] [IM Output] [IM Output] ==>  
  [IM Output] [IM Output] ==> [IM Output]

Four randomly placed areas are generated using a Voronoi Sparse Color image. This is then given an Implosion Distortion to warp the areas into the center of the image. As each of the four colored areas remain solid colors, each area will contain a displaced copy of the original image. However each color displaces the image in a different way.

To finish off the mirror, Edge Detection is used to outline the edges of the regions and thus the fractured nature of the resulting image. That is the cracks are also made visible.

Of course if I can find a better method of generating a more 'broken like' displacement map, then the results of the above will also be a lot better. Ideas anyone? Mail me.

Shepards Displacement

Random Displacements

Lensing Effects

Frosted Glass Effects

Dispersion Effects (rotated displacements)

Dispersion Effects with Randomized Displacement


FUTURE: Other possible distort / displace mapping examples


Created: 14 January 2009 (distorts sub-division)
Updated: 1 Feburary 2009
Author: Anthony Thyssen, <A.Thyssen@griffith.edu.au>
Examples Generated with: [version image]
URL: http://www.imagemagick.org/Usage/displace/

a