-1

我正在使用带有二值图像的轮廓函数。我很烦恼如何获得图像中不同表面的面积和质心,需要这个任务来对物体进行分类。

4

1 回答 1

1

您需要使用Contour Matrix 输出

这是一个例子:

function data = ContourInfo(C)

data = [];

if isempty(C)
    return
end

k = 1;
j = 1;
while j < size(C,2);
    data(k).numxy = C(2,j);
    data(k).x = C(1,j+1:j+data(k).numxy);
    data(k).y = C(2,j+1:j+data(k).numxy);
    data(k).level = C(1,j);

    [data(k).centroid(1) data(k).centroid(2) data(k).area] = ... 
          polycentroid(data(k).x, data(k).y);
    data(k).area = polyarea(data(k).x, data(k).y);
    data(k).centroid = polycentroid(data(k).x, data(k).y);

    j = j + data(k).numxy + 1;
    k = k+1;
end

function [x0,y0,a] = polycentroid(x,y)

[m1,n1] = size(x); [m2,n2] = size(y);
n = max(m1,n1);
x = x(:); y = y(:);
x2 = [x(2:n);x(1)];
y2 = [y(2:n);y(1)];
a = 1/2*sum (x.*y2-x2.*y);
x0 = 1/6*sum((x.*y2-x2.*y).*(x+x2))/a;
y0 = 1/6*sum((x.*y2-x2.*y).*(y+y2))/a;

调用如下:

Z = peaks(20);
[C, h] = contourf(Z,10);
contourData = ContourInfo(C)

disp('Area of contour 1:');
disp(contourData(1).area
disp('Centroid of contour 1:');
disp(contourData(1).centroid);
于 2015-10-26T00:49:29.013 回答