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

4 views (last 30 days)
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,:)));

Accepted Answer

DGM
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
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1224457/image.png');
% 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.

More Answers (1)

Bora Eryilmaz
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)
atan2(x(1)-x(2), y(1)-y(2)) % In radians
180/pi*atan2(x(1)-x(2), y(1)-y(2)) % In degrees

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!