Calculate the mean of nonzero pixels
8 次查看(过去 30 天)
显示 更早的评论
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 个评论
Guillaume
2019-10-24
Note: there was an issue that prevented any sort of editing/commenting on this question and its answers. The issue has been fixed now.
采纳的回答
Guillaume
2019-10-24
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 个评论
Guillaume
2020-3-4
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.
更多回答(1 个)
Cyrus Tirband
2019-10-24
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 个评论
Guillaume
2019-10-24
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
2019-11-4
You're commenting on the wrong answer, making the conversation a bit more difficult.
Answer to that as a comment to my answer.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Segmentation and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!