Fred's ImageMagick Scripts



    Licensing:

    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial use, ONLY.

    For use of my scripts in commercial (for-profit) environments or non-free applications, please contact me (Fred Weinhaus) for licensing arrangements. My email address is fmw at alink dot net.

    If you: 1) redistribute, 2) incorporate any of these scripts into other free applications or 3) reprogram them in another scripting language, then you must contact me for permission, especially if the result might be used in a commercial or for-profit environment.

    Usage, whether stated or not in the script, is restricted to the above licensing arrangements. It is also subject, in a subordinate manner, to the ImageMagick license, which can be found at: http://www.imagemagick.org/script/license.php

ROTATE3D


Applies a perspective distortion to an image by providing three rotation angles and an optional perspective exaggeration

Download Script

last modified: September 26, 2017



USAGE: rotate3D pan,tilt,roll[,pef] infile outfile
USAGE: rotate3D

pan ................. rotation about image vertical centerline: -180 to +180 (deg)
tilt ................ rotation about image horizontal centerline: -180 to +180 (deg)
roll ................ rotation about the image center: -180 to +180 (deg)
pef ................. perspective exaggeration factor: 0 to 3.19
function name only .. get help

PURPOSE: To apply a perspective distortion to an image by providing three rotation angles and an optional perspective exaggeration.

DESCRIPTION: ROTATE3D applies a perspective distortion to an image by providing three optional rotation angle: pan, tilt and roll with an option to control the perspective exaggeration.

PAN is a rotation of the image about its vertical centerline -180 to +180 degrees. Positive rotations turn the right side of the image away from the viewer and the left side towards the viewer. Zero is no rotation. A PAN of +/- 180 deg achieves the same results as -flip.

TILT is a rotation of the image about its horizontal centerline -180 to +180 degrees. Positive rotations turn the top of the image away from the viewer and the bottom towards the viewer. Zero is no rotation. A TILT of +/- 180 deg achieves the same results as -flop.

ROLL (like image rotation) is a rotation in the plane of the the image -180 to +180 degrees. Positive values are clockwise and negative values are counter-clockwise. Zero is no rotation. A ROLL of any angle achieves the same results as -rotate.

PAN, TILT and ROLL are order dependent. If all three are provided, then they will be done in that order.

PEF is the perspective exaggeration factor. It ranges from 0 to 3.19. A normal perspective is achieved with the default of 1. As PEF is increased from one the perspective effect moves towards that of a wide angle lens (more distortion). If PEF is decreased from one the perspective effect moves towards a telephoto lens (less distortion). PEF of 0.5 achieves an effect close to no perspective distortion. PEF values too large may cause part of the image appear above the 'horizon' in the 'sky'.

Mathematics Background (32KB PDF)

CAVEAT: No guarantee that this script will work on all platforms, nor that trapping of inconsistent parameters is complete and foolproof. Use At Your Own Risk.


EXAMPLES


Original Image



Various Perspective Distortions Applied To Image
Rotation Angle Changes

arguments:
45,0,0
(pan=45 degrees)

arguments:
0,45,0
(tilt=45 degrees)

arguments:
0,0,45
(roll=45 degrees)

arguments:
45,45,0
(pan=45 degrees,
tilt=45 degrees)



Various Perspective Distortions Applied To Image
Perspective Exaggeration Factor Changes

arguments:
0,45,0,0.5
(pef=0.5 telephoto)

arguments:
0,45,0,1
(pef=1 default)

arguments:
0,45,0,1.8
(pef=1.8 wide angle)

arguments:
0,45,0,2.8
(pef=2.8 too large)



Comparison Of Perspective vs Bilinear Distortions
For Pan=30 and Tilt=30

Perspective

True Bilinear

Pseudo Bilinear

Peserves all straight lines from input

Preserves all horizontal and vertical straight lines from input, but diagonal lines get curved.

Does not preserve any straight lines from input.


What the script does is as follows:

  • Treats the image as if it is on the X-Z plane at Y=0
  • Rotates the corner coordinates of the image according to the
    rotation angles
  • Defines a field of view, fov=56 degrees (nominally) corresponding to
    the diagonal dimension of the image
  • Computes the corresponding focal length which is then taken as the
    distance of a fictitious camera behind the picture along the -Y
    direction which is capturing the resulting perspective picture.
  • Projects the rotated corner coordinates into the camera plane
  • Computes the bounding box for these points
  • Scales and offsets the bounding box to fill an image the size
    of the input image
  • Uses the scaled and offset output picture coordinates with the
    input coordinates and feeds these to -distort perspective to
    compute the new output image

For the case of pan=30 and tilt=30, this is equivalent to:

  • convert mandril.jpg -virtual-pixel transparent -background black \
    -distort Perspective '0,0 127,0 127,127 0,127 27,0 112,35 126,127 0,115' \
    mandril_perspective_p30_t30.jpg