Consecutive SRT distorts

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

Re: Consecutive SRT distorts

Post by fmw42 »

concatenation of pure rotations (without the translate for the center) is just matrix multiplication.

see http://en.wikipedia.org/wiki/Rotation_m ... iplication
emery

Re: Consecutive SRT distorts

Post by emery »

fmw42 wrote:
The first rotation about the center doesn't change the center.
Yes it does, if you rotate about the center, then afterwards as the image is bigger the new center will be further from the upper left corner. So to do the second rotation about the center, you need to specify a new center as well.
I'm not using bestfit, the image is always going to be within a container large enough to encompass its rotation, or expect it to extend beyond the edges. So the center remains the same. Look at the second example images. The 40 degree rotation doesn't change the center. Then the following 10 degree rotation at the same center goes haywire. I didn't manually add that white background, that is the "canvas" image which I am rotating. The fish is composited into the position I want it and then the rotation pivot point is the center of the fish image within the canvas (200,172). Could this cause a problem?

Edit: I dumped getImageWidth and getImageHeight after the transformation and the size definitely doesn't change.
fmw42 wrote:concatenation of pure rotations (without the translate for the center) is just matrix multiplication.
see http://en.wikipedia.org/wiki/Rotation_m ... iplication
Then what else could be going wrong? My matrix multiplication seems accurate. :\
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Consecutive SRT distorts

Post by fmw42 »

your matrix has both rotation and translation in it. you cannot just multiply them. you must multiply the rotations only and figure out the total translation you want. You have a 3x2 matrix. Multiplication in 2D is 2x2.

Show me the formula you use in terms of sines and cosines of angles etc for each term of your matrix.

If you keep the image the same size and rotate about the center, then multiply the 2x2 matrix parts for the rotation, then append the translation for the centering.

Or translate so center at upper left corner (0,0), apply as many rotates as you want by matrix multiply of 2x2 matrix, then translate back to center.

some references:

do a google search on concatenate affine transformations

http://developer.apple.com/documentatio ... ffine.html

http://books.google.com/books?id=SUVClx ... q=&f=false

http://books.google.com/books?id=SUVClx ... q=&f=false
Last edited by fmw42 on 2009-08-11T16:29:42-07:00, edited 1 time in total.
emery

Re: Consecutive SRT distorts

Post by emery »

This is the code:

Code: Select all

  public function rotate($degrees, $fromX='full/2+sl', $fromY='full/2+st') {
      $px = $this->parseShapeLength($fromX, $this->getWidth());
      $py = $this->parseShapeLength($fromY, $this->getHeight());   
      $degrees = $this->parseShapeLength($degrees);

      $rads = deg2rad($degrees);
      $c = cos($rads);
      $s = sin($rads);

      $sx = $c;
      $rx = $s;
      $ry = -$s;
      $sy = $c;

      $tx = $px;
      $ty = $py;

      $tx -= $px*$c - $py*$s;
      $ty -= $px*$s + $py*$c;

      $affine = array(
         $sx,$rx,
         $ry,$sy,
         $tx,$ty);

      $this->affineMatrix = $this->matrixMult($this->affineMatrix, $affine, 3, 2);

   }

How do I calculate the aforementioned cumulative translation?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Consecutive SRT distorts

Post by fmw42 »

I don't want your code. I want the math formula to see what you are doing.

Study the references that I just gave you. Try to separate the translate from the rotate. Translate the center of rotation to the upper left corner, apply each rotation, then translate the upper left corner back to the center of the image.

As I said before, you cannot matrix multiply 2x3 matrices for the full transformation including the translations. You can separate out the rotation matrices and the translation matrices and the scaling matrices, then do matrix multiply to combine them all.

Anthony was saying it correctly in his first post to you:

"All SRT distorts are linear. They are simply a sub-set of Affine (Matrix) distortions.
When you combine such distortions together you eventually get a single Affine Distortion
That is a combined rotate,shear,scale,translate distortion (one and only one of each).

The way to combine the distortions is to start with a 'null' or 'unity' matrix, and then matrix multiply each of the individual distortions you want to apply in the sequence wanted. At the end you will have a single affine matrix (6 values) that you can apply ONCE to an image, using AffineProjection."

However, if you first translate to the origin, then do all rotations and scalings and then translate back to wherever you want the center to be, you can do multiply operattions via matrix multiply.
emery

Re: Consecutive SRT distorts

Post by emery »

Actually I simply needed to add an extra column to the matrix to make it 3x3 and it works beautifully. One matrix multiply on the whole thing.

Image


Why does it work?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Consecutive SRT distorts

Post by anthony »

Fred, it is only a bigger image if you used +distort.

Yes matrix multiplication is a 3x3 matrix. The only reason 6 values are give is the other three are constant for affine transformations.

However you affine matrix calculation may be wrong.

The inital 40 degree rotation produces...

Code: Select all

convert originalh.png -verbose -distort SRT 40 +verbose show:

Code: Select all

0.766044,0.642788
-0.642788,0.766044
 182.484,-101.371
The rotation/scale components are right but the final translation isn't!
Though that may depend on what you regard as being the 'center' of the image

The 10 degree rotation about center which you want to multiply with the first is VERY different.

Code: Select all

 convert originalh.png -verbose -distort SRT 10 +verbose show:

Code: Select all

0.984808,0.173648,
-0.173648,0.984808,
38.2314,-36.9875
As the centers are the same, and rotations just add, multiplying the baove two should produce...

Code: Select all

convert originalh.png -verbose -distort SRT 50 +verbose show:

Code: Select all

0.642788,0.766044,
-0.766044,0.642788,
235.546,-105.131
If anything this may be the reason your initial example did not produce what you were expected!!!!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
emery

Re: Consecutive SRT distorts

Post by emery »

Thanks a ton Anthony. Actually, that last image I posted is the intended result. I was trying to arbitrate the pivot point, and in that example each of those seperate rectangles are rotated about the center, and then the whole thing is rotate about 0,0. The example demonstrates a purple-blue gradient rectangle image and the ImagickDraw vector rectangles which both independently affine transformed and produce the same results (overlapping). =)
Post Reply