Affine vs perspective transformation

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
qwertz

Affine vs perspective transformation

Post by qwertz »

I'm porting some code from an ancient graphic toolkit to use the ImageMagick API instead because I need things like interpolation which the old toolkit does not have. In that old toolkit there is a function named MapTexture() which distorts the input image to a trapezoid that is defined by 4 points that must be given: (x1|y1),(x2|y2),(x3|y3),(x4|y4). What ImageMagick equivalent should I use to replace this function? My suggestion is to use a perspective transformation like this:

Code: Select all

w := width of test.jpg
h := height of test.jpg
convert test.jpg -distort -virtual-pixel black -distort Perspective "0,0,x1,y1 w,0,x2,y2 w,h,x3,y3 0,h,x4,y4" output.jpg
I tried this and the result looks good. But I'm still a bit confused because I always thought this kind of texture mapping would be an affine transformation and not a perspective distortion! Am I wrong here or could the same effect also be achieved by using an affine transformation matrix in the format of sx,rx,ry,sy,tx,ty?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Affine vs perspective transformation

Post by snibgo »

The obvious method is "-distort perspective". The "-distort affine" "-affine" options don't give you shear, which you need.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Affine vs perspective transformation

Post by fmw42 »

A four point transform is usually equivalent to -distort perspective or -distort bilinearforward, if you need shear or trapezoid distortion and depending upon whether you need to preserver all straight lines. Otherwise it is just affine that needs 3 points to do scale, translate and rotate, which can be done most easily with -distort SRT

see http://www.imagemagick.org/Usage/distorts/ and esp http://www.imagemagick.org/Usage/distor ... ar_forward
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Affine vs perspective transformation

Post by anthony »

qwertz wrote:In that old toolkit there is a function named MapTexture() which distorts the input image to a trapezoid that is defined by 4 points that must be given: (x1|y1),(x2|y2),(x3|y3),(x4|y4).
A 4 point trapezoid distortion is a Bilinar Distortion. This preserves distances, and orthogonal lines in the image.

Perspective preserves ALL straight lines, not distance, whcih results in the center of the image becoming shifted toward the 'smaller' ends of the distorted image.

See comparisons in IM Examples, Bilinear Distortions
http://www.imagemagick.org/Usage/distorts/#bilinear

Original
Image
Perspective
Image
BilinearForward -- Trapeziodal
Image
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Affine vs perspective transformation

Post by anthony »

snibgo wrote:The obvious method is "-distort perspective". The "-distort affine" "-affine" options don't give you shear, which you need.
Affine does give shear! What it doesn't give is position dependant scaling (image gets smaller on one side compares to the other.)
It is the difference between three control points (affine) and four control points.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply