图像旋转不正确 - Matlab

时间:2018-01-27 09:00:04

标签: matlab image-processing plot image-rotation

如何在分离的图中绘制旋转的裁剪图像,以及我无法正确旋转的问题是什么(例如,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  

1 个答案:

答案 0 :(得分:4)

问题在于,当你得到物体的角度时,如果你想让它们水平,你必须这样做:

uprightImage = imrotate(croppedImage, -angle);

而不是:

uprightImage = imrotate(croppedImage, angle);

如果我在Mario上运行此代码:

Original 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

在那之后,马里奥的头是水平的:

Horizontal Mario