new coordinates of a point after image rotation
new coordinates of a point after image rotation
Consider an image of breadth 'b' pixel and length 'l' pixel.
If this image is rotated clockwise by angle theta then coordinates of point (x,y) become (x cos theta  y sin theta , y cos theta + x sin theta).
For an image where each x,y is an integer such that x belongs to {0,1,...,b1} and y belongs to {0,1,...,l1}
So pixel x,y becomes ( (l1) sin theta + x cos theta  y sin theta , y cos theta + x sin theta )
Is above statement true ?
Thanks.
If this image is rotated clockwise by angle theta then coordinates of point (x,y) become (x cos theta  y sin theta , y cos theta + x sin theta).
For an image where each x,y is an integer such that x belongs to {0,1,...,b1} and y belongs to {0,1,...,l1}
So pixel x,y becomes ( (l1) sin theta + x cos theta  y sin theta , y cos theta + x sin theta )
Is above statement true ?
Thanks.
 fmw42
 Posts: 23831
 Joined: 20070702T17:14:5107:00
 Authentication code: 1152
 Location: Sunnyvale, California, USA
Re: new coordinates of a point after image rotation
I think that is only true when rotating about the origin at x=y=0. But in Imagemagick, the original is at the top left corner. So you have to change the x,y values to offset them to the center before rotation, then shift them back to the new origin (top left corner) after the rotation.

 Posts: 10696
 Joined: 20100123T23:01:3307:00
 Authentication code: 1151
 Location: England, UK
Re: new coordinates of a point after image rotation
IM has multiple methods of rotating an image. The "most pure" method is "distort SRT 0,0,n" where n is the degrees of rotation. This rotates about (0,0), which is topleft, eg:
The red dot moves to the predicted location, (x * cos (theta)  y * sin (theta) , y * cos (theta) + x * sin (theta)).
Another rotation operation is "rotate n", which rotates around the centre. The same prediction can be used, but first subtract (width/2,height/2) than add it at the end.
Code: Select all
magick size 200x200 xc:#8f8 fill Red draw "point 50,20" virtualpixel Black distort SRT 0,0,20 +repage pure_rot.png
The red dot moves to the predicted location, (x * cos (theta)  y * sin (theta) , y * cos (theta) + x * sin (theta)).
Another rotation operation is "rotate n", which rotates around the centre. The same prediction can be used, but first subtract (width/2,height/2) than add it at the end.
snibgo's IM pages: im.snibgo.com
Re: new coordinates of a point after image rotation
(Question 1)Can i say that first pixel on top left of image is regarded as 0,0 in image magick . and horizontal axis is x while vertical is y , so bottom right pixel of a 512x512 image will be (511,511)?
(Question 2)Now when I run the command
convert input_image.png rotate n rotated_image.png
where n is a number denoting degree of rotation in clockwise direction.
Then , rotation takes place about the center of image.
Here is my logic about effect of rotation on a particular point in image  https://ibb.co/dqZr2z .
So , I should calculate x and y after subtracting width/2 and height/2 respectively from pixel coordinates of imagemagick.
Now new_x should be calculated using formula mentioned in image .
The pixel coordinates of point after rotation can be found out by adding width/2 and height/2 to new_x and new_y respectively.
Is that right ?
Thanks.
(Question 2)Now when I run the command
convert input_image.png rotate n rotated_image.png
where n is a number denoting degree of rotation in clockwise direction.
Then , rotation takes place about the center of image.
Here is my logic about effect of rotation on a particular point in image  https://ibb.co/dqZr2z .
So , I should calculate x and y after subtracting width/2 and height/2 respectively from pixel coordinates of imagemagick.
Now new_x should be calculated using formula mentioned in image .
The pixel coordinates of point after rotation can be found out by adding width/2 and height/2 to new_x and new_y respectively.
Is that right ?
Thanks.

 Posts: 10696
 Joined: 20100123T23:01:3307:00
 Authentication code: 1151
 Location: England, UK
Re: new coordinates of a point after image rotation
Yes, and yes.
But ... although that is true of pixel coordinates (which are always discrete integers), image coordinates are continuous floatingpoint, eg pixel coordinates are 0 to 511 but image coordinates are between 0.0 and 512.0. "distort SRT" works in image coordinates, so we should use "distort SRT 0.5,0.5,20" if we want the topleft pixel to remain in the same position.
But ... although that is true of pixel coordinates (which are always discrete integers), image coordinates are continuous floatingpoint, eg pixel coordinates are 0 to 511 but image coordinates are between 0.0 and 512.0. "distort SRT" works in image coordinates, so we should use "distort SRT 0.5,0.5,20" if we want the topleft pixel to remain in the same position.
snibgo's IM pages: im.snibgo.com
Re: new coordinates of a point after image rotation
I get that a pixel in original image defined by an integer won't necessarily map to another integer coordinate .
I think if image is square shape and rotation angle is 90 degrees then i will get integer coordinates .
In other cases i am okay with slight error .
I don't want to skew the image just simple rotation so 'distort' option may not be useful in my case .
I think if image is square shape and rotation angle is 90 degrees then i will get integer coordinates .
In other cases i am okay with slight error .
I don't want to skew the image just simple rotation so 'distort' option may not be useful in my case .
Re: new coordinates of a point after image rotation
I saw that rotating by multiple 90 degrees is most simple case which I have verified.
But when image is rotated by any other agnle then rotated image has higher dimension than original one .
In that case , what should be formula for new dimension of image ?
Also , is it that I have to subtract width/2 and height/2 of original image to get x and y respectively then add new_width/2 and new_height/2 to new_x and new_y respectively to get final coordinates ?
Thanks.
But when image is rotated by any other agnle then rotated image has higher dimension than original one .
In that case , what should be formula for new dimension of image ?
Also , is it that I have to subtract width/2 and height/2 of original image to get x and y respectively then add new_width/2 and new_height/2 to new_x and new_y respectively to get final coordinates ?
Thanks.

 Posts: 10696
 Joined: 20100123T23:01:3307:00
 Authentication code: 1151
 Location: England, UK
Re: new coordinates of a point after image rotation
For the command I showed, the dimensions don't change. If you are using some other command, please show it.manit wrote:But when image is rotated by any other agnle then rotated image has higher dimension than original one .
It depends on your command. You also need to understand the canvas size and offset, eg:manit wrote:Also , is it that I have to ...
Code: Select all
magick rose: rotate 30 info:
rose: PNM 86x76 86x76815 8bit sRGB 0.016u 0:00.046
snibgo's IM pages: im.snibgo.com
Re: new coordinates of a point after image rotation
I am running following command
convert fp.png rotate 15 fprotated15degree.png
In this case fp.png is 512x512 (https://ibb.co/f8YvZe) while fprotated15degree.png turns out to be 630x630 (https://ibb.co/dST7fK) .
convert fp.png rotate 15 fprotated15degree.png
In this case fp.png is 512x512 (https://ibb.co/f8YvZe) while fprotated15degree.png turns out to be 630x630 (https://ibb.co/dST7fK) .

 Posts: 10696
 Joined: 20100123T23:01:3307:00
 Authentication code: 1151
 Location: England, UK
Re: new coordinates of a point after image rotation
The basic formulae are:
new_width = w*cos(ang) + h*sin(ang)
new_height = h*cos(ang) + w*sin(ang)
I think IM rounds these up to the nearest integers ("ceiling"), and adds 2.
new_width = w*cos(ang) + h*sin(ang)
new_height = h*cos(ang) + w*sin(ang)
I think IM rounds these up to the nearest integers ("ceiling"), and adds 2.
snibgo's IM pages: im.snibgo.com
Re: new coordinates of a point after image rotation
512 * (cos15 + sin 15) = 627.06
So , I will try to find correspondence for a point in 512x512 image to its 15 degree rotated (clockwise) variant by following steps
Consider example of point 13,366 in 512x512 image.
So its x=13(512/2)=243 , y=366(512/2)=110
new_x (according to https://ibb.co/dqZr2z) = 243 cos 15 + 110 sin 15 = −206.249880827 (note theta in our case is 15 degree)
new_y = 110 cos 15  (243) sin 15 = 169.144868852
206+315=109
169+315=484
so new coordinates of 13,366 is (109,484)
Distance of 13,366 from 256,256 is 266.737698873
Distance of 109,484 from 315,315 is 266.45262243
Makes me think . I am doing right .
Is that okay ?
Is there a quick way to check like making a red dot on white 512x512 canvas then searching for red dot in rotated image ?
Thanks.

 Posts: 10696
 Joined: 20100123T23:01:3307:00
 Authentication code: 1151
 Location: England, UK
Re: new coordinates of a point after image rotation
Code: Select all
magick size 512x512 xc:#8f8 fill Red draw "point 13,366" virtualpixel Black rotate 15 +write info: +repage p1.png
Of course, you could do this with IM, but it's worth looking at the image to check it is as you intended.
snibgo's IM pages: im.snibgo.com
Re: new coordinates of a point after image rotation
The above calculation I posted is wrong.manit wrote: ↑20180905T08:43:3007:00512 * (cos15 + sin 15) = 627.06
So , I will try to find correspondence for a point in 512x512 image to its 15 degree rotated (clockwise) variant by following steps
Consider example of point 13,366 in 512x512 image.
So its x=13(512/2)=243 , y=366(512/2)=110
new_x (according to https://ibb.co/dqZr2z) = 243 cos 15 + 110 sin 15 = −206.249880827 (note theta in our case is 15 degree)
new_y = 110 cos 15  (243) sin 15 = 169.144868852
206+315=109
169+315=484
so new coordinates of 13,366 is (109,484)
Distance of 13,366 from 256,256 is 266.737698873
Distance of 109,484 from 315,315 is 266.45262243
Makes me think . I am doing right .
Is that okay ?
Is there a quick way to check like making a red dot on white 512x512 canvas then searching for red dot in rotated image ?
Thanks.
I figured out the right one from fmw42 advise 
It should be as followsI think that is only true when rotating about the origin at x=y=0. But in Imagemagick, the original is at the top left corner. So you have to change the x,y values to offset them to the center before rotation, then shift them back to the new origin (top left corner) after the rotation.
Suppose pixel location is (a,b) in original image
then shifting origin from topleft to center makes pixel coordinate (ah_res/2,v_res/2b)
Now use formula x_new=x cos theta + y sin theta , y_new= y cos theta  x sin theta
(NOTE  theta is angle of rotation (in degrees) in clockwise direction which is default for rotate option)
Now move origin back to top left . So final coordinates become  (x_new+new_h_res/2 , new_v_res/2  y_new)
Here h_res = horizontal resolution , v_res = vertical resolution.
Thanks.