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. |
Corrects or applies pincushion and/or barrel lens distortion to an image. |
last modified: February 04, 2019
USAGE: pinbarrel "ax,bx,cx,dx,ex ay,by,cy,dy,ey" [-c coords] [-m mode] [-v vp] infile outfile
ax,bx,cx,dx,ex ...... radial coefficients for x dimension; PURPOSE: To correct or apply pincushion or barrel lens distortion. DESCRIPTION: PINBARREL is designed to correct or apply pincushion or barrel lens distortion. The script makes use of -fx and therefore will be rather slow. ARGUMENTS: ax,bx,cx,dx,ex ... These are the radial distortion/correction coefficients for the X dimension equation of: R=r*(a + b*r + c*r^2 + d*r^3 + e*r^4) for normal mode and R=r/(a + b*r + c*r^2 + d*r^3 + e*r^4) for inverse mode, where R is the radius from the center of the infile and r is the radius from the center of the outfile. ay,by,cy,dy,ey ... These are the radial distortion/correction coefficients for the y dimension equation of: R=r*(a + b*r + c*r^2 + d*r^3 + e*r^4) for normal mode and R=r/(a + b*r + c*r^2 + d*r^3 + e*r^4) for inverse mode, where R is the radius from the center of the infile and r is the radius from the center of the outfile. The coefficient a represents scaling and the radial terms control pincushion and barrel distortion. The higher order coefficients affect the pixels further from center. The whole expression has a scaling effect. Thus, to avoid overall scaling, keep a+b+c+d+e=1. For normal mode, pincushion correction and barrel distortion are achieved using b,c,d,e positive and barrel correction and pincushion distortion are achieved using b,c,d,e negative. For a+b+c+d+e>1, the image will be scaled smaller and for a+b+c+d+e<1, the image will be scaled larger. Typical values are: a near 1 and b,c,d,e near zero. For inverse mode, the situation above is reversed. Thus for inverse mode, pincushion correction and barrel distortion are achieved using b,c,d,e negative and barrel correction and pincushion distortion are achieved using b,c,d,e positive. For a+b+c+d+e<1, the image will be scaled smaller and for a+b+c+d+e>1, the image will be scaled larger. Values of a=1,b=0,c=0,d=0,e=0 makes no change in the image in the appropriate dimension. Coefficients a,b,c,d,e are floating point values. -c ... COORDS are the x,y center point coordinates for computing the radial distances. These values may be positive or negative floating point values to allow the center point to be off the image if the image is a subsection. -m ... MODE controls whether to use the normal polynomial coefficient term or its inverse form. The default is normal. -v ... VP is the virtual-pixel method to use. Any valid IM virtual-pixel may be used. The default is black. For more details, see Helmut Dersch's page at http://www.all-in-one.ee/~dersch/barrel/barrel.html and http://wiki.panotools.org/Lens_correction_model Note that the meaning and order of a,b,c,d have been reversed in this script from that of Dresch's work to make parameter specification easier and keep the most significant parameters first in the list. Also as some references suggest that a proper barrel or pincushion distortion is modeled by only the coefficients of the even exponents of r, a fifth term (ex*r^4 and ey*r^4) has been added so that one can specify at least three even term (a+c*r^2+e*r^4). See http://www.imatest.com/docs/distortion.html and http://www.fieldrobotics.org/~cgeyer/OMNIVIS05/final/Li.pdf Other references suggest that the inverse form allows better fit with the same or fewer coefficients. See http://www.fieldrobotics.org/~cgeyer/OMNIVIS05/final/Li.pdf WARNING: The input file must not start with a digit or the script will think it is another set of coefficients. RECOMMENDATION: Use the IM function -distort barrel 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. |
Barrel Distortion - White Square On Black Background |
|||
Original Image |
Both Directions: |
X Direction Only: |
Y Direction Only: |
Pincushion Distortion - White Square On Black Background |
|||
Original Image |
Both Directions: |
X Direction Only: |
Y Direction Only: |
Mixed Barrel And Pincushion Distortion - White Square On Black Background |
||
Original Image |
Barrel in X and Pincusion in Y |
Pincusion in X and Barrel in Y |
Barrel Distortion With Scaling - White Square On Black Background |
||
Original Image |
Barrel Scaled Smaller |
Barrel Scaled Larger |
Pincushion Distortion With Scaling - White Square On Black Background |
||||
Original Image |
Pincusion Scaled Smaller |
Pincusion Scaled Larger |
||
Off Center Barrel And Pincushion Distortions - White Square On Black Background |
||
Original Image |
Barrel |
Pincusion |
Off Center Barrel And Pincushion Distortions - Girl Image (Fat Head vs Pin Head) |
||
Original Image |
Barrel |
Pincusion |
Barrel Distortion Animations |
||
Original Image |
Big Chin |
Evil Eye |
Barrel Distortion Parameter Normalization Versus Image Size |
||
Full Size Image |
Image Center |
Eye Center |
Half Size Image |
Image Center |
Eye Center |
Full Size Results Resized By 50% |
Image Center |
Eye Center |
Barrel Distortion - Grid Image |
||||
Original Image |
Arguments (a,b): |
Arguments (a,b): |
Arguments (a,b): |
Animation |
Pincushion Distortion - Grid Image |
||||
Original Image |
Arguments (a,b): |
Arguments (a,b): |
Arguments (a,b): |
Animation |
Barrel Distortion - Grid Image |
||||
Original Image |
Arguments (a,b): |
Arguments (a,c): |
Arguments (a,d): |
Animation |
Pincushion Distortion - Grid Image |
||||
Original Image |
Arguments (a,b): |
Arguments (a,c): |
Arguments (a,d): |
Animation |
Barrel Distortion: Normal Versus Inverse Mode - Grid Image |
|||||
Original Image |
Normal Mode |
Normal Mode |
Normal Mode |
Normal Mode |
Animation |
Original Image |
Inverse Mode |
Normal Mode |
Normal Mode |
Normal Mode |
Animation |
Barrel Distortion: Normal Versus Inverse Mode - Grid Image |
|||||
Original Image |
Normal Mode |
Normal Mode |
Normal Mode |
Normal Mode |
Animation |
Original Image |
Inverse Mode |
Inverse Mode |
Inverse Mode |
Inverse Mode |
Animation |
Barrel Distortion Correction - Dersch Image |
||
Original Image |
Dersch Correction |
|
What the script does is as follows:
This is equivalent to the following IM commands:
|