Can I edit regionpros centroid to use the previous location if no centroid is found?

1 view (last 30 days)
Tim Sanborn
Tim Sanborn on 19 Jul 2017
Commented: Walter Roberson on 25 Aug 2017
I need to track ~500 centroids in consecutive images (in a for loop). Sometimes the centroids disappear. For example, centroids #250 and #251 may disappear from images 157-162 and then reappear for the rest of the images. Is there a way to edit regionprops so that it does not fail when it cannot complete the same struct array in the loop? i.e. when regionprops finds all the centroids it places them in a struct and if the number of centroids changes it cannot continue the loop.
for j = 1:numframes
for i = 1:cx
l(:,:,i,j) = imcrop(g(:,:,j),A(i,:));
l2(:,:,i,j) = l(:,:,i,j) > 10;
l3(:,:,i,j) = bwareaopen(l2(:,:,i,j),6,8);
s2(i,:,j) = regionprops(l3(:,:,i,j),'centroid');
centroid(i,:,j) = cat(1,s2(i,:,j).Centroid);
cen_loc(i,:,j) = base_corners(i,:)+centroid(i,:,j)-1;

Answers (3)

Walter Roberson
Walter Roberson on 19 Jul 2017
No, there is no way to edit regionprops to do that. You should instead be changing the way you store the result of regionprops. For example,
s2{i,j} = regionprops(l3(:,:,i,j),'centroid');
centroid{i,j} = cat(1,s2{i,j}.Centroid);
cen_loc{i,j} = bsxfun(@plus, base_corners(i,:), centroid{i,j}-1 );
I get the impression from your code that each object is expected to be constrained within a known region and the objects cannot move relative to each other except by sort of jiggling around. Is that correct?

Image Analyst
Image Analyst on 19 Jul 2017
No, sorry. Each time bwconncomp() or bwlabel() runs, it assigns ID labels independently. In general with tracking you will have some objects that move some distance and stay in the field of view, other objects that vanish or leave the field of view, and other objects that newly appear or enter the field of view. For those object that don't move much, you can try to match objects in different images by finding the old centroid that is closest to the new centroid. Not too hard if they don't move much. However if they move a tremendous amount, like you take a picture of a box full of honeybees and another picture 60 seconds later, it's hard or impossible to associate the objects in frame 2 with those from frame 1.
If you had special circumstances like you have 100 objects that don't move much but 10 of them are missing from some frames, then you could first try to match existing objects, then any "left over"/unmatched objects, go back in history and try to find some prior frame where that object existed. But there's nothing in MATLAB that does this at a high level so basically you're going to have to program up your own special ad hoc tracking algorithm. Good luck.

Tim Sanborn
Tim Sanborn on 25 Aug 2017
Answer: Once the number of blobs is established in the first frame, use an if statement inside the for loop to tell is to use the previous location. Easy work around.
Walter Roberson
Walter Roberson on 25 Aug 2017
No, if the objects are moving, that solution is not sufficient. Unless there are constraints on the velocity or permitted permissions, it can be difficult to tell whether any particular object "disappeared" and later "reappeared", or if the object just moved to another location.
Consider, for example, a "15 Puzzle", where two configurations that look close might have involved a lot of pieces moving around.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by