MATLAB Answers

How to find new coordinates of a point after image rotation?

조회 수: 5(최근 30일)
faran
faran 2021년 6월 8일
답변: Tarunbir Gambhir 2021년 6월 16일
I have an image that has a ROI inside it. I have the coordiantes for the ROI inside the image. Now I am rotating the image with 180 degree and wants to find the new coordinates for the image. I used this code:
P = [31961;29101]; % coordinates of point
alpha = 180; % 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;
And the output will be negative values of :
-31273
-28305
How to solve this issue?!

답변(1개)

Tarunbir Gambhir
Tarunbir Gambhir 2021년 6월 16일
I am not able to rectify the issue in your case since you have not provided the image. But you can see the results on a sample image below without any issue.
im = imread('peppers.png');
P = [100;200]; % coordinates of point within the image size
alpha = 180; % 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,1,2)/2)'; % Center of the main image
ImCenterB = (size(RotatedIm,1,2)/2)'; % Center of the transformed image
RotatedP = RotMatrix*(P-ImCenterA)+ImCenterB;
figure('Name','Before Rotation','NumberTitle','off');
imshow(im);
hold on
plot(P(2),P(1),'r+', 'LineWidth', 20);
plot(ImCenterA(2),ImCenterA(1),'k+', 'LineWidth', 20);
figure('Name','After Rotation','NumberTitle','off');
imshow(RotatedIm);
hold on
plot(RotatedP(2),RotatedP(1),'r+', 'LineWidth', 20);
plot(ImCenterB(2),ImCenterB(1),'k+', 'LineWidth', 20);
(note the change made in line 6,7 for the size function is because I used an RGB image)
The thing here to keep in mind is to reverse the coordinates when drawing/plotting on an image. This is the spatial image coordinates which uses the intrinsic coordinate system. You can find more information regarding it here.

Community Treasure Hunt

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

Start Hunting!

Translated by