Trouble Looping and recording distance between nearest centriods

2 次查看(过去 30 天)
Hi all,
I am having trouble looping through this. I have 20 images of rising bubbles against a black background that I am looking to loop through. The images are of blobs and each blob has it's own centroid. (about 50 or so per image). What I am trying to do is record the nearest blob from image to image. I have been able to do this manually from one image to the next but I am having trouble getting this into a for loop. I am sending all of this to an index matrix that looks something like this:
1 1
2 3
3 6
4 5
5 7
with the first number being the original blob in the first image and the second number being the closest blob in the next image (presumably the same blob that is moving upwards).
Below is my code snippet:
for k = 1:20
k = k %original image
j = k+1; % next image
index_matrix = [1:1:length(centroid(k).X)]';
for m = 1:length(centroid(k).X) % starting bubble in the k image
for ii = 1:length(centroid(j).X) %loop over possible bubbles, finding distance
Dist(ii) = sqrt( (centroid(k).X(m)-centroid(j).X(ii))^2 + (centroid(k).Y(m)-centroid(j).Y(ii))^2);
end
[dumb, ind] = min(Dist);
index_matrix(m,j) = ind;
end
end
The error that I am getting is that the Index exceeds the number of array elements .

采纳的回答

Tarunbir Gambhir
Tarunbir Gambhir 2021-4-27
The Index exceeding error might be because you are looping "k" variable from 1 to 20, but the "j" variable is looping from 2 to 21. To avoid this, you should loop the variable "k" from 1 to 19.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by