Page 1 of 1

Distort Perspective

Posted: 2013-05-17T09:32:01-07:00
by smajler
Hi everyone i have a question like is there any way to do Perspective distortion which resizes image? Example:
Original image size : 100x100px
Now i'd like to do Perspective distortion to get image with size 100x150px:

0,0,0,0 0,100,0,100 100,0,100,0 100,100,100,150
When i'm using points like above my image is the same as original, it is not resized and trapezoid. Is there any way to achive something like that ?

Re: Distort Perspective

Posted: 2013-05-17T09:47:17-07:00
by GreenKoopa
So you are achieving the desired distortion, but you want the resulting canvas size to be larger? If I'm not understanding, images may help.

-distort keeps the same canvas size. +distort attempts to show the whole of the distorted image. Further control can be gained using -define distort:viewport={geometry_string} For further information see:
http://www.imagemagick.org/script/comma ... hp#distort

Re: Distort Perspective

Posted: 2013-05-18T09:31:30-07:00
by smajler
witch +distort Perspective i've achived in 90% what i wanted. Output image is resized but ImageMagick adds a 2 pixel width border, so output image is 102x152px instead of 100x150. Do you know how it would be in Magick++ (C++) diffrence between -distort and +distort ?

Anyway thanks a lot :)

Re: Distort Perspective

Posted: 2013-05-18T10:12:47-07:00
by fmw42
ImageMagick adds a 2 pixel width border
Not sure that should be, but you can try using -fuzz XX% -trim +repage to remove it. Also you might want to use image coordinates rather than pixel coordinates. See http://www.imagemagick.org/Usage/distor ... oordinates

Re: Distort Perspective

Posted: 2013-05-18T10:39:31-07:00
by GreenKoopa
fmw42 wrote:Also you might want to use image coordinates rather than pixel coordinates.
I think this is likely. Try changing:
0 => 0.5
100 => 99.5
150 => 149.5

Even then, I did say "+distort attempts". If it is closer but still not perfect, you can shave another 0.05 off, or use -define distort:viewport={geometry_string} to specify the output canvas explicitly.

Re: Distort Perspective

Posted: 2013-05-19T20:53:28-07:00
by anthony
I would not use 'pixel centers' but edges as you did originally. Just remember the numbers are floating point, and whole numbers are the 'edges' of pixel areas (image coordinates). Pixel centers are typically only used when liniting up image according to the pixel, rather than using the edge.
See... Layering Distorted Images
http://www.imagemagick.org/Usage/layers/#layer_distort

The 2 pixel with border is to hold 'semi-transparent pixels' that often results in any re-sampled resize (scaling) of an image. Normal resize just 'clips' (actually ignores) such fine details, but distort does not. These pixels are important as they handle the 'edge joining' of multiple images, or layering of images on other images properly.

Actually the 2 pixels is actually a bit of a fudge. In reality, the number of extra pixels added should be the re-sampling filter window radius, scaled appropriately. Which for most distortions is 2*scaling factor. So in your case (50% enlargement) it really should really be 2 pixels * 1.5, or exactly 3 pixels added (exact is rare)!!!!

If you do not like the 2 pixels, just 'shave' the image! Or DIY your own 'viewport', as that is what +distort is actually calculating, using the equivalent forward mapped version of the distortion, if that is possible (not always possible). At least with the current fudge, you know exactly how many pixels needed to be shaved!

NOTE: The image location IS correct, on the virtual canvas (as a layered image). So the image is likely to have a -2-2 page offset. A -crop of that 'virtual canvas' will come out correct, but -shave will shift the image on the virtual canvas.

Re: Distort Perspective

Posted: 2013-05-19T22:22:05-07:00
by GreenKoopa
I always define the viewport explicitly, and now I know why it works. Thank you for the lesson anthony.

Re: Distort Perspective

Posted: 2013-05-19T22:52:32-07:00
by anthony
It is all on the IM Examples pages
http://www.imagemagick.org/Usage/distor ... rt_bestfit

Though the reason for the 2 extra pixels does seem to be missing... will correct.