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.

  • 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.