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
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
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)
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!