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.

  • Steve: Kezia—Try imrotate.
  • kezia: steve, how to perform rotation of structuring element by 15 degrees. kindly answer my question. thank u kezia...
  • Steve: Tasha—I only accept comments that are relevant to the particular blog post or are questions or comments...
  • Tasha: Steve,I send you a comment here but still didn’t get any reply yet.I did not see my comment posted here...
  • Steve: Carsten—Thanks for your input.
  • Carsten: Another vote for either imtranslate.m, or at least a blurb in the imtransform help why pure translation...
  • Loren Shure: If you look towards the end of the fftfilt program, you will see that there’s a check to see if...
  • Steve: Sonja—My imwritesize submission on the MATLAB Central File Exchange might be helpful. It was posted...
  • Steve: Grant—Sorry, but it won’t be for R2010a. That development deadline has already passed.
  • Sonja: My publisher is wanting images for a new book to be 300 dpi. Only 5 of the 19 images are 300, the rest are...

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