Opening by Reconstruction Example

From MATLAB Techniques for Image Processing by Steve Eddins.

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

text = imread('book_text.png');
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)

Using an eroded image as the marker for morphological reconstruction, with the original image as the mask, is called "opening by reconstruction."