필터 지우기
필터 지우기

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
Asi
Asi 2023년 10월 17일
OK. So how do I rotate it to a "B normal position"?
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