我没有测试过性能,但这里有一个算法来获得加权质心和面积.
这应该适用于Jacket以及matlab(取决于输入的类型).
function [WC, WA] = WeightedMoments(L, I);
[m, n] = size(L);
[keys, idx] = sort(L(:));
% Get weights and locations in sorted order
w = I(idx);
idx = idx - 1; % Convert to 0-index for simplicity
x = floor(idx / m);
y = idx - x * m;
% Location of the starting point of each region.
locs = find([diff(keys); 1]);
% Weighted area and locations
Msum = cumsum([w, x.*w, y.*w]);
Mloc = diff(Msum(locs,:));
M00 = Mloc(:, 1);
M10 = Mloc(:, 2);
M01 = Mloc(:, 3);
% Weighted centroids and areas
WC = [M10 ./ M00, M01 ./ M00] + 1; % Convert back to 1-index
WA = M00;
end
附:我是AccelerEyes的开发人员.抱歉延误.
编辑:稍微清理一下代码.