Identify the nonzero pixels

12 vues (au cours des 30 derniers jours)
Gina Carts
Gina Carts le 1 Avr 2019
Commenté : Image Analyst le 1 Avr 2019
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 commentaires
Gina Carts
Gina Carts le 1 Avr 2019
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 le 1 Avr 2019
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.

Connectez-vous pour commenter.

Réponses (2)

Matt J
Matt J le 1 Avr 2019
Modifié(e) : Matt J le 1 Avr 2019
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 le 1 Avr 2019
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.

Catégories

En savoir plus sur Modify Image Colors dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by