Reputation: 26069
Using Matlab's image processing toolbox I can find weighted centroids using the regionprops
function. This is because the function can return either a WeightedCentroid
or a list of pixels indices per labeled part of the image, by PixelList
, and then the weighted centroid is easily calculated. However, jacket's support in regionprops only returns an unweighted centroid (or the centroid of the binary "island" that was obtained using bwlabel
earlier). This means that the info regarding pixel positions was somehow used in order to find these centroids.
How can I access jacket's regionprops info regarding the list of pixels it uses to calculate the unweighted centroid so I could use it to calculate a weighted centroid?
(One important reason to do this is because the function find
cannot be used in a gfor
loop, otherwise one can find
the different output values of bwlabel
...)
Upvotes: 4
Views: 3107
Reputation: 12099
I have not tested performance, but here is an algorithm to get weighted centroids and areas. This should work with Jacket as well as matlab (depending on the type of Input).
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
P.S. I am a developer at AccelerEyes. Sorry about the delay.
EDIT: Cleaned up the code a little bit.
Upvotes: 3