# I want to rotate the rotated image in the forward direction. How do I do this?

4 views (last 30 days)
채호 박 on 8 Dec 2022
Commented: 채호 박 on 9 Dec 2022
I know I need to rotate the picture with imrotate. However, imrotate rotates based on the center point, so I need to know the angle of the picture, but I don't know how to write it in code. I want to find the angle after inputting three points (red circles) with the mouse. (Picture description: Rotate the rotated image of picture 1 by the corresponding angle to make it look like picture 2) Below is the code to obtain the angles of three input points through ginput(3).
[x, y] = ginput(3);
app.UIFigure.HandleVisibility = fhv;
x = round(x);
y = round(y);
z=[x,y];
d=diff(z);
Angle=acos(dot(-d(1,:),d(2,:))/norm(d(1,:))/norm(d(2,:)));
Bora Eryilmaz on 8 Dec 2022
You actually only need two points to determine the angle of rotation needed.

DGM on 8 Dec 2022
Edited: DGM on 8 Dec 2022
For this specific image, you could also try calculating the angles by finding certain features.
For what it's worth:
% the initial image
% find the red circles in HSV, create a mask
inhsv = rgb2hsv(inpict);
mk = inhsv(:,:,2) > 0.1;
% get the centers of the red circles
S = regionprops(mk,'centroid');
C = vertcat(S.Centroid);
% reorder them
C = C([2 1 3],:);
% find the angles between adjacentpairs
th = atan2d(diff(C(:,2)),diff(C(:,1)));
th = mod(th,90)
th = 2×1
7.1944 6.9314
% just use the mean
th = mean(th);
% rotate the image
% i'm going to be lazy and rotate the whole composite image
outpict = imrotate(inpict,th);
% show it
imshow(outpict) Note that this is a drawn image. The red circles are not accurately on the vertices, so we should expect some error. While it's likely that the nominal rotation is supposed to be 7 degrees, it's also likely that whoever drew it used an inaccurate method of rotating the object (grab & drag versus direct transform). Even the person who drew it might not know that it's not exactly 7 degrees.
채호 박 on 9 Dec 2022
This is really amazing code. Once again I am in awe of matlab.

Bora Eryilmaz on 8 Dec 2022
Select upper point and then lower point using the code below:
line([0.1 0.2], [0.1 0.2]) % Should give a 45 degree line
[x y] = ginput(2)
180/pi*atan2(x(1)-x(2), y(1)-y(2)) % In degrees
채호 박 on 9 Dec 2022
Thank you very much for your dedication.

### Categories

Find more on ROI-Based Processing in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!