I have a binary mask, with let's say 40 images (Mask(:,:,40) and I want to calculate centroids per image.

1 visualización (últimos 30 días)
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.

Respuestas (1)

DGM
DGM el 31 de Dic. de 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)
All_centroids{1} = 221.5530 118.6445 221.5530 170.3555 All_centroids{2} = 222.5496 119.2282 222.5496 169.7718 All_centroids{3} = 219.6863 115.7635 219.6863 173.2365 All_centroids{4} = 218.7851 114.4577 218.7851 174.5423 All_centroids{5} = 214.9337 112.5646 214.9337 176.4354 All_centroids{6} = 215.4312 111.0686 215.4312 177.9314 All_centroids{7} = 214.8798 112.0566 214.8798 176.9434 All_centroids{8} = 214.5429 144.5000 All_centroids{9} = 214.2310 144.5000 All_centroids{10} = 212.1985 144.5000 All_centroids{11} = 214.0044 144.5000 All_centroids{12} = 213.4396 144.5000 All_centroids{13} = 213.7744 144.5000 All_centroids{14} = 213.5686 144.5000 All_centroids{15} = 213.0828 144.5000 All_centroids{16} = 212.9977 144.5000 All_centroids{17} = 212.9637 144.5000 All_centroids{18} = 212.9661 144.5000 All_centroids{19} = 212.7378 144.5000 All_centroids{20} = 213.3489 144.5000 All_centroids{21} = 213.3154 144.5000 All_centroids{22} = 213.7434 144.5000 All_centroids{23} = 214.5903 112.0145 214.5903 176.9855 All_centroids{24} = 215.5823 112.0156 215.5823 176.9844 All_centroids{25} = 216.1676 111.6308 216.1676 177.3692 All_centroids{26} = 216.2678 113.1700 216.2678 175.8300 All_centroids{27} = 218.0549 113.4860 218.0549 175.5140 All_centroids{28} = 216.7306 115.2095 216.7306 173.7905 All_centroids{29} = 219.0463 114.4316 219.0463 174.5684 All_centroids{30} = 218.8644 115.1333 218.8644 173.8667 All_centroids{31} = 217.9833 115.4503 217.9833 173.5497

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by