Distort Perspective

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
smajler
Posts: 28
Joined: 2013-05-17T09:26:53-07:00
Authentication code: 6789

Distort Perspective

Post 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 ?
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Distort Perspective

Post 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
smajler
Posts: 28
Joined: 2013-05-17T09:26:53-07:00
Authentication code: 6789

Re: Distort Perspective

Post 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 :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Distort Perspective

Post 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
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Distort Perspective

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Distort Perspective

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Distort Perspective

Post by GreenKoopa »

I always define the viewport explicitly, and now I know why it works. Thank you for the lesson anthony.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Distort Perspective

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply