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

6 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?

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Steve Eddins manages the Image & Geospatial development team at The MathWorks and coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.

  • Sana: hi steve, could you explain to me how i would be able to use the dir function, to do a loop through a directory...
  • Nishtha: Sir, I have preprocessed the image in following steps: [1] adaptive histogram equalization [2] thresholding...
  • Kristof: I also strongly support the idea. I have just recently bumped into the problem that im2single was not...
  • Steve: David—I’ m glad you found it useful!
  • David Lalejini: I found your example very useful for finding connected nodes in a large set of input pairs. I start...
  • tommy: Dear Steve, I have a question,please if you are kind to help me regarding the accumulator array dimensions of...
  • Steve: Abc—I don’t know how to distinguish the faces. You might try posting your question in the MATLAB...
  • Manju: well if we have a few ovals within each other like in a cell how do we measure the distance from the center...
  • Steve: Manju—What do you mean? How is each region defined?
  • Manju: if we have 2-3 regions within each other how do we measure the regions of each one?

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