Skip to Main Content Skip to Search
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

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


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.

  • erik: “The two-dimensional Gaussian is the only radially symmetric function that is also separable” i...
  • huda: Hye.. I’m new to this image processing things.. I was told to make an image segmentation of a picture.....
  • Steve: Collin—There are functions that might be useful for the task. You might want to take a look at roifill.
  • Steve: Lori—Sure, multiple Handle Graphics objects can be combined in one plot. If you want to use imshow to...
  • collin: Hi steve, Could I erase the lines detected from an image. is there any function? thank you!
  • Lori: Is there a way to superimpose a small image onto a larger plot? I’m doing some geographical maps and...
  • Steve: WS—Because they are used twice. Here’s an example: A 1-by-5 flat structuring element can be...
  • WS: What I don’t understand is, why some decomposed structuring elements are listed twice?
  • Steve: Eman—In my completely biased opinion, I think that MATLAB (with the Image Processing Toolbox) is a...
  • Steve: Gene— sum(bw(:)) / numel(bw)

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

Related Topics