Getting weighted centroids using regionprops in matlab + jacket

Using Matlab's image processing tools, I can find weighted centroids using the regionprops function. This is because the function can return either a WeightedCentroid or a list of pixel indices on the marked part of the image, on the PixelList , and then the weighted centroid is easily calculated. However, jacket support in regionprops only returns an unweighted centroid (or the binary "island" centroid that was obtained using bwlabel earlier). This means that the pixel position information was somehow used to find these centroids.

How can I access the region’s region information regarding the list of pixels that it uses to calculate the unweighted center of gravity so that I can use it to calculate the weighted centroid? (One of the important reasons for this is that the find function cannot be used in the gfor loop, otherwise find can use different bwlabel output values ​​...)

+4
source share
1 answer

I have not tested performance, but here is an algorithm for obtaining weighted centroids and areas. This should work with Jacket as well as with 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 

PS I'm a developer at AccelerEyes. Sorry for delay.

EDIT : cleared the code a bit.

+3
source

Source: https://habr.com/ru/post/1445130/


All Articles