ImageMagick and Transformation Matrix of Homography

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
User avatar
HunteX
Posts: 13
Joined: 2011-05-06T05:41:03-07:00
Authentication code: 8675308
Contact:

ImageMagick and Transformation Matrix of Homography

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

Re: ImageMagick and Transformation Matrix of Homography

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

Re: ImageMagick and Transformation Matrix of Homography

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
HunteX
Posts: 13
Joined: 2011-05-06T05:41:03-07:00
Authentication code: 8675308
Contact:

Re: ImageMagick and Transformation Matrix of Homography

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

Re: ImageMagick and Transformation Matrix of Homography

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
HunteX
Posts: 13
Joined: 2011-05-06T05:41:03-07:00
Authentication code: 8675308
Contact:

Re: ImageMagick and Transformation Matrix of Homography

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

Re: ImageMagick and Transformation Matrix of Homography

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