perspective projection corrdinates

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
pospiech
Posts: 5
Joined: 2012-09-09T00:41:12-07:00
Authentication code: 67789

perspective projection corrdinates

Post by pospiech »

I want to do perspective projections, but I completely fails to understand the coordinates used.

This code does nothing

Code: Select all

convert input.jpg  -matte -virtual-pixel transparent -distort Perspective "0,0,0,0  0,90,0,90  90,0,90,0 90,90,90,90" output.png
This shrinks the image on the right side

Code: Select all

convert input.jpg  -matte -virtual-pixel transparent -distort Perspective "0,0,0,0  0,90,0,90  90,0,90,5 90,90,90,90" output.png
But what is necessary to shrink the image on the left side ?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: perspective projection corrdinates

Post by Bonzo »

The co-ordinate system changed at some point so the version can have an effect.

In your case the blocks of numbers are the original co-ordinates in x y and the new co-ordinates in x y.

so 0,0,0,10 will move the top left corner which is 0,0 to the point 0,10 X stays the same and Y moves down 10px

You can see some examples using php on my website: Distort and more details on Anthony's example site
pospiech
Posts: 5
Joined: 2012-09-09T00:41:12-07:00
Authentication code: 67789

Re: perspective projection corrdinates

Post by pospiech »

Bonzo wrote:The co-ordinate system changed at some point so the version can have an effect.

In your case the blocks of numbers are the original co-ordinates in x y and the new co-ordinates in x y.
Coordinates in pixels ? That would be a big problem. I want to let it run in a loop where all images have different pixel sizes.
Bonzo wrote: so 0,0,0,10 will move the top left corner which is 0,0 to the point 0,10 X stays the same and Y moves down 10px
And what does the third '90,0,90,5' mean then? it performs a perspective transformation and does not move the image.
Aditionally I do not understand the meaning of 90, 0 if my image has the size 200x200.
Bonzo wrote: You can see some examples using php on my website: Distort and more details on Anthony's example site
Unfortunaltely all I have seen are fancy examples, but I need very basic examples that allow me to understand how to change the number to achive the image I want.
pospiech
Posts: 5
Joined: 2012-09-09T00:41:12-07:00
Authentication code: 67789

Re: perspective projection corrdinates

Post by pospiech »

I found it out by simply trying the effect of each number.

These numbers are organised as original position to target position x1y1 -> x2y2
with the coordinates: left-top, left-bottom, right-top, right-bottom,
where each number is in coordinates of pixels (unfortunately).

So what I need is:

Code: Select all

convert input.jpg  -matte -virtual-pixel transparent -distort Perspective "0,0,0,20  0,200,0,180  200,0,200,0 200,200,200,200" output.png 
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: perspective projection corrdinates

Post by fmw42 »

It works just a well without the middle comma.

convert input.jpg -matte -virtual-pixel transparent -distort Perspective "0,0 0,20 0,200 0,180 200,0 200,0 200,200 200,200" output.png

The order of corners does not matter.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: perspective projection corrdinates

Post by anthony »

pospiech wrote:I found it out by simply trying the effect of each number.

These numbers are organised as original position to target position x1y1 -> x2y2
with the coordinates: left-top, left-bottom, right-top, right-bottom,
where each number is in coordinates of pixels (unfortunately).

So what I need is:

Code: Select all

convert input.jpg  -matte -virtual-pixel transparent -distort Perspective "0,0,0,20  0,200,0,180  200,0,200,0 200,200,200,200" output.png 
That is correct. Each set of four numbers is the movement of ONE control point, with the first two numbers becoming the second two numbers.

I do explain this in IM Examples, in the section, Distortions Using Control Points
http://www.imagemagick.org/Usage/distor ... rol_points
It is also explained in the option reference... (at the end of the list of distort methods)
http://www.imagemagick.org/script/comma ... p?#distort

This explaination is in 'Affine Distortions' (2 or 3 point) in the section BEFORE the more complex 'perspective distortions'.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: perspective projection corrdinates

Post by whugemann »

Coordinates in pixels ? That would be a big problem. I want to let it run in a loop where all images have different pixel sizes.
In fact, it's not a problem. If you are working with a bunch of images, you will probably have to use a script anyway. This script could also perform any co-ordinate transformation you need. Please note that IM accepts fractional coordinates, such that you don't have to introduce rounding errors. At http://www.imagemagick.org/Usage/windows/#vb_text you find a VBScript that transforms WinMorph's fractional co-ordinates (0 to 1) into the co-ordinates used by IM.
Wolfgang Hugemann
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: perspective projection corrdinates

Post by anthony »

Also not the coordinates are 'image coordinates' not 'pixel coordinates'. That means 0,0 referred to the exact edge of the top-left corner of the IMAGE. It is not the center of the PIXEL in the top-corner.

See Image Coordinates vs Pixel Coordinates
http://www.imagemagick.org/Usage/distor ... oordinates

Basically that means if you want to line up a pixel, you need to specify the center of the pixel, which is the pixel coordinates + 0.5.

Often it does not matter much, but if not right the distorted image may have be incorrectly positioned by a sub-pixel distance translation.

Finally image offsets (layer images) are not handled by 'composite' which is a much lower level operator. Images should be 'layered' using -layer merge, -flatten, -mosaic (the later are short aliases for -layer flatten and -layer mosaic).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply