Fred's ImageMagick Scripts



    Licensing:

    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial (non-profit) 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

    Please read the Pointers For Use on my home page to properly install and customize my scripts.

LOGPOLAR


Applies either a rectangular (cartesian) to log-polar transformation or a log-polar to rectangular (cartesian) transformation on an image.

Download Script

last modified: July 31, 2021



USAGE: logpolar [-m mode] [-c xc,yc] [-r rmn,rmx] [-a amn,amx] [-v vpmode] [-b bgcolor] [-i interpolate] infile outfile
USAGE: logpolar [-h or -help]

-m .... mode ......... rect2logpolar or logpolar2rect; default=rect2logpolar
-c .... xc,yc ........ coordinates of the transformation center;
...................... default is image center
-r .... rmn,rmx ...... min and max radius; default=0,(half image diagonal);
...................... non-negative floats; rmx can be w, h, d or m for
...................... width/2, height/2, diagonal/2 or min(width,height)/2 of image
-a .... amn,amx ...... min and max angle; default=-180,180;
...................... floats between -180 and 180
-v .... vpmode ....... virtual-pixel mode to use to fill area of output image
...................... that are outside the input image; default=black
-b .... bgcolor ...... background color for virtual-pixel=background
...................... any valid IM color is allowed. The default=black
-i .... interpolate .. type of interpolation to use; choices are: nearest,
...................... bilinear, catrom, average4 and average9; the
...................... default=bilinear

PURPOSE: To apply either a rectangular (Cartesian) to log-polar transformation or a log-polar to rectangular (Cartesian) transformation on an image.

DESCRIPTION: LOGPOLAR applies either a rectangular (Cartesian) to log-polar transformation or a log-polar to rectangular (Cartesian) transformation on an image. The user has control of the range of radius, angle and center in the transformation.

ARGUMENTS:

-m mode ... MODE is the transformation mode, which can be either rect2logpolar (rectangular to log-polar) or logpolar2rect (log-polar to rectangular). The former, rect2logpolar, treats the input as a normal rectangular image with columns representing X and rows representing Y and treats the output as having columns representing angle and rows representing the log of radius. The latter, logpolar2rect, treats the input as having columns representing angle and rows representing the log of radius and treats the output as having columns representing X and rows representing Y. Note that in a polar image, nominally the left column is angle=amn=-180, the middle column is angle=0 and the right column is angle=amx=180. Similarly, the top row corresponds to the radius=rmin and the bottom row corresponds to radius=rmx.

-c xc,yc ... XC,YC are the pixel coordinates for the transformation center. The default is the image center.

-r rmn,rmx ... RMN,RMX are the min and max radii. By default, rmn=0 and rmx=(half image diagonal). This provides a transformation that includes the whole image. However, for the rect2polar mode, this will cause the output image to contain areas that are outside the input image. They will be filled as determined by the virtual pixel setting. Value are non-negative floats. Values are generally non-negative floats, but four other choices are allowed: w, h, d and m, where w=width/2, h=height/2, d=diagonal/2 and m=min(width,height)/2.

-a amn,amx ... AMN,AMX are the min and max angles. By default, amn=-180 and amx=180. Values for amn and amx are floats between -180 and 180.

-v vpmode ... VPMODE is the virtual-pixel mode. Any valid IM virtual-pixel setting is allowed. The default is black.

-b bgcolor ... BGCOLOR is the background color to use with vpmode=background. Any valid IM color may be used. The default is black.

-i interpolate ... INTERPOLATE is the type of interpolation to use. The choices are: nearest (n), bilinear (b), catrom (c), average4 and average9. The default=bilinear. Note that catrom is the same as Key's cubic convolution.

Note that this script may be slow due to the use of -fx.

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

Image In Log-Polar Coordinates Using This Script
Arguments:
-m rect2logpolar

Image In Polar Coordinates Using Polar Script
Arguments:
-m rect2polar



What the script does is as follows:

  • Applies an rectangular to log-polar or log-polar to rectangular warp to an image
  • Uses -fx; therefore slow

This is equivalent to the following IM commands for the case
of a rectangular to logpolar warp:

  • compute image dimensions for default center and max radius values
  • yy="yy=exp((j-$rmn)*ln($rmx-$rmn)/(h-1))*cos((($amx-($amn))*pi/180)*(i/(w-1))+($amn*pi/180))+$yc;"
  • xx="xx=exp((j-$rmn)*ln($rmx-$rmn)/(h-1))*sin((($amx-($amn))*pi/180)*(i/(w-1))+($amn*pi/180))+$xc;"
  • convert $infile -virtual-pixel $vpmode -background $bgcolor -monitor \
    -interpolate $interpolate -fx "$xx $yy u.p{xx,yy}" +monitor "$outfile"