Steve on Image Processing

April 14th, 2008

Relabeling a label matrix

The three most common questions I've been hearing about bwlabel, which is used for labeling connected components in a binary image, are about

  • Search order
  • Relabeling (renumbering the label matrix)
  • Correspondence between labels in two different images

Today I'll tackle relabeling.

In my previous post on search order, I showed how to postprocess the labeled objects to modify their order according to various criteria. Now I'll take that a step further and use the sorted output from regionprops to renumber the labels in the label matrix.

Blog reader Trung wanted to know if we could sort lexicographically by centroid, so I'll do that here.

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

Here's a false-color view of the label matrix using label2rgb:

L = bwlabel(bw);
rgb = label2rgb(L, 'jet', [.95 .95 .95], 'shuffle');
imshow(rgb, 'InitialMagnification', 'fit')

Now let's use regionprops to get the centroids for each object. To facilitate the relabeling step, I'll get the 'PixelIdxList' for each object.

s = regionprops(L, {'Centroid', 'PixelIdxList'});

Next, sort lexicographically by the centroids, sorting first by the vertical coordinate.

centroids = cat(1, s.Centroid);
[sorted_centroids, sort_order] = sortrows(fliplr(centroids));
s2 = s(sort_order);

To visualize the sorted object order, we can display the object numbers on top of the objects like this:

% First, make an image with a light gray background instead
% of a black background, so that the numbers will be visible
% on top of it.
I = im2uint8(bw);
I(~bw) = 200;
I(bw) = 240;
imshow(I, 'InitialMagnification', 'fit')

% Now plot the number of each sorted object at the corresponding
% centroid:
hold on
for k = 1:numel(s2)
   centroid = s2(k).Centroid;
   text(centroid(1), centroid(2), sprintf('%d', k));
end
hold off

We sorted the output of regionprops, but we haven't touched the label matrix itself. We can relabel it according to the sort order by using linear indexing and the PixelIdxList of each object. An object's PixelIdxList is a vector of linear indices for the pixels belonging to the object. Here's the relabeling loop:

for k = 1:numel(s2)
   kth_object_idx_list = s2(k).PixelIdxList;
   L(kth_object_idx_list) = k;
end

In MATLAB 7.6 (R2008a), the publish feature lets you capture multiple graphics from within a loop. I'll use that new capability to show the location of the first few relabeled objects in L.

for k = 1:5
   imshow(L == k)
end

Soon I'll write another post that shows how how to match up labels in objects that overlap between two images.


Get the MATLAB code

Published with MATLAB® 7.6

9 Responses to “Relabeling a label matrix”

  1. Swetha replied on :

    Hello Steve,

    The posts on connected components is very useful.I have tried relabelling the objects with two images an image and i just clipped some portion and tested . I get two different objects ,an object recognised in one image and in another image it is not an object . How is it possible?
    -swetha

  2. Swetha replied on :

    Hello,

    If you are confused with the two images taken ,i have taken image1- some image ,image2-clipped version of image1.
    -swetha

  3. Steve replied on :

    Swetha—Your question is pretty vague. Can you be more specific?

  4. azhagumani replied on :

    i want to merge the segments having the same average intensity value and adjacent to each other

  5. Steve replied on :

    Azhagumani—The Image Processing Toolbox does not have such a function, so you’ll need to code it up yourself.

  6. shahn replied on :

    Dear Steve

    I have tried relabelling objects in my binary image, using
    region groups but the labelling is incohereant.

    how can I relabel my binary objects row wise as :

    1 2 3 4 5 6 7
    8 9 10 11 12 13 etc ?????? in my case the objects represent atoms.
    Regards Shahn

  7. Steve replied on :

    Shahn—See this post.

  8. Piyush replied on :

    Hi, I have a labeled matrix but one thing I would like to do is create text labels on the image matrix itself without having to open up a figure and plot text labels on them because I am trying to do it in a parallel process, and have it go really fast. For example I have circles that I am checking for their circularity and I just want to overlay text in the image itself over the circle with the circularity value. Is this possible with the current toolset Matlab provides? If not what is the recommended way to implement this?

    Thanks,
    Piyush

  9. Steve replied on :

    Piyush;mdash;There’s no good way in MATLAB to render text except via graphics commands into a figure window. However, you can do it into an invisible figure and then use the print function to output to an image file. I don’t know whether the performance will be suitable for you or not.

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.