I have a binary mask, with let's say 40 images (Mask(:,:,40) and I want to calculate centroids per image.
1 view (last 30 days)
Show older comments
I have a binary mask (see attached) with let's say 40 images and I want to calculate and save the centroids across every image. I am trying to do something like:
for i=1:p
centroid=cell2mat(struct2cell(regionprops(FCombinedmask(:,:,p),'Centroid')));
centroid
All_centroids(p,1)=centroid(1,1);All_centroids(p,2)=centroid(1,2);
end end
However, it only gives me the centroid in X and Y coordinates of the last binary image, whilst I want the centroids across all images. I would appreciate any help.
0 Comments
Answers (1)
DGM
on 31 Dec 2023
You aren't using the loop index for anything, so nothing changes.
You don't appear to be preallocating anything.
Your entire setup assumes that there is only one blob in each frame when there's no assurance that there is.
Start over.
% contains a 288x384x31 logical image
% with only one blob per slice
load FMask.mat
% modify the test image for the sake of demonstration
% now we can't presume that there is only one blob per slice
FCombinedmask = FCombinedmask | flipud(FCombinedmask);
% if you're not keeping track of what your variables mean
% then use variable names which actually mean something on their own
nframes = size(FCombinedmask,3);
% preallocate
All_centroids = cell(nframes,1);
% do the thing
for f = 1:nframes
% actually use the loop index somewhere for something
% don't presume the number of blobs if you have no means to ensure that
S = regionprops(FCombinedmask(:,:,f),'Centroid'); % get the struct
All_centroids{f} = vertcat(S.Centroid); % extract each list of centroids
end
% number of centroids differ
celldisp(All_centroids)
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!