Calculate the mean of nonzero pixels
6 views (last 30 days)
Show older comments
I have a 3D volume and a 3D binary mask. I want to calculate the mean of the pixels which correspond to the regions where the mask is equal to 1.
I thought to calculate the volume with the mask in order to have only the regions I am interested and then I calculated the mean.
I wrote the following piece of code but the mean value is extremely small and I think it's because I am taking into account the whole 3D volume (even the areas where the volume is zero) and not only the regions which I am interested.
r=volume;
for i=1:size(r,1)
for j=1:size(r,2)
for k=1:size(r,3)
masked_volume(i,j,k) = volume(i,j,k)*mask(i,j,k);
end
end
end
mean = mean(masked_volume(:))
1 Comment
Guillaume
on 24 Oct 2019
Note: there was an issue that prevented any sort of editing/commenting on this question and its answers. The issue has been fixed now.
Accepted Answer
Guillaume
on 24 Oct 2019
The loop was pointless to start with. All you're doing is:
m = mean(volume .* mask, 'all'); %don't use mean as a variable name!
which indeed averages a lot of 0s. The proper way to do this:
m = mean(volume(mask)); %assuming that mask is of type logical
6 Comments
Guillaume
on 4 Mar 2020
This requires a different approach altogether. You need to use one of the aggregating functions splitapply, accumarray or groupsummary. Assuming your mask is not a mask but a label image with integer values from 0 to N:
objectsmean = accumarray(double(mask(:))+1, yourimage(:), [], @mean);
%or
objectsmean = splitapply(@mean, yourimage(:), double(mask(:))+1);
%or
objectsmean = groupsummary(yourimage(:), mask, 'mean');
accumarray is probably the fastest. The double(..) is here in case your mask is stored as an integer type and the number of objects is equal to intmax(class(mask)) which would cause an overflow when 1 is added.
In each case, the first value in the vector objectsmean will be the mean of the background.
More Answers (1)
Cyrus Tirband
on 24 Oct 2019
Replace the last line by
meanval = mean(nonzeros(masked_volume));
That said, are mask and volume the same size? Those for loops are incredibly inefficient, and the whole snippet can be replaced by the one-liner if the dimensions of volume and mask are identical by using point-wise multiplication:
meanval = mean(nonzeros(volume.*mask));
3 Comments
Guillaume
on 24 Oct 2019
This solution multiplies the volume with the mask resulting in 0s everywhere that is mask (like your original code did). It then removes all the 0s, that is the 0s that are the result of masking but also all the 0s in the non-masked area. So yes, you'll get different results unless there's no 0s in the non-masked area.
My solution simply discards the masked pixels (with logical indexing) and average what's left so will give you the correct mean in all cases.
Guillaume
on 4 Nov 2019
You're commenting on the wrong answer, making the conversation a bit more difficult.
Answer to that as a comment to my answer.
See Also
Categories
Find more on Image Segmentation and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!