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

1 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2023-10-18
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.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2023-10-17
移动:Matt J 2023-10-17
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 个评论
Asi
Asi 2023-10-18
移动:Walter Roberson 2023-10-18
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)?

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by