Identify the nonzero pixels

7 次查看(过去 30 天)
Gina Carts
Gina Carts 2019-4-1
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 个评论
Gina Carts
Gina Carts 2019-4-1
The mask is binary has values 0 and 1 only. I want to identify the nonzero pixels i.e. pixels=1.
Image Analyst
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
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

Walter Roberson
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.

类别

Help CenterFile Exchange 中查找有关 Modify Image Colors 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by