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

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


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.

  • ismail: i love chess keep posting :) can we make a web cam identify a chess set ? so we have a roboarm plays for real...
  • Navan: While black is going to win with a smothered mate, it is hard to see what moves would have led to this...
  • Doug: Forced ’smothermate&# 8217; is about to happen, with the added insult of threatening the queen on the...
  • Viton: Let’s give it a try: - RxQ : White Rook takes black Queen (White King was checked, can’t take...
  • Omar: Hi Steve, when using tformfwd to find corresponding points in the new space, the resulting co-ordinates from...
  • Steve: Cris—You’ ;re right, I should have caught the plot scaling issue. I wasn’t actually trying...
  • Cris Luengo: Not to spoil your upcoming bog entry too much, but if you scale the first graph (times vs Q) by setting...
  • Steve: Jim—Thanks for adding your comment showing how the syntax works for matrices.
  • Jim: for i = A …statements 230; end; Description: The …statements 230; are executed (as MATLAB...
  • Steve: Omar—Nice work.

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

Related Topics