Steve on Image Processing

April 21st, 2008

Corresponding labeled objects in two images

Several questions I've seen about bwlabel are about finding the correspondences between object labels in two images. In other words, if a particular pixel is in the foreground in two binary images, is that pixel labeled the same in both images, or is it different? In general, for foreground pixels in the same locations, how do object labels in one binary image match up to object labels in the second?

I'll show one method using the same small binary image that I've been using in my other recent bwlabel posts.

url = 'http://blogs.mathworks.com/images/steve/186/scanned_page.png';
bw = imread(url);
bw = ~bw(1107:1194, 17:135);
imshow(bw, 'InitialMagnification', 'fit')

Compute and display the labeled objects.

L1 = bwlabel(bw);

I = im2uint8(bw);
I(~bw) = 200;
I(bw) = 240;
s = regionprops(L1, 'extrema');
imshow(I, 'InitialMagnification', 'fit')
hold on
for k = 1:numel(s)
   e = s(k).Extrema;
   text(e(1,1), e(1,2), sprintf('%d', k));
end
hold off

Now let's try a morphological closing operation on this image.

bw2 = imclose(bw, ones(3,3));
imshow(bw2, 'InitialMagnification', 'fit')

Compute and display the labeled objects in the second image.

L2 = bwlabel(bw2);

I2 = im2uint8(bw2);
I2(~bw2) = 200;
I2(bw2) = 240;
s2 = regionprops(L2, 'extrema');
imshow(I2, 'InitialMagnification', 'fit')
hold on
for k = 1:numel(s2)
   e2 = s2(k).Extrema;
   text(e2(1,1), e2(1,2), sprintf('%d', k));
end
hold off

Compare the two sets of labels visually. You can see that the broken "f" character on the bottom line was labeled with 6 and 7 in the first image. In the second image, the broken pieces have been merged and labeled with 4.

On the first line of the first image, the "s" and "t" characters are distinct and are labeled with 2 and 4, respectively. In the second image, they have been merged into one object that was labeled 2.

Let's see how to compute all of the correspondences between the object labels in the two images. First, compute the set of pixels that belong to the foreground in both images.

overlap = bw & bw2;

Next, compute the corresponding label pairs by using logical indexing into the two label matrices.

pairs = [L1(overlap), L2(overlap)];

Eliminate the duplicates in the list of pairs.

pairs = unique(pairs, 'rows');

Let's look at a few of the pairs to see how to interpret their meaning:

pairs(1:5, :)
ans =

     1     1
     2     2
     3     3
     4     2
     5     1

Pixels belonging to objects 1 and 5 in the first image belong to a single object, labeled 1, in the second image. Similarly, pixels belonging to objects 2 and 4 in the first image belong to a single object, labeled 2, in the second image. Pixels labeled with a 3 in the first image are also labeled with a 3 in the second image.

Depending on your application, you might want to form an adjacency graph, represented as a sparse matrix.

S = sparse(pairs(:,1), pairs(:,2), 1);
spy(S)

One way to use the graph is to find all the object labels in image 1 that correspond to a given label in image 2. For example, here are all the first-image objects corresponding to the object labeled with 6 in the second image:

find(S(:, 6))
ans =

     9
    10

My other recent bwlabel posts were about search order and relabeling. You can also look the archive of all my posts about connected-component labeling.


Get the MATLAB code

Published with MATLAB® 7.6

10 Responses to “Corresponding labeled objects in two images”

  1. Brett Shoelson replied on :

    Nice post, Steve. In fact, very nice series on labeling.

    For completeness on this entry, I thought I’d point out that if you don’t specifically need the visual of the adjacency graph, you can easily get the same information (which image 1 objects were relabeled 6 in image 2?) directly from the calculation of pairs:

    >> pairs(pairs(:,2)==6)
    ans =
    9
    10

    Also, one might not always be looking for how objects relabeled in image 2 correspond to original labels in image 1. Sometimes one might want to know how an object labeled 9 in image 1, for instance, was relabeled in image 2:

    >> find(S(9,:))
    ans =
    6

    Or:

    >> pairs(pairs(:,1)==9,2)
    ans =
    6

  2. Steve replied on :

    Brett—Thanks.

  3. Shoeb replied on :

    Great post Steve! I have one question. Is it possible to do the same thing with labeled objects that moves in the next image?

    For example, I have series of images where my region of interest is slowly moving with time. Sometimes one breaks into 2 and sometimes 2 merge into one. Other times that moves slowly from its starting point. Is there any way to identify a particular object in all the images?

    Thanks in advance!

  4. Steve replied on :

    Shoeb—There’s nothing in the Image Processing Toolbox that will do that for you automatically. You’ll have to do some algorithm development on your own. I’d start my searches with something like “object tracking” and “blob analysis.”

  5. Michael Habib replied on :

    Hi,
    Is there a way to ‘imeorde’ merged labeled objects to set them apart. I found that ‘imerode’, and ‘imdilate’ treat the labeled image as a binary image. I know I can do it through a loop, but it gives poor performance, as I have tens of thousands of objects.

    thanks

    Michael

  6. Steve replied on :

    Michael—If these labeled objects came from bwlabel or bwconncomp, then they are already apart. You can use imerode on the whole image at once to shrink all the objects further. If the labeled objects are actually touching, can you say more about how you computed them?

  7. sathish replied on :

    hi steve,

    I have similar images with circular/partially circular objects in them. The object number is not conserved and the same object in one image is not necessarily circular in the other.
    I would like to compare the objects with the same label in both images. Is there anyway to track the object and give it the same label as in the other image? To simply say it, I want to preserve the label of the object from one image to other.
    I tried using regionprops with centroid. I m wondering if there is better way to do it.

    Thanks for the posts. I closely follow them when I m in doubt.

    sathish.

  8. Steve replied on :

    Satish—That’s more or less what this post is about. Did you find the techniques described here to be helpful? If not, what’s missing?

  9. Image vision replied on :

    Hi,
    Thanks for your post.
    Please guide in the following:
    Please consider some other example!
    1)
    If in image2 objects are numbered differently than that of image1.
    how i can relabel as that of image1. Here the number of objects found are same in both images.

    2) in another case:
    If in image2, the numbers of objects are more than that of image1
    how i can pick up objects in image2 that corresponds to image1 with labeling as that of image1 and discarding extra objects in image2.
    best regards,
    Image Vision.

  10. Steve replied on :

    Image Vision—Regarding question 1, I assume that the two images are somehow different, even though they have the same number of objects. How are they different? Is each object in one image guaranteed to overlap with only one object in the other image? Regarding question 2, that’s what this post is about. Did you find the techniques described here to be helpful? If not, what’s missing?


MathWorks
Steve Eddins is a software development manager in the MATLAB and image processing areas at MathWorks. Steve coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.

These postings are the author's and don't necessarily represent the opinions of The MathWorks.