根据对象边界框裁剪旋转的图像-Matlab

时间:2019-07-07 12:25:18

标签: matlab

为什么我不能基于对象(喷射)边界框裁剪旋转的图像,并且有更有效的方法吗? 根据上一个问题: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');

1 个答案:

答案 0 :(得分:2)

替换:

bbmea = regionprops(LI_new, 'BoundingBox');

使用:

bbmea = regionprops(~LI_new, 'BoundingBox');

说明:

问题在于regionprops计算非零元素的组,在黑白逻辑图像中,函数计算白色像素的一组。要计算黑色像素的边界框,可以反转图像。

使用bbmea = regionprops(LI_new, 'BoundingBox');

Using LI_new

使用bbmea = regionprops(~LI_new, 'BoundingBox');

Using ~LI_new