【问题标题】:MATLAB help for having the vertices corresponding to area使顶点对应于区域的 MATLAB 帮助
【发布时间】:2025-12-22 16:35:12
【问题描述】:

我在 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)

【问题讨论】:

标签: matlab


【解决方案1】:

看看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);

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

【讨论】: