How can I get the new coordinates of a point after rotation of an image (2D)?

9 visualizzazioni (ultimi 30 giorni)
I have got the coordinates of a point (P) in my main image (2D). The main image (im) is now rotated using imrotate and the angle alpha. Now, I want to know what the coordinates of P are in the rotated image.
P = [5;8]; % coordinates of point
alpha = 45; % angle for rotation
RotatedIm = imrotate(im,alpha); % rotation of the main image (im)
RotMatrix = [cosd(alpha) -sind(alpha); sind(alpha) cosd(alpha)];
ImCenter = (size(im)/2)'; % Center of the main image
RotatedP = RotMatrix*(P-ImCenter)+ImCenter; % Rotation of point P over center of the image with angle alpha
The rotatedP coordinates are not the right ones. The problem is that there are zeros added on the borders of RotatedIm. I think that that is the reason why the coordinates of RotatedP are not correct. Can someone help me to find the coordinates of point P in the rotated image?
  4 Commenti
Marco A.
Marco A. il 20 Gen 2025
Spostato: Matt J il 20 Gen 2025
Dear @was, @Matt J and @Rik,
I know this is a thread from more than 5 years ago but, after transformation, I encountered a +/-1 px shift between the image and the coordinates of the points (when they are at the corners of the image input). In this script, I use the corners of the input to re-locate an extracted (and transformed) patch in its original position (before the transformation). I have managed to improve the shift to a minimum.
See attached.
I hope this script helps someone trying to reconstruct and image from its pieces. :)
Cheers,
Marco
Matt J
Matt J il 20 Gen 2025
That might be because the OP defined the center of the rotation as
ImCenter = (size(im)/2)';
when really, in 1-based pixel coordinates, the geometric center of the image grid would be at
ImCenter = ((size(im)+1)/2)';

Accedi per commentare.

Risposta accettata

Matt J
Matt J il 3 Ott 2018
Modificato: Matt J il 3 Ott 2018
P = [5;8]; % coordinates of point
alpha = 45; % angle for rotation
RotatedIm = imrotate(im,alpha); % rotation of the main image (im)
RotMatrix = [cosd(alpha) -sind(alpha); sind(alpha) cosd(alpha)];
ImCenterA = (size(im)/2)'; % Center of the main image
ImCenterB = (size(RotatedIm )/2)'; % Center of the transformed image
RotatedP = RotMatrix*(P-ImCenterA)+ImCenterB;

Più risposte (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by