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

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

  3. fer replied on :

    I keep on getting this error message when I try to use imreconstruct:

    “???MARKER pixels must be <= MASK pixels”

    Can anybody help me out?

  4. Raymond replied on :

    The function need the same size between marker and mask. BTW,the value of marker should be smaller or equal to the mask.

  5. Uma Ranjan replied on :

    The marker image is the one which has the “seed points”. The mask image is the original image (in this case, bw). You obviously cannot have seed points which are more than the number of image pixels.

  6. L. Jégou replied on :

    Thanks for this example, i’m testing the reconstruction operation in an image segmentation process, inspired by : http://rfv.insa-lyon.fr/~jolion/IP2000/report/node41.html


MathWorks
Steve Eddins is a software development manager in the MATLAB and image processing areas at MathWorks. Steve coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.

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