为什么我不能基于对象(喷射)边界框裁剪旋转的图像,并且有更有效的方法吗? 根据上一个问题:Image single background color - Matlab
代码:
close all;
clear;
clc;
url='http://www.clker.com/cliparts/T/i/o/c/X/Q/airplane-md.png';
rgbImage = imread(url);
I = rgb2gray(rgbImage);
BI = imbinarize(I);
LI = bwlabel(BI);
mea = regionprops(LI, 'Orientation');
RI = imrotate(rgbImage, -mea(1).Orientation,'loose');
imshow(RI);
% Replace all black pixels with white
inds = sum(RI,3)==0;
RI_new = RI;
RI_new(repmat(inds,1,1,3))=255;
imshow(RI_new);
title('RI new');
% Crop rotated image
RI_newGray = rgb2gray(RI_new);
BI_newGray = imbinarize(RI_newGray);
LI_new = bwlabel(BI_newGray);
LI_new= bwareaopen(LI_new, 50);
bbmea = regionprops(LI_new, 'BoundingBox');
rect = bbmea.BoundingBox;
RI_newGray = imcrop(RI_new, rect);
imshow(RI_newGray);
title('crop RI new');