find top left x,y coordinates after +distort SRT rotate ?

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
ravikum
Posts: 34
Joined: 2013-03-20T05:35:37-07:00
Authentication code: 6789

find top left x,y coordinates after +distort SRT rotate ?

Post by ravikum »

Hello,

I am using the following code to rotate an image.

Code: Select all

$c='convert input -virtual-pixel white +distort SRT '20' +verbose output';
the red dot is at x=1, y=1 in original image.
how to find the coordinates of that red dot after rotation?

Image

After rotate, the size of the image is increased.
I don't want to crop the output to fit the size.

can you please suggest me how to find the top left x,y coordinates after +distort SRT rotate ?

Thanks.
(using the latest IM version on linux centos 6)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: find top left x,y coordinates after +distort SRT rotate

Post by fmw42 »

As long as the top left corner of your input image is not white, just find the location of the first non-white pixel in the resulting rotated image. For example in unix syntax:

Code: Select all

convert -size 100x100 xc:gray99 -virtual-pixel white +distort SRT '20' +repage txt:- |\
grep -v "white" | head -n 2 | tail -n +2 | sed -n 's/^\([0-9]*,[0-9]*\).*$/\1/p'
35,0
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: find top left x,y coordinates after +distort SRT rotate

Post by snibgo »

Looking at it a different way: the top-left triangle is a right-angled triangle, with diagonal equal to original image height H, bottom angle 20 degrees, so top distance is H * sin(20). Does that help?
snibgo's IM pages: im.snibgo.com
ravikum
Posts: 34
Joined: 2013-03-20T05:35:37-07:00
Authentication code: 6789

Re: find top left x,y coordinates after +distort SRT rotate

Post by ravikum »

Thanks for your replies.

when I printed the verbose result, it is like this :

Code: Select all

Array
(
    [0] => image JPEG 300x317 300x317+0+0 8-bit sRGB 17.9KB 0.000u 0:00.000
    [1] => Affine Projection:
    [2] =>   -distort AffineProjection \
    [3] =>       '0.939693,0.342020,-0.342020,0.939693,63.256300,-41.744302'
    [4] => Affine Distort, FX Equivelent:
    [5] =>   -size 392x403 -page -46-43 xc: +insert \
    [6] =>   -fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;
    [7] =>        xx=+0.939693*ii +0.342020*jj -45.164086;
    [8] =>        yy=-0.342020*ii +0.939693*jj +60.861741;
    [9] =>        v.p{ xx-v.page.x-.5, yy-v.page.y-.5 }' \
)
How can I use this, to find the top left coordinates of the result image ?

Thanks.
(using the latest IM version on linux centos 6)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: find top left x,y coordinates after +distort SRT rotate

Post by fmw42 »

Those parameters are for the reverse mapping that -fx would need to perform the same image processing. What you want is the forward mapping, which is the inverse of what is shown. So you would have to take those coefficients and put them in matrix form and then invert the matrix.

But the equations for rotation and translation (of any point) are well-known and can be found at
http://en.wikipedia.org/wiki/Transformation_matrix
http://www.cs.mtu.edu/~shene/COURSES/cs ... -tran.html

snibgo used a simplified form, since the y coordinate is known to be zero. However, I expect that his approach was just simple triangular geometry of the upper left white triangle formed from the background by the rotation. Length of the top is equal to original image height times sin(20 deg).
ravikum
Posts: 34
Joined: 2013-03-20T05:35:37-07:00
Authentication code: 6789

Re: find top left x,y coordinates after +distort SRT rotate

Post by ravikum »

Thanks for your reply.

x'=x cos(20) + y sin(20);
y'= -x sin(20) + y cos(20);

since I want to get the coordinates of x=1, y=1 and angle=20
x'= 0.93969262 + 0.34202014
y'= -0.34202014 + 0.93969262

but these values don't give the correct x, y coordinates after rotation.

can you please tell me how to calculate the x, y coordinates of top left after rotation ?
thanks.
(using the latest IM version on linux centos 6)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: find top left x,y coordinates after +distort SRT rotate

Post by fmw42 »

For the top left corner, it is y=0. (y increases downward in image coordinates). You have to adjust the equations for the change in y direction and to translate to the center of the image, use the rotation equations, then translate back to the new upper left corner again. So they get more complex that the simple equations listed earlier.

However for the upper left corner and +distort, the proper equations reduce to:

x=h*sin(angle)
y=0

where h=height of input image and angle is in degrees measured clockwise, if you calculator uses degrees. If it uses radians, then sin(angle*pi/360)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: find top left x,y coordinates after +distort SRT rotate

Post by anthony »

The -verbose output of -distort outputs two sets of values...
Forward mapping "projection" coefficents, and the FX equivelent reversed mapping that distort actually performs.

Perspective I have direct examples for...

For example (from the IM Exmaples page, Perspective Internals)
http://www.imagemagick.org/Usage/distor ... _internals

Code: Select all

Perspective Projection:
  -distort PerspectiveProjection \
      '1.430099, 0.246650, 3.000000, 0.147296, 
       1.434591, 0.000000, 0.006757, 0.009448'
Perspective Distort, FX Equivelent:
  -fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;
       rr=-0.004119*ii -0.005877*jj + 1;
       xx=(+0.711858*ii -0.108326*jj -2.135575)/rr;
       yy=(-0.073090*ii +0.699571*jj +0.219269)/rr;
       rr>0 ? p{ xx-page.x-.5, yy-page.y-.5 } : blue' \
The first set is the forward mapping values for the perspective matix, as used by "PerspectiveProjection"
in the order sx, ry, tx, rx, sy, ty, px, py
See PrespectiveProjection
http://www.imagemagick.org/Usage/distor ... projection
the other part is the reversed mapping algorithm using the inverse perspective matrix values in the for of a FX expression.
That is the equivelent of what the Distort is actually doing! But using px, py values first to generate teh divisor, then using the sx, ry, tx, rx, sy, ty values to map distination ii,jj to source xx,yy values (floating point)

Now the forward mapped values is what you need to map any specific point in the source image to the distination image.

This is exampled in Layed Images, Positioning Distorted Layer Images
http://www.imagemagick.org/Usage/layers/#layer_distort

I use an "awk" script to do the mathematical calculations, after extracting the values directly from the Verbose output (such as that shown above).



Affine (or SRT) is simplier as you do not have the Px and Py parts. (Affine is perspective with out changing scale)

From your previous posts the part you want is...

Code: Select all

-distort AffineProjection \
     '0.939693,0.342020,-0.342020,0.939693,63.256300,-41.744302'
This represents (as per Affine Projection)
the values... sx, rx, ry, sy, tx, ty as a forward mapping!

NOTE: the slight reordering of the tx and ty values. This differance is just a traditional ordering for Affine Matrix values, and is the same ordering used for a old IM setting and operation "-affine" & = "-transform" whcih "-distort replaced, but is still available.

Affine transforms are looked at in detail on a older set of IM Example pages that has become its own 'sub-page'.
Affine Matrix Transforms
http://www.imagemagick.org/Usage/distorts/affine/

Some mathematical examples are given on that page too, as well as much more detail of what each component actually does to images.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
ravikum
Posts: 34
Joined: 2013-03-20T05:35:37-07:00
Authentication code: 6789

Re: find top left x,y coordinates after +distort SRT rotate

Post by ravikum »

Hello,
Thanks for your reply.

code :

Code: Select all

$c='convert -verbose image.jpg -virtual-pixel white -verbose +distort SRT 20 +verbose image2.jpg';
my original image.
Image

rotated image :
Image

verbose output

Code: Select all

Array
(
    [0] => image.jpg JPEG 300x317 300x317+0+0 8-bit sRGB 17.9KB 0.000u 0:00.000
    [1] => Affine Projection:
    [2] =>   -distort AffineProjection \
    [3] =>       '0.939693,0.342020,-0.342020,0.939693,63.256300,-41.744302'
    [4] => Affine Distort, FX Equivelent:
    [5] =>   -size 392x403 -page -46-43 xc: +insert \
    [6] =>   -fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;
    [7] =>        xx=+0.939693*ii +0.342020*jj -45.164086;
    [8] =>        yy=-0.342020*ii +0.939693*jj +60.861741;
    [9] =>        v.p{ xx-v.page.x-.5, yy-v.page.y-.5 }' \
)
http://www.imagemagick.org/Usage/distorts/affine/
on this page, the equation is
( x', y' ) = ( x * sx + y * ry + tx, x * rx + y * sy + ty )

for a point on source image (0,0),
the destination image point for my image should be :
( x', y' ) = ( 0 * sx + 0 * ry + 63.256300, 0 * rx + 0 * sy + -41.744302 )
= 63.256300, -41.744302

however, for the rotated image, the top left coordinates are at around 111,0

why is the equation not giving the expected result?

Please suggest.

Thanks.
(using the latest IM version on linux centos 6)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: find top left x,y coordinates after +distort SRT rotate

Post by snibgo »

Those 6 parameters are correct for the "-" version of distort. The "+" version adjusts the image, in this case by dx=-46 and dy=-43. See line 5 of your verbose output. These should be subtracted from the result given by the formula.

63.256300 - (-46) = 109.256300
-41.744302 - (-43) = 1.255698
snibgo's IM pages: im.snibgo.com
ravikum
Posts: 34
Joined: 2013-03-20T05:35:37-07:00
Authentication code: 6789

Re: find top left x,y coordinates after +distort SRT rotate

Post by ravikum »

thanks,
all of you helped me a lot.
thanks for the correction snibgo
(using the latest IM version on linux centos 6)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: find top left x,y coordinates after +distort SRT rotate

Post by anthony »

It isn't the +distort that adjusts the position.

It is the removal of the virtual offset (the +repage) which you can see being added to the created output image in the FX equivelent formula... -page -46-43

Subtract the offset from the converted point... 63.256300, -41.744302 and you get... +109.256300, +1.255698
which is the exact coordinate (as a sub-pixel location) of the actual corner of the image.

NOTE; 0,0 is the exact corner!
It is NOT the center of the top-left pixel which in the source image is located at 0.5,0.5 which you are probably thinking it is.
that is why you 'rouch approximation' was at 111 across. and not at 109

However do not get the offset from the verbose output. I recommend you get it from the image generated. though they should be the same as the 'fx equivelent'.

Better still leave the image with the offset for further processing. As I showed in
Positioning Distorted Perspective Images
You should preserve virtual offsets until you have finished actually positioning the image.

Also when positioning images you should include final positioning information as part of the distortion as the distortion can translate images to sub-pixel accuricy, where as virtual offsets only can translate images in whole (intger amount) pixels.

That is why in the example link I gave the image is distorted twice. once to get the distortion calculations (with a minimal single pixel image, and a single pixel destination image), and then do the real distortion once offset calculations are complete.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
ravikum
Posts: 34
Joined: 2013-03-20T05:35:37-07:00
Authentication code: 6789

Re: find top left x,y coordinates after +distort SRT rotate

Post by ravikum »

Thanks all of you!
It is working now.
(using the latest IM version on linux centos 6)
Post Reply