MATLAB Answers

How to calculate mean in moving cubic volume?

33 views (last 30 days)
SojM
SojM on 25 Jul 2020 at 10:39
Edited: SojM on 2 Aug 2020 at 17:03
I have a stack of 2D images in 3D array format 100x100x500. Now I want to select a small cubic volume of 1x1x1 at center of the stack and increase this cubic volume step by step till I reach the array size, calculating mean at each stage. I know I need to use mat2cell and mean function. How can be done in corect for loop?

  0 Comments

Sign in to comment.

Accepted Answer

Matt J
Matt J on 25 Jul 2020 at 12:36
Edited: Matt J on 25 Jul 2020 at 15:15
I know I need to use mat2cell and mean function.
I don't think you do. Let's call your 3D volume, V:
[ci,cj,ck]=deal(51,51,251); %center coordinate ?
[I,J,K]=ndgrid(abs((1:100)-ci),abs((1:100)-cj),abs((1:500)-ck));
L=max(max(I,J),K)+1; %label matrix
stats = regionprops3(L,V,'MeanIntensity','Volume');
cumVolumes = cumsum(stats.Volume);
cumIntensities = cumsum(stats.Volume.*stats.MeanIntensity);
result = cumIntensities ./cumVolumes

  6 Comments

Show 3 older comments
SojM
SojM on 2 Aug 2020 at 14:32
Hi Matt, I am trying to use this code in for loop by changing the 'Z center coordinate (50 to 300)' keeping x and y center coordinate constant. I also want to change grid in Z such that it stays 100. How would I save all the results in struct array for all "i" in the loop? Could you please help me to fix following code?
I tried this, but this does not seem right
for i = 1:400
[ci,cj,ck]=deal(51,51,50+i); %center coordinate
[I,J,K]= ndgrid(abs((1:100)-ci),abs((1:100)-cj),abs((i:100+i)-ck)); % Z remain 100 but changes with center
L = max(max(I,J),K)+1; %label matrix
stats = regionprops3(L,V(:,:[i:100+i]),'MeanIntensity','Volume');
cumVolumes{i} = cumsum(stats.Volume);
cumIntensities{i} = cumsum(stats.Volume.*stats.MeanIntensity);
result{i} = cumIntensities ./cumVolumes
end
Matt J
Matt J on 2 Aug 2020 at 16:11
Something like this?
clear S
S(400)=struct('cumVolumes',[],'cumIntensities',[],'result',[]); %pre-allocate
for i = 1:400
[ci,cj,ck]=deal(51,51,50+i); %center coordinate
[I,J,K]= ndgrid(abs((1:100)-ci),abs((1:100)-cj),abs((i:100+i)-ck)); % Z remain 100 but changes with center
L = max(max(I,J),K)+1; %label matrix
stats = regionprops3(L,V(:,:[i:100+i]),'MeanIntensity','Volume');
S(i).cumVolumes = cumsum(stats.Volume);
S(i).cumIntensities = cumsum(stats.Volume.*stats.MeanIntensity);
S(i).result = cumIntensities ./cumVolumes
end
SojM
SojM on 2 Aug 2020 at 17:02
Thanks Matt. Yes, I was looking for somethign like that. That was very useful. Your code gave me error that "Undefined function or variable 'cumIntensities' ". I needed to define them before using as struct fields. It is working perfectly now. Following is the modified version of your code:
clear S
S(400)=struct('cumVolumes',[],'cumIntensities',[],'result',[]); %pre-allocate
for i = 1:400
[ci,cj,ck]=deal(51,51,50+i); %center coordinate
[I,J,K]= ndgrid(abs((1:100)-ci),abs((1:100)-cj),abs((i:100+i)-ck)); % Z remain 100 but changes with center
L = max(max(I,J),K)+1; %label matrix
stats = regionprops3(L,V(:,:,[i:100+i]),'MeanIntensity','Volume');
cumVolumes = cumsum(stats.Volume);
cumIntensities = cumsum(stats.Volume.*stats.MeanIntensity);
result = cumIntensities ./cumVolumes;
S(i).cumVolumes = cumVolumes;
S(i).cumIntensities = cumIntensities;
S(i).result = result;
end

Sign in to comment.

More Answers (0)