Clear Filters
Clear Filters

Rotate an object based on regionprops Orientation, MinFeret and MaxFeret angles

1 view (last 30 days)
How can I rotate an object based on regionprops Orientation, MinFeret and MaxFeret angles?
My aim is that the object and the regionprops based plotted lines will be 0 or 90 degrees.
Thanks.
Code:
close all;
clear;
clc;
I = (insertText(zeros(150,150),[0 0],'B','BoxOpacity',0,'FontSize',70,'TextColor','w'));
I = imrotate(I,15);
I = rgb2gray(I);
figure('Name','Img');
imshow(I);
I=flipud(I);
BW = imbinarize(I);
[B,L] = bwboundaries(BW,'noholes');
k=1;
st = regionprops(I,"Centroid","MajorAxisLength","Orientation","MinFeretProperties","MaxFeretProperties");
b = B{k};
c = st(k).Centroid;
yBoundary = b(:,2);
xBoundary = b(:,1);
cy = c(:,2);
cx = c(:,1);
figure('Name','Boundary and lines');
plot(yBoundary, xBoundary, 'black', 'linewidth', 2);
hlen = st.MajorAxisLength/2;
xCentre = st.Centroid(1);
yCentre = st.Centroid(2);
cosOrient = cosd(st.Orientation);
sinOrient = sind(st.Orientation);
xcoords = xCentre + hlen * [cosOrient -cosOrient];
ycoords = yCentre + hlen * [-sinOrient sinOrient];
line(xcoords, ycoords, 'Color','red');
line([st.MaxFeretCoordinates(1),st.MaxFeretCoordinates(2)],[st.MaxFeretCoordinates(3),st.MaxFeretCoordinates(4)],'Color','blue');
line([st.MinFeretCoordinates(1),st.MinFeretCoordinates(2)],[st.MinFeretCoordinates(3),st.MinFeretCoordinates(4)],'Color','green');
orientationRotImg = imrotate(I, st.Orientation-90);
figure('Name','orientationRotImg');
imshow(orientationRotImg)
MaxFeretAngleRotImg = imrotate(I, st.MaxFeretAngle-90);
figure('Name','MaxFeretAngleRotImg');
imshow(MaxFeretAngleRotImg)
MinFeretAngleRotImg = imrotate(I, st.MinFeretAngle-90);
figure('Name','MaxFeretAngleRotImg');
imshow(MinFeretAngleRotImg)
  3 Comments
Walter Roberson
Walter Roberson on 18 Oct 2023
The min ferret angle and max ferret angle are not typically going to be 90 degrees from each other, so requiring that both be rotated to horizontal or vertical is not typically going to work.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 17 Oct 2023
Moved: Matt J on 17 Oct 2023
close all;
clear;
clc;
I = (insertText(zeros(150,150),[0 0],'B','BoxOpacity',0,'FontSize',70,'TextColor','w'));
I = imrotate(I,15);
I = rgb2gray(I);
figure('Name','Img');
imshow(I);
I=flipud(I);
BW = imbinarize(I);
[B,L] = bwboundaries(BW,'noholes');
k=1;
st = regionprops(I,"Centroid","MajorAxisLength","Orientation","MinFeretProperties","MaxFeretProperties");
MinFeretAngleRotImg = imrotate(I, st.MinFeretAngle);
figure('Name','MaxFeretAngleRotImg');
imshow(MinFeretAngleRotImg)
  1 Comment
Asi
Asi on 18 Oct 2023
Moved: Walter Roberson on 18 Oct 2023
Hi,
Thanks but it seems to me that the B should be flliped.
I tried the code with another letter "A"
I = (insertText(zeros(150,150),[0 0],'A','BoxOpacity',0,'FontSize',70,'TextColor','w'));
and this us the result of MinFeretAngleRotImg = imrotate(I, st.MinFeretAngle);
Any other ideas how to solve it also as a general solution for object (text, geometric shape, etc)?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!