MATLAB: Multiple ROI within one slice median calculation

multiple roi calculationmultiple roi mask within one slice median calculation

Hi all,
I have a mask file 192x192x10, within each slice there are multiple ROIs with mask = 1 and background = 0. I then use bsxfun to apply this mask to an image 192x192x17 with values between [0,1]. What I'm trying to calculate is the median value of each individual ROIs within each slice. Is there an easy way to calculate the median value of each individual ROIs?
Many thanks!
Fiona

Best Answer

I think this is what you want:
I=reshape(Image,[],17);
clear Medians
for i=1:10 %loop over masks
S=regionprops(mask(:,:,i),'PixelIdxList');
J=numel(S);
for j=J:-1:1 %loop over ROIs
Medians{i}(:,j)=median(I(S(j).PixelIdxList,:),1);
end
end
Related Question