Page 1 of 1

ImageMagick and Transformation Matrix of Homography

Posted: 2011-05-07T09:23:43-07:00
by HunteX
Hi! I have transformation matrix of homography with 9 parameters. How can I apply this matrix to an image?
OpenCV have cvWarpPesrpective() function. Does ImageMagick similar function?

Re: ImageMagick and Transformation Matrix of Homography

Posted: 2011-05-07T09:33:38-07:00
by fmw42
From Wikipedia:

"Homography is a concept in the mathematical science of geometry. A homography is an invertible transformation from the real projective plane to the projective plane that maps straight lines to straight lines. Synonyms are collineation, projective transformation, and projectivity,[1] though "collineation" is also used more generally."

Therefore you appear to be wanting a perspective transformation.

IM can do that with -distort perspective. See http://www.imagemagick.org/Usage/distorts/#perspective

This is generally done with 4 conjugate pairs of control points.

However, I was under the impression that there was (or was going to be) a perspective projection distort that allowed the use of the actual coefficients (although you can use -fx to do that, but it is slow). Perhaps I am missing it on that page or misunderstood. Anthony would have to comment as that is his development area.

You can also do your own computations to convert your coefficients into the control points for the corners of the input and output images and then use -distort perspective. -fx will help you do that. see
http://www.imagemagick.org/Usage/transform/#fx_escapes
http://www.imagemagick.org/script/fx.php

You might also want to see my scripts 3Drotate and rotate3D below that allow perspective rotations by angles.

Re: ImageMagick and Transformation Matrix of Homography

Posted: 2011-05-09T21:14:11-07:00
by anthony
Perspective Projection takes a 3x3 matrix of 8 values. The 9th value is 1.0. If it is not, juts divide all the matrix values so that the 9th value does become a value of 1.0. Depending on how OpenCV defines its perspective matrix you may need to transpose (diagonally flip) the matrix values.

For details see...
http://www.imagemagick.org/Usage/distor ... projection

The matrix is the forward (source to destination) matrix. IM converts this to a reverse matrix for it internal use.
You can see the matrix that it calculates (from the forward projection, or from 4 coordinate pairs) using the verbose
option with distort.
See the very next section on perspective internals.

IM Perspective can handle Infinite tiled planes, and will use -mattecolor for areas that produce invalid results.

NOTE all coordinates used by Distort are image coordinates. That is 0,0 is the edge of the image, not the center of the top left pixel
http://www.imagemagick.org/Usage/distor ... oordinates

Re: ImageMagick and Transformation Matrix of Homography

Posted: 2011-05-10T10:15:01-07:00
by HunteX
Thanks, Fred, Anthony!

I applied the matrix on image

Code:

Code: Select all

InitializeMagick(*argv);
Image o2("o2.jpg");

double matrix[8];
matrix[0] = 0.8784113208323167;	matrix[1] = 0.4980249306365054;	matrix[2] = -157.6203276579947200;
matrix[3] = -0.4975379836532773;	matrix[4] = 0.8683160578912063;	matrix[5] = 89.0443436945734130;
matrix[6] = 0.0000207596600135;	matrix[7] = -0.0000083533323455;

o2.distort(DistortImageMethod::PerspectiveProjectionDistortion, 8, matrix, true);
o2.write("out.jpg");
And get this:
What a lines derived from the angles of the object??? In OpenCV all works well.
Image

Yet did so with the same result :( :

Code: Select all

double dots[16];
dots[0] = 0;	dots[1] = 0;
dots[2] = 0;	dots[3] = 293;
dots[4] = 0;	dots[5] = 552;
dots[6] = 275;	dots[7] = 775;
dots[8] = 596;	dots[9] = 552;
dots[10] = 793;	dots[11] = 474;
dots[12] = 596;	dots[13] = 0;
dots[14] = 519;	dots[15] = 0;

o2.distort(DistortImageMethod::PerspectiveDistortion, 16, dots, true);

Re: ImageMagick and Transformation Matrix of Homography

Posted: 2011-05-10T20:49:55-07:00
by anthony
The lines are where the corners of the source image touched the edge, and with the default Virtual-Pixels ('Edge' replication) the edge pixel colors became long lines of pixels spreading out from the image, which is then also distorted.

To remove them use a -virtual-pixel setting like black, white or gray. You can also use transparent, but you need to also include -alpha set operation to enable transparency.

Re: ImageMagick and Transformation Matrix of Homography

Posted: 2011-05-11T01:01:08-07:00
by HunteX
anthony wrote:The lines are where the corners of the source image touched the edge, and with the default Virtual-Pixels ('Edge' replication) the edge pixel colors became long lines of pixels spreading out from the image, which is then also distorted.

To remove them use a -virtual-pixel setting like black, white or gray. You can also use transparent, but you need to also include -alpha set operation to enable transparency.
Thank you!

Code:

Code: Select all

o2.virtualPixelMethod(BlackVirtualPixelMethod);
Image

Nice :)

Re: ImageMagick and Transformation Matrix of Homography

Posted: 2011-05-11T16:34:27-07:00
by anthony
If you want to specify a specific color set the background color you want and use the -virtual-pixel 'Background' method.

See Virtual Pixel handling
http://www.imagemagick.org/Usage/misc/#virtual-pixel