MATLAB帮助使顶点对应于区域

时间:2012-03-09 10:23:18

标签: matlab

我在matlab中有一个绘制边界框的程序。它显示每个blob的区域。我按降序排列了区域。  现在我想让verticesX和vertixesY对应于我按降序排列的区域以进​​一步使用它。你可以告诉我该怎么做吗?

clear all;

close all;

clc

I=imread('image.jpg');
......
bw2=im2bw(J(:,:,2),L);

subplot(2,3,4);
imshow(bw2);

% Label each blob so we can make measurements of it

[labeledImage numberOfBlobs] = bwlabel(bw2, 8);

% Get all the blob properties.

blobMeasurements = regionprops(labeledImage, 'BoundingBox','Area');

allBlobAreas = [blobMeasurements.Area];

% Loop through all blobs, putting up Bounding Box.
hold on; 

for k = 1 : numberOfBlobs

boundingBox = blobMeasurements(k).BoundingBox;   % Get box.

x1 = boundingBox(1);

y1 = boundingBox(2);

x2 = x1 + boundingBox(3) - 1;

y2 = y1 + boundingBox(4) - 1;

verticesX = [x1 x2 x2 x1 x1];

verticesY = [y1 y1 y2 y2 y1];

% Calculate width/height ratio.

aspectRatio(k) = boundingBox(3) / boundingBox(4);

fprintf('\n For blob #%d, area = %d, aspect ratio = %.2f\n' ,k, allBlobAreas(k), aspectRatio(k));

fprintf('\n VerticesofX=[%.2f %.2f %.2f %.2f %.2f],VerticesofY=[%.2f %.2f %.2f %.2f %.2f]\n',verticesX,verticesY);

%% Loop for having area in descending order
x(k)=allBlobAreas(k);

for i=1:length(x)-1

for j=i+1:length(x)

if x(i)<x(j)

c=x(i);

x(i)=x(j);

x(j)=c;

end
end
end
end
%% Displays area in descending order
disp(x)

1 个答案:

答案 0 :(得分:0)

查看the sort function built-in to MATLAB

[B,IX]=sort(A)将对矢量进行排序并返回将A转换为sorted-A的索引。

[B,IX]=sort(allBlobAreas,'descend');
sortedAreas=B; % equivalent to sortedAreas=allBlobAreas(IX);
sortedVerticesX=verticesX(IX);
sortedVerticesY=verticesY(IX);

这也可以让你用更简单的东西替换排序算法。