MATLAB помогает иметь вершины, соответствующие области - PullRequest
0 голосов
/ 09 марта 2012

У меня есть программа в Matlab, которая рисует ограничивающий прямоугольник.Он отображает область каждого BLOB-объекта. Я расположил эти области в порядке убывания.Теперь я хочу иметь вершины X и вершины Y, соответствующие области, которую я расположил в порядке убывания, чтобы использовать ее дальше.Подскажите, пожалуйста, как это сделать?

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 голосов
/ 09 марта 2012

Посмотрите на функцию sort, встроенную в MATLAB

[B,IX]=sort(A) отсортирует вектор и вернет индексы, которые преобразуют A в отсортированный-A.

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

Это также позволит вам заменить алгоритм сортировки на что-то гораздо более простое.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...