Steve on Image Processing

July 14th, 2008

Opening by reconstruction

Today I want to show you a morphological operation called "opening by reconstruction."

The normal morphological opening is an erosion followed by a dilation. The erosion "shrinks" an image according to the shape of the structuring element, removing objects that are smaller than the shape. Then the dilation step "regrows" the remaining objects by the same shape.

Here's an example using a fragment of text from the book Digital Image Processing Using MATLAB.

url = 'http://blogs.mathworks.com/images/steve/2008/book_text.png';
text = imread(url);
bw = text(1:500, 1:500);
imshow(bw)

Suppose we want to identify characters containing a tall vertical segment. We can do this by opening with a vertical structuring element.

Erode first:

se = strel(ones(51, 1));
bw2 = imerode(bw, se);
imshow(bw2)

Then dilate:

bw3 = imdilate(bw2, se);
imshow(bw3)

Or you can do the opening in a single step by calling imopen:

bw3 = imopen(bw, se);
imshow(bw3)

The dilation step in the opening operation restored the vertical strokes, but the other strokes of the characters are missing. How can we get the entire characters containing vertical strokes?

The answer is to use morphological reconstruction. For binary images, reconstruction starts from a set of starting pixels (or "seed" pixels) and then grows in flood-fill fashion to include complete connected components.

To get ready to use reconstruction, first define a "marker" image. This is the image containing the starting or seed locations. For our text example, the marker image will the output of the erosion.

marker = imerode(bw, se);
imshow(marker)

Next, define mask image. The flood-filling will be constrained to spread only to foreground pixels in the mask image. We can use the original text image as our reconstruction mask.

mask = bw;

Finally, call imreconstruct to perform the operation.

characters = imreconstruct(marker, mask);
imshow(characters)

Performing morphological reconstruction, using the eroded image as the marker and the original image as the mask, is called "opening by reconstruction."

Do you have other uses for morphological reconstruction in your own applications? Tell us about it: Click on the "Comment" link below.


Get the MATLAB code

Published with MATLAB® 7.6

2 Responses to “Opening by reconstruction”

  1. Shalin replied on :

    Hi Steve, there is a neat use of opening by reconstruction in counting cells from segmented light microscope images. When quantifying number of cells from a given image, one usually wants to discard the cells that fall on the edge of the image. Those cells may skew the statistics of the measurements being made as they do not present the ‘whole’ picture. The method works by reconstructing the cells on the edge and then subtracting them from original image. It will be interesting to know if there is an approach to reconstruct gray-scale image? What would we use instead of flood-fill? I first came across this during a talk by Pascal Valloton.

  2. Steve replied on :

    Shalin—The Image Processing Toolbox function imclearborder uses the method you describe to remove objects touching the image border. The function imreconstruct supports gray-scale image reconstruction.

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: Dansheng—The following code will give you the binary image corresponding to a single object. L =...
  • Dansheng Song: Hi Steve, Your posts are very helpful for Matlab users. Do you know how can I get the binary image by...
  • DP: Thanks Steve. it did work. now can easily calculate DSC.
  • Steve: DP— A & B
  • DP: Hi Steve, Dice similarity coefficient is defined to find the overlapped regions between two objects in an image....
  • Steve: Searching— axis on
  • Steve: DP—Sorry, but I’ve never heard of dice similarity coefficients.
  • searching: Hey, I found this place extremely useful. I have a question though. Although I manage to plot my points on...
  • DP: Hi Steve, thanks for your email. i could not seperate those connected objects. Instead i am trying to compare...
  • Gopesh: Excellent code..was very helpful to me to change the background color of mri images and thereby improving the...

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