keep the number give by bwlabel to one object constant in all images of an image sequence
5 views (last 30 days)
Show older comments
Dear readers,
I have a sequence of 10 binary tiff images. In this images there are objects that I can detect by using the bwlabel function.
In the first image I have 3 objects and in the second image I have 4 objects so when I use the bwlabel, the numbers give to the 3 objects is modify because of the new object entering in the field. I want to keep the number give to one object constant in all images. Do you know if it is possible and the way to do it ?
Thank you in advance,
Cheers,
Sophia
0 Comments
Accepted Answer
Image Analyst
on 21 Dec 2018
If you don't have the Automated Driving System Toolbox that Mark mentioned then you can either try to find some tracking code in the Computer Vision System Toolbox, or write it yourself, or look for some open source tracking software.
To write it yourself, what you could do is first accept the labels of the first frame. Then compute all the centroids. Then if you can assume that the centroids did not move very much from one frame to the next, you can use pdist2() in the stats toolbox to find distances of every centroid in frame n+1 to the centroids in frame n. Start out by picking the two closest, and call those a match, assign the label from frame n to frame n+1 (meaning you've determined that they're the same blob), and remove them from consideration. Look at what's left and repeat, basically finding closest pairs, matching labels, and removing from consideration.
When you get down to a few blobs, you'll have to see if they are roughly the same distance away from each other as all the prios blobs. Otherwise you could get a situation where two blobs entered the field of view, and maybe some left the field of view and you'll have a hard time matching them up, so that's why you need to look at the distance and if it's way more than all the other matched distances, then they're probably different blobs, NOT a matched pair.
Writing tracking software is not easy - there are lots of weird pathological cases that need to be handled to do it properly.
More Answers (2)
Mark Sherstan
on 21 Dec 2018
Look at the example located here. If this does not work for you please post your images so that we can help you further.
Brian Hart
on 21 Dec 2018
Hi Sofia,
I assume these 10 images are related; that they show a changing field.
I think the only way to do what you want is to create a "composite" label matrix, that you update as each new image is processed. I wrote a very basic routine to do this below.
You only mention new objects appearing, so I'll assume new objects appear, but old ones never disappear. You'd have to update the code to handle disappearing objects. But this should get you started.
%create some small binary images, where the second image has one more
%object than the second:
img1 = logical ([ 0 0 0 0 0 0 1 1 1
0 0 1 1 0 0 1 1 1
0 0 1 1 0 0 1 1 1
0 0 0 0 0 0 1 1 1
0 0 0 0 1 0 1 1 1
0 0 0 0 1 0 1 1 1
0 0 0 1 1 0 1 1 1
0 0 0 0 0 0 1 1 1]);
img2 = logical ([ 0 0 0 0 0 0 1 1 1
0 0 1 1 0 0 1 1 1
0 0 1 1 0 0 1 1 1
0 0 0 0 0 0 1 1 1
0 0 0 0 1 0 1 1 1
0 0 0 0 1 0 1 1 1
1 1 0 1 1 0 1 1 1
1 1 0 0 0 0 1 1 1]);
%this shows the problem:
[L1, n1]= bwlabel(img1,4)
[L2, n2]= bwlabel(img2,4) %here the objects from img1 have different numbers.
%Put the images in a 3d array for in-loop indexing:
imgArr(:,:,1) = img1;
imgArr(:,:,2) = img2;
numImgs = size(imgArr,3);
%create all-zero "composite" L to start:
compL = img1 * 0;
%Now evaluate each image, adding _new_ objects to the composite L in
%series:
for i = 1:numImgs
[L, n] = bwlabel(imgArr(:,:,i));
if i==1
compL = L; %use the first image L as the starting composite L
numTotalObjs = n;
else
L(compL ~= 0) = 0; %zero out already-identified objects
newObjs = unique(L);
numNewObjs = length (newObjs) - 1; %assume there's always a zero
if numNewObjs > 0
for j = 1:numNewObjs
compL(L==newObjs(j+1)) = numTotalObjs + j;
end
numTotalObjs = numTotalObjs + numNewObjs;
end
end
end
%Now compL should have the first three objects labeled as in L1, with the
%new object from L2 labeled as "4"
3 Comments
Brian Hart
on 21 Dec 2018
Yes; this basic example depends on the detected pixels remaining constant. If they move a bit, then adding a dilation to the line Image Analyst quoted could help.
The primary question seemed to be how to manipulate the per-image results to maintain an overall object numbering and count. I think the provided algorithm shows a way to do that.
See Also
Categories
Find more on Computer Vision with Simulink in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!