Page 1 of 1
Affine vs perspective transformation
Posted: 2010-07-02T05:26:59-07:00
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?
Re: Affine vs perspective transformation
Posted: 2010-07-02T07:13:16-07:00
by snibgo
The obvious method is "-distort perspective". The "-distort affine" "-affine" options don't give you shear, which you need.
Re: Affine vs perspective transformation
Posted: 2010-07-02T09:50:30-07:00
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
Re: Affine vs perspective transformation
Posted: 2010-07-04T17:13:59-07:00
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

Perspective

BilinearForward -- Trapeziodal

Re: Affine vs perspective transformation
Posted: 2010-07-04T17:17:08-07:00
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.