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.

  • murat: Hi Steve, I have an rgb image of a kind of cream and it contains some small black particles (black dots). In...
  • Steve: Ernest—Look at setting the FaceColor property. The code for setting that is shown on the page you asked...
  • Ernest Miller: Hi Steve, Understood. However, can you explain how to change the colors? Thanks, Ernest
  • Jan: Hi Steve Very useful code, yet what if I parts of my rotated+translated object are outside the original...
  • Steve: MoHDa—It might be possible. You’ll need to use one of the options that produces closed edge...
  • MoHDa: I have one question about the ROIPOLY: I have an image with stripes, I use the “edge” command for...
  • Steve: Shahn—My November 17, 2006 post shows you how to do it.
  • Steve: Kay-Uwe—Thanks for following up. I am planning to make it easier to use test directories in a package....
  • shahn: Hello Steve Instead of superimposing a star on the image to show the centroide. How would you superimpose a...
  • Kay-Uwe: Having TestSuite.fromPackag e() would be nice to have, but so far using simple “test” subdirs...

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