- A binary 4-D volume where your original 4-D volume is masked?
- A list giving the indexes of the non-zero pixels in the mask?
- The total number of non-zero pixels in the masked volume?
Identify the nonzero pixels
12 次查看(过去 30 天)
显示 更早的评论
I have a stack of 3D volumes, i.e. 4D with dimensions [156, 192, 26, 41]. I have also a 3D binary mask with dimensions [152, 192, 26].
I would like to identify the locations of the nonzero pixels to speed up a calculation that I am doing in a for loop.
I tried the following but it returns a 1D array. I am looping over the voxels so it doesn't work. If I do that in the loop would it work? My results look totally black so I am not sure if it's the way I identify the nonzero pixels the problem or something else.
f = find(mask(:,:,:,1)>0);
for i=1:size(r,1)
for j=1:size(r,2)
for k=1:size(r,3)
if (mask(i,j,k)>0)
do some calculations..
end
end
end
end
4 个评论
Image Analyst
2019-4-1
He's not asking about the mask of course. We know that masks have only 0 and 1. He's asking about the 4-D array that the mask is to be applied to.
Plus you didn't answer my question. Do you want 1, 2, or 3.
回答(2 个)
Matt J
2019-4-1
编辑:Matt J
2019-4-1
Ideally, your code would have no more than one loop. It could look like the following,
locations=(mask>0);
for i=1:41
A=volume_stack(:,:,:,i); %extract a 3D volume from stack
B = A(locations); %extract its masked voxels
A(locations) = .... calculations with B ....;
volume_stack(:,:,:,i)=A; %put back in the stack
end
0 个评论
Walter Roberson
2019-4-1
idx = find(mask);
[r, c, p] = ind2sub(size(mask), idx);
Now mask(r(K), c(K), p(K)] is nonzero, and you can access
Your4DArray(r(K), c(K), p(K), :)
However, I notice that your 4D array has 156 rows but your mask has 152 rows, and I do not know how the mask matches the array.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Modify Image Colors 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!