Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
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 Kommentare
Asi
Asi am 17 Okt. 2023
OK. So how do I rotate it to a "B normal position"?
Walter Roberson
Walter Roberson am 18 Okt. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 17 Okt. 2023
Verschoben: Matt J am 17 Okt. 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 Kommentar
Asi
Asi am 18 Okt. 2023
Verschoben: Walter Roberson am 18 Okt. 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)?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by