How to find non zeros in multiple matrices ?

1 次查看(过去 30 天)
Hi,
I have an issue with my program :
Basically I have a stack of 20 images (555*555 pixels each) and I want to record the coordinates of a bright object on each image. For that, I use the function imbinarize that returns a 555*555 matrix of 0 (backgroud) and 1 (bright object). I have now a Bin=555*555*20 matrix with all the 0 and 1 of my 20 images.
My problem is that when I use the find fuction : [a,b]=find(Bin(:,:,1:20)==1); it works for the variable a that gives me all the y (horizontal) coordinates that have 1 for value but the value b only works for the first image (for the other images b is superior to 555 which is normally the maximal coordinate !).
Can you please help me with that ?
Thanks a lot

采纳的回答

Jakub
Jakub 2017-8-3
Variable b holds linear indices corresponding to second and third dimension. That's why it exceeds the size of array. A possible solution is to use linear indexing from start followed by ind2sub:
k = find(Bin(:,:,1:20)==1);
[a,b,nimage] = ind2sub(size(Bin),k);

更多回答(1 个)

fish minox
fish minox 2017-8-3
Yes it works ! Thank you very much for the fast answer man!I need to dig in this linear indexing thing, looks powerful. thanks again!

Community Treasure Hunt

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

Start Hunting!

Translated by