url = 'https://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.
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.