Page 1 of 2

SOLVED - Question: Perspective and Coordinates ?

Posted: 2009-05-04T04:34:58-07:00
by rick0
Hi guys,

I'm looking for a way of using the info I got from Gimp Perspective tool and put that info in my cmd line.
Any tip or trick would be much appreciate (gimp or other).

Gimp Perspective tool:
Image

Imagemagick cmd:
convert pic1.png -matte -virtual-pixel transparent -distort Perspective "0,0,0,0  0,50,0,50  0,90,0,90  90,90,90,90" pic1_pers.png

How do you guys callculate the perspective coordinates normally ??
Thanks :)

Re: Question: Perspective and Coordinates ?

Posted: 2009-05-04T10:02:35-07:00
by fmw42
see http://www.imagemagick.org/Usage/distor ... projection

You may have to adjust the coefficients from GIMP, but worth a try to just plug in. If not wait for Anthony to reply. He can give you more details on the IM coefficients used with -distort PerspectiveProjection

you can also add -verbose to your -distort perspective and get the coefficients

convert pic1.png -matte -virtual-pixel transparent -verbose -distort Perspective "0,0,0,0 0,50,0,50 0,90,0,90 90,90,90,90" +verbose pic1_pers.png

see http://www.imagemagick.org/Usage/distor ... rt_verbose as per this affine distortion summary

Re: Question: Perspective and Coordinates ?

Posted: 2009-05-04T19:07:32-07:00
by anthony
If you add -verbose just before the Perspective distortion IM will output two operators that should be near equivalent replacements to the -distort operator. One is a VERY SLOW -fx version/
The other will be the Forward mapping Perspective_Projection matrix.


For example (this is NOT the same as your maping

Code: Select all

convert rose: -matte -virtual-pixel transparent -verbose \
        -distort Perspective "0,0,0,0 0,46,0,46 70,0,60,10 70,46,60,30" \
       +verbose null:
produces

Code: Select all

Perspective Projection:
  -distort PerspectiveProjection \
      '1.97143,-2.14114e-17,3.50196e-15,0.328571,
       1,2.27881e-14,0.0185714,2.71608e-18'
Perspective Distort, FX Equivelent:
  -fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;
       rr=-0.009420*ii -0.000000*jj + 1;
       xx=(+0.507246*ii +0.000000*jj -0.000000)/rr;
       yy=(-0.166667*ii +1.000000*jj -0.000000)/rr;
       rr>0 ? p{ xx-page.x-.5, yy-page.x-.5 } : blue'
The first section Perspective Projection can be used to map source coordinates into destination coordinates, allowing you to mark or draw match lines on BOTH images. But the numbers don't seem to match up to the GIMP matrix you give. So I am not sure how they relate. But they generate the mapping form...
i = ( 1.97143*x +0.000000*y -0.000000 ) / ( 0.0185714*x + 0.000000*y + 1 )
j = ( 0.328571*x +1.000000*y +0.000000 ) / ( 0.0185714*x + 0.000000*y + 1 )
The numbers of the form ####e-19 may as well be equivalent to 0.0000 for all that it matters.

WARNING: about coordinates see Image Coordinates vs Pixel Coordinates That is add 0.5 from X and Y coordinates before applying the above, and subtract 0.5 from them afterwards.


The FX equivalent is a check of the internal algorithm used, but without the Elliptical Weighted Average used to merge multiple pixels together when the distortion become highly compressed. Instead it only uses the -fx direct Interpolated Lookup of the source image.

The numbers used in the FX equivalent algorithm is related to the forward mapped matrix coefficients (using exactly the same formula), but are in fact the reverse mapped matrix coefficients being used to map a destination coordinate to the source image, for the interpolated color lookup.

The final test in the FX handles the invalid 'sky', where the destination fails to map to the source image correctly.



I have written up the above in IM Examples, Distorts, Perspective Mapping
http://www.imagemagick.org/Usage/distor ... ve_mapping

I really should do one on mapping draw coordinates ;-)

Re: Question: Perspective and Coordinates ?

Posted: 2009-05-04T19:35:19-07:00
by fmw42
It would seem to me that the -fx inverse coefficients should be equivalent to what GIMP produces.

However the GIMP coefficients the original user provided do NOT match the IM transformation that he provided since the IM transformation is an Identity transformation or null transformation. The input and output coordinates are identical. The GIMP coordinates should then be a diagonal of ones with zero everywhere else. So perhaps the original user should check one or the other again and provide a proper comparison.

Also strange that GIMP seems to be reporting a comma in stead of a decimal. Is this a European version of GIMP?

Testing GIMP with Anthony's exact coordinates:
Anthony wrote:convert rose: -matte -virtual-pixel transparent -verbose \
-distort Perspective "0,0,0,0 0,46,0,46 70,0,60,10 70,46,60,30" \
+verbose null:
For which I get:

Perspective Projection:
-distort PerspectiveProjection \
'1.97143,-3.04525e-17,4.54175e-16,0.328571,
1,4.44238e-15,0.0185714,-4.38105e-18'
Perspective Distort, FX Equivelent:
-fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;
rr=-0.009420*ii +0.000000*jj + 1;
xx=(+0.507246*ii +0.000000*jj -0.000000)/rr;
yy=(-0.166667*ii +1.000000*jj -0.000000)/rr;
rr>0 ? p{ xx-page.x-.5, yy-page.x-.5 } : blue'

(Which does not quite match what Anthony reported??? I am on IM 6.5.2-0 Q16 Mac OSX Tiger)

(Also as Anthony is only reporting 8 coefficients in the -distort PerspectiveProjection, I presume the 9th is 1. In order to make it easier to interpret, it might be nice to have it formatted so that one has 3 rows of 3 coefficients: numX, numY, den)

However, I get the following from GIMP for the corresponding coefficients:

Image

Which is not close to either set of coefficients reported by IM -verbose -distort perspective.

Re: Question: Perspective and Coordinates ?

Posted: 2009-05-04T20:27:37-07:00
by anthony
fmw42 wrote:It would seem to me that the -fx inverse coefficients should be equivalent to what GIMP produces.
Except that most of those original control points are almost in a line. The error on the original set is enormous. They are not good control points! But then you noted that it should generate a unity matrix, (except for the error rate).

It may be that gimp added 0.5 to the control points to convert from 'pixel coodinates' to image coordinates. I do not.

If I want to move and position the 0,0 pixel I would use 0.5,0.5 If I want to move the actual 'corner' of the image (not the corner pixel) I would then use 0,0. As this transform may eventually be used for 'panorama' matching, I wanted to be able to specify coodinates with fractions of a pixel.

This may be the source of the differences between IM and GIMP.

Re: Question: Perspective and Coordinates ?

Posted: 2009-05-04T20:33:01-07:00
by anthony
fmw42 wrote:Also as Anthony is only reporting 8 coefficients in the -distort PerspectiveProjection, I presume the 9th is 1. In order to make it easier to interpret, it might be nice to have it formatted so that one has 3 rows of 3 coefficients: numX, numY, den)
Already changed the output into three rows of 3,3,2 numbers, which you can see on the IM Examples page (when it appears)
Mapping Perspective Distortion
http://www.imagemagick.org/Usage/distor ... ve_mapping

I would like to eventually replace the example to include marking a coordinate in source (using -draw), and marking the same coordinate in its distorted position in the distorted image (again using draw), but AFTER the the image is distorted, so the mark is not distorted.

Re: Question: Perspective and Coordinates ?

Posted: 2009-05-04T21:19:26-07:00
by fmw42
Already changed the output into three rows of 3,3,2 numbers, which you can see on the IM Examples page (when it appears)
Mapping Perspective Distortion
http://www.imagemagick.org/Usage/distor ... ve_mapping

If you don't mind, please make it 3,3,3 (including the constant 1 term). Many people may not understand the default of that term is 1 and may assume it is zero, if left off.

If I want to move and position the 0,0 pixel I would use 0.5,0.5 If I want to move the actual 'corner' of the image (not the corner pixel) I would then use 0,0. As this transform may eventually be used for 'panorama' matching, I wanted to be able to specify coodinates with fractions of a pixel.

This may be the source of the differences between IM and GIMP.
The difference appears to be too big to be accounted for by a 0 to 0.5 change. But I could be wrong. Anthony, do you have a way to regenerate the -verbose information with the old code that did not account for the 0.5 shift?

Re: Question: Perspective and Coordinates ?

Posted: 2009-05-04T21:54:00-07:00
by anthony
fmw42 wrote:
Already changed the output into three rows of 3,3,2 numbers, which you can see on the IM Examples page (when it appears)
Mapping Perspective Distortion
http://www.imagemagick.org/Usage/distor ... ve_mapping
If you don't mind, please make it 3,3,3 (including the constant 1 term). Many people may not understand the default of that term is 1 and may assume it is zero, if left off.
Sorry if I did that then their are too many terms for the FX PerspectiveProjection argument, and the 'Equivelent Command' will not work.
If I want to move and position the 0,0 pixel I would use 0.5,0.5 If I want to move the actual 'corner' of the image (not the corner pixel) I would then use 0,0. As this transform may eventually be used for 'panorama' matching, I wanted to be able to specify coodinates with fractions of a pixel.

This may be the source of the differences between IM and GIMP.
The difference appears to be too big to be accounted for by a 0 to 0.5 change. But I could be wrong. Anthony, do you have a way to regenerate the -verbose information with the old code that did not account for the 0.5 shift?
The old code did it the same way! The difference was I did not document it!

If you want to do what GIMP is doing try adjusting the input/output coordinates by 0.5. I am just not exactly certian what GIMP does, as in IM the control points are used ASIS for the mathematical calculations.

Re: Question: Perspective and Coordinates ?

Posted: 2009-05-05T05:16:50-07:00
by rick0
Thanks anthony/fmw42

Lots of info in here now, but to be honest you guys are talking to technical for me ;)
so let me read this post a couple of times. I need to understand. and then I reply.

- Yes im using Gimp European version.
convert pic1.png -matte -virtual-pixel transparent -distort Perspective "0,0,0,0 0,50,0,50 0,90,0,90 90,90,90,90" pic1_pers.png
- The numbers in red have nothing todo with the numbers from gimp, it was just an example.

Note: I'm just trying to find a visual and easy way of find the coordinates, and then implement them in IM.
That's why I included the screenshot.

Thanks a million for your replies!
You guys rock!

Re: Question: Perspective and Coordinates ?

Posted: 2009-05-05T10:07:04-07:00
by fmw42
Anthony wrote:Sorry if I did that then their are too many terms for the FX PerspectiveProjection argument, and the 'Equivelent Command' will not work.
Yes, I see that now in your distort page. You assume the constant in the denominator is 1 and you only excect 8 coeffs. Unfortunately, it is a little inconsistent to have one set of coeffs from fx show 9 values and the other show 8, but I understand your issue.


Here are some experiments:


convert rose: -matte -virtual-pixel transparent -verbose \
-distort Perspective "0,0,0,0 0,46,0,46 70,0,60,10 70,46,60,30" \
+verbose null:

Perspective Projection:
-distort PerspectiveProjection \
'1.97143,-3.04525e-17,4.54175e-16,0.328571,
1,4.44238e-15,0.0185714,-4.38105e-18'

Perspective Distort, FX Equivelent:
-fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;
rr=-0.009420*ii +0.000000*jj + 1;
xx=(+0.507246*ii +0.000000*jj -0.000000)/rr;
yy=(-0.166667*ii +1.000000*jj -0.000000)/rr;
rr>0 ? p{ xx-page.x-.5, yy-page.x-.5 } : blue'


Original: rose.png
Image

Warp by control points:
convert rose: -channel rgba -matte -virtual-pixel transparent -distort Perspective "0,0,0,0 0,46,0,46 70,0,60,10 70,46,60,30" rose_control_pts.png
Image


Then I took the fx coefficients and verified by fx warp:
convert rose: -channel rgba -matte -virtual-pixel transparent -mattecolor none -monitor -fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;
rr=-0.009420*ii +0.000000*jj + 1;
xx=(+0.507246*ii +0.000000*jj -0.000000)/rr;
yy=(-0.166667*ii +1.000000*jj -0.000000)/rr;
rr>0 ? p{ xx-page.x-.5, yy-page.x-.5 } : none' rose_fx.png
Image

Then I put the fx coeffs into -distort perspectiveprojection:
convert rose.png -distort perspectiveprojection ".507,0,0,-.167,1,0,-.00943,0" rose_fx_coeffs.png
Image

But the result was not even close. My understanding of perspective is that the inverse of a perspective transformation is a perspective transformation. Thus I would have thought taking the fx coefficients and using them in -distort perspectiveprojection might have been close, apart from the 0.5 pixel shift.

So then I took the distort perspectiveprojection coeffs and used them as:

convert rose: -channel rgba -matte -virtual-pixel transparent -mattecolor none -distort perspectiveprojection "1.97,0,0,0.329,1,0,0.0185,0" rose_proj_coeffs.png
Image

THIS WORKS. The coefficients are indeed the forward projection coefficients, but the function -distort perspectiveprojection is doing an inverse transformation. Thus Anthony is inverting the coefficient matrix before using it. (As he notes at http://www.imagemagick.org/Usage/distor ... projection if I had read it more carefully beforehand)

So then I took the coefficients from GIMP:

convert rose: -channel rgba -matte -virtual-pixel transparent -mattecolor none -distort perspectiveprojection "3.55,0,0,.951,.922,0,.0491,-.00169" rose_gimp_coeffs.png
Image


This seems close and the difference could be due the 0.5 pixel shift that is expected in -distort perspectiveprojection. So the GIMP coefficients are GIMPs version of the forward coefficients and not the inverse or reverse mapping coefficients.
Anthony wrote:If you want to do what GIMP is doing try adjusting the input/output coordinates by 0.5. I am just not exactly certian what GIMP does, as in IM the control points are used ASIS for the mathematical calculations.
Unfortunately GIMP does not allow one to supply fractional pixel coordinates. It works only by GUI by dragging the corners, but does give a read out (in the lower left corner of the window) to show you the coordinates.

rick0 wrote:Note: I'm just trying to find a visual and easy way of find the coordinates, and then implement them in IM.
Best I can suggest is to try adjusting the corners of the image in GIMP and write down the old and new coordinate values. The old ones are the original image corners and the new ones are the values shown in the bottom left corner of the window as you drag the corner. Too bad GIMP does not list out the control points, but only their version of the coefficients.

Re: Question: Perspective and Coordinates ?

Posted: 2009-05-05T11:38:00-07:00
by rick0
Hi guys,

I read this post like 5 times, and understand even less now :D

fmw42, i tried what you said but still doesnt work. let me explain what i try to do:

the purpose for this is that I want to create the right cmd with coordinates to put on my PHP exec().

Let's take for example this car:
Image

This is before i drag the corners, (don't know why it show 1, but i guess every colum is a corner + middle)
Image

Simple perspective, by moving 2 right corners.
Image


Now I get the Matrix data. after the drag.
1,15090 0,00000 0,00000
0,08297 0,99140 0,00000
0,00052 -0,00003 1,00000

................. EDITED
I just saw your images and example,
convert rose: -channel rgba -matte -virtual-pixel transparent -mattecolor none -distort perspectiveprojection "1.97,0,0,0.329,1,0,0.0185,0" rose_proj_coeffs.png
Image

THIS WORKS. So I am puzzled why Anthony says it is forward projecting?
how did you get the numbers ? 1.97,0,0,0.329,1,0,0.0185,0
don't see the link with:

Code: Select all

convert rose.png -distort perspectiveprojection ".507,0,0,-.167,1,0,-.00943,0" rose_fx_coeffs.png
You pretty close!!

This is getting to complicated for me, so I can't really help understand :(
But if you could provide us a working example and how you got there, like you did above that would safe our lifes.

Did you try adding 0.50pixel to it ?
So you guys don't have anything visual or some draw tip that I can use to find out the coordinates??

I'm so confused. :roll:
Thanks for trying to help fmw42. Much appreciated mate.

Re: Question: Perspective and Coordinates ?

Posted: 2009-05-05T13:56:59-07:00
by fmw42
I will try to help but I have to go out for a while. But quickly, here are some replies:
This is before i drag the corners, (don't know why it show 1, but i guess every colum is a corner + middle)
The perspective coefficient matrix is not a listing of control points. It is the transformation coefficients for the perspective function which is a rational linear polynomial equation (first order in the numerator and first order in the denominator). See http://www.imagemagick.org/Usage/distor ... projection It will be all zeros with 1s along the diagonal for any original image as it shows that there is no change. The ones means that x stays at x and y stays at y for every point.

Code: Select all

how did you get the numbers ? 1.97,0,0,0.329,1,0,0.0185,0 

From Anthony's example:

convert rose: -matte -virtual-pixel transparent -verbose \
-distort Perspective "0,0,0,0 0,46,0,46 70,0,60,10 70,46,60,30" \
+verbose null:

Which outputs
Perspective Projection:
-distort PerspectiveProjection \
'1.97143,-3.04525e-17,4.54175e-16,0.328571,
1,4.44238e-15,0.0185714,-4.38105e-18'

But if you could provide us a working example and how you got there, like you did above that would safe our lifes.
All I did was open the image in GIMP and drag the corners, but I recorded the coordinates that show in the lower left corner before moving the cursor away from the corner. See below:

Image


All I did in my earlier examples was to pull the corners of the rose image to match Anthony's coordinates above. Then I recorded the coefficients from the GIMP window and supplied them to -distort perspectiveprojection.

However, this will not be a good idea, since the GIMP coefficients do not produce the correct values for the IM -distort perspectiveprojection.

BUT, if you record the 4 moved corner points along with the original image corner points and supply them to IM -distort perspective, you should get the right IM result.

Hope that helps. If not I will try to answer more questions later when I get back.

P.S. I don't know exactly what you are trying to achieve for results, but if you are on Linux or Mac OSX and want something simple, you can try either of my perspective rotation scripts, 3Drotate or rotate3D at http://www.fmwconcepts.com/imagemagick/index.php

Re: Question: Perspective and Coordinates ?

Posted: 2009-05-05T18:49:44-07:00
by anthony
rick0 wrote:
convert pic1.png -matte -virtual-pixel transparent -distort Perspective "0,0,0,0 0,50,0,50 0,90,0,90 90,90,90,90" pic1_pers.png

The numbers in red have nothing todo with the numbers from gimp, it was just an example.


I see the mail point for you was that three of the control points were in a line, and that confused the issue. Change the third set (now in blue) to 90,0,90,0 would have make thinks more sensible.

However in summery from out discussion. Gimp coefficients should be reasonable close to what IM is using. We are just attempting to determine if GIMP adds 0.5 to the points you give (convert pixel positions to mathematical image coordinates) before attempting to figure out the matrix coefficients. This makes some sense for gimp which I believe sets the points via its user interface.

In any case the coefficients should be close enough to produce very very simular results. Just take the first 8 values and ignore the final 9th value which is always 1.0. I am not certain however if the vaules from gimp should be read row by row, or column by column without further input.

The best idea --- try it and see, then let us know!

Re: Question: Perspective and Coordinates ?

Posted: 2009-05-05T19:27:34-07:00
by fmw42
In any case the coefficients should be close enough to produce very very simular results. Just take the first 8 values and ignore the final 9th value which is always 1.0. I am not certain however if the vaules from gimp should be read row by row, or column by column without further input.
Anthony, unfortunately, my example shows that they do not appear to be close enough. But it may be this particular example in which the right side control points are rather close together. Thus adding or not adding 0.5 to each coordinate may make a significant difference in this example. A larger image with less distortion may work out better.

It is also possible that I did not get the corners to exactly match your coordinates and that may also have made the mismatch. Someone else could retry and see what they get.

My guess is that GIMP does not add 0.5 as you do in -distort perspective before computing the coefficients.

Thus I believe that one would be better off just reading the control points from GIMP while one is adjusting the corners and feeding this to -distort perspective.

Fred

Re: Question: Perspective and Coordinates ?

Posted: 2009-05-05T20:09:52-07:00
by anthony
[quote="fmw42"]My guess is that GIMP does not add 0.5 as you do in -distort perspective before computing the coefficients./quote]

Other way around. I don't add anything to the control points they should be in image coordinates not pixel positions.

Gimp I think does add 0.5 as it probably has user selection of a pixel position (which makes sense). EG: a selected control point should have 0.5 added before feeding to IM, as users select a particular pixel to move.

Image Registration Programs on the other had tend to select image coordinates.

But the only way to tell is to try it.