필터 지우기
필터 지우기

Calculate the mean of region with nonzero pixels

조회 수: 1 (최근 30일)
Gina Carts
Gina Carts 2020년 3월 4일
답변: Guillaume 2020년 3월 4일
I have a mask with regions labeled with 1, 2 and 3. I have a 3D volume that I would like to calculate the mean of the pixels correspond to those regions.
I want to calculate the mean of the regions with number 1, number 2 and number 3 separately.
Is the following correct?
m = mean(volume(mask==1));
m = mean(volume(mask==2));
m = mean(volume(mask==3));
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 3월 4일
편집: Walter Roberson 2020년 3월 4일
Looks fine to me, other than needing three different output variables

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2020년 3월 4일
As I've answered in your other question:
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.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by