Steve on Image Processing

September 4th, 2007

Clearing border components

I saw an application recently where someone wanted to handle NaNs in an image differently depending on whether they were "interior." In other words, a set of connected NaN pixels surrounded by non-NaN pixels are handled one way, and a set of connected NaN pixels that touches the image border are handled another way.

Coincidentally, another person recently commented on the need in some measurement situations to remove objects that touch the image border.

So I thought it would be good time to show how to use the Image Processing Toolbox function imclearborder. This function removes all connected components of a binary image that touch any image border. Here's an example. (To run the example, download aug31.png from here.

BW = imread('aug31.png');
imshow(BW)
BW2 = imclearborder(BW);
imshow(BW2)
title('Objects touching image borders removed')

Now back to the NaN problem. Here's a small example:

f = magic(5);
f(2:5,1:2) = NaN;
f(3:4,4) = NaN
f =

    17    24     1     8    15
   NaN   NaN     7    14    16
   NaN   NaN    13   NaN    22
   NaN   NaN    19   NaN     3
   NaN   NaN    25     2     9

The NaNs on the left make up a group touching the border. The NaNs in the fourth column are an interior group. We can distinguish between the two groups by using imclearboder and some logical operations.

nan_mask = isnan(f)
nan_mask =

     0     0     0     0     0
     1     1     0     0     0
     1     1     0     1     0
     1     1     0     1     0
     1     1     0     0     0

border_nans = nan_mask & ~imclearborder(nan_mask)
border_nans =

     0     0     0     0     0
     1     1     0     0     0
     1     1     0     0     0
     1     1     0     0     0
     1     1     0     0     0

interior_nans = nan_mask & ~border_nans
interior_nans =

     0     0     0     0     0
     0     0     0     0     0
     0     0     0     1     0
     0     0     0     1     0
     0     0     0     0     0

You can use also imclearborder on gray scale images. As the documentation describes, the function "suppresses structures that are lighter than their surroundings and that are connected to the image border." You can see the effect using the rice.png image:

I = imread('rice.png');
imshow(I)
J = imclearborder(I);
imshow(J)

The modified image looks darker. As the documentation notes, for gray scale images imclearborder "tends to reduce the overall intensity level in addition to suppressing border structures." Using the display scaling syntax of imshow helps to see the result clearly:

imshow(J, [])

So what happened to those two dark grains (on the left and on the right)? If we zoom into the original image to look closely at the grain on the right, we can see that there are a couple of slightly darker gray pixels connecting the grain to its neighbor on the right.

imshow(I)
axis([200 256 81 137])

But that grain on the right touches the border. So when imclearborder removes it, it also has the affect of lowering the gray level of its neighbor.

imshow(J, [])
axis([200 256 81 137])

Have you found imclearborder to be useful? Let me know - I'd like to hear about it.


Get the MATLAB code

Published with MATLAB® 7.5

3 Responses to “Clearing border components”

  1. Daphne replied on :

    Hi Steve,
    Found imclearborder very useful. I am now implementing the centroid code you had suggested in the blog to tracking particles in cells. Looks to be much, much faster than what I have now, but I haven’t quantified yet.
    Being able to remove particles on the edges really helps, as those usually throw the centroid calculation off, which in turn will affect the trajectories that we collect.
    Thanks for the helpful blogs!
    Daphne

  2. Pancham Shukla replied on :

    This is quite useful function.

  3. jairp replied on :

    Thanks a lot,
    Matlab Help file did not explain this function well enough for me to understand. Read your article and now I know
    thanks

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.