如何在分离的图中绘制旋转的裁剪图像,以及我无法正确旋转的问题是什么(例如,k = 3或6)?
代码:
clear;
clc;
RGB = imread('pillsetc.png');
I = rgb2gray(RGB);
bw = imbinarize(I);
bw = bwareaopen(bw,30);
bw = imfill(bw,'holes');
imshow(bw)
[B,L] = bwboundaries(bw,'noholes');
imshow(bw)
[labeledImage, numBlobs] = bwlabel(bw);
for k = 1 : numBlobs
thisObject = ismember(labeledImage, k);
measurements = regionprops(thisObject, 'Orientation', 'BoundingBox');
croppedImage = imcrop(RGB, measurements.BoundingBox);
angle = measurements.Orientation
uprightImage = imrotate(croppedImage, angle);
imshow(uprightImage);
hold on
end
答案 0 :(得分:4)
问题在于,当你得到物体的角度时,如果你想让它们水平,你必须这样做:
uprightImage = imrotate(croppedImage, -angle);
而不是:
uprightImage = imrotate(croppedImage, angle);
如果我在Mario上运行此代码:
clear;
clc;
RGB = imread('Small-mario.png');
I = rgb2gray(RGB);
bw = imbinarize(I);
bw = bwareaopen(bw,30);
bw = imfill(bw,'holes');
figure;
imshow(bw)
[B,L] = bwboundaries(bw,'noholes');
figure;
imshow(bw)
[labeledImage, numBlobs] = bwlabel(bw);
for k = 1 : numBlobs
thisObject = ismember(labeledImage, k);
measurements = regionprops(thisObject, 'Orientation', 'BoundingBox');
croppedImage = imcrop(RGB, measurements.BoundingBox);
angle = measurements.Orientation;
uprightImage = imrotate(croppedImage, -angle);
figure;
imshow(uprightImage);
end
在那之后,马里奥的头是水平的: