我尝试使用以下脚本做一些事情:
填充图像,以便图像x和y尺寸相似,因此当我旋转对象时,它会适合并且不会被裁剪。我的padarray定义有什么问题?
为什么定向线没有穿过质心点?
如何将对象旋转90度?
clc;
clear;
close all;
url='https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRvbdGFenahz-NLr7xufhIAph5Z-WQ0y9qpOLQ4jdnylgC2OCVK';
Image = imread(url);
Image = rgb2gray(Image);
Image = imcomplement(Image);
[yDim,xDim] = size(Image);
if yDim>xDim
lineLength = yDim;
else
lineLength = xDim;
end
Image = padarray(Image, [lineLength+10 lineLength+10], 'both');
imshow(Image);
hold on;
%%%%%%Boundary plots
BW = imbinarize(Image);
[B,L] = bwboundaries(BW,'noholes');
k=1;
stat = regionprops(Image,'Centroid','Orientation','MajorAxisLength');
b = B{k};
yBoundary = b(:,2);
xBoundary = b(:,1);
centroidObject = stat(k).Centroid;
xCentre = centroidObject(:,2);
yCentre = centroidObject(:,1);
plot(yCentre, xCentre, 'b*')
orientationDegree = 0 - stat(k).Orientation
hlen = stat(k).MajorAxisLength/2;
cosOrient = cosd(stat(k).Orientation);
sinOrient = sind(stat(k).Orientation);
xcoords = xCentre + hlen * [cosOrient -cosOrient];
ycoords = yCentre + hlen * [-sinOrient sinOrient];
line(xcoords, ycoords,'Color',[1,0,0],'LineWidth',2);
%%%%Rotate object
plot(yBoundary, xBoundary, 'g', 'linewidth', 2);
pgon1 = polyshape(yBoundary, xBoundary); %create polyshape
poly1 = rotate(pgon1,90,centroidObject); %rotate polyshape
plot(poly1);