Area Opening Example

From MATLAB Techniques for Image Processing by Steve Eddins.

When you segment an image, you often get small "noisy" spots. For example:

bw = imread('segmented_rice.png');
imshow(bw)  % Zoom and pan to show spots.

There's a very handy image processing operation called area opening, which removes all binary image objects whose area (number of pixels) is smaller than some threshold. The Image Processing Toolbox function is called bwareaopen.

bw2 = bwareaopen(bw,10);
imshow(bw2)

Before-and-after comparisons are frequently important. Did you know that you "link" two different plots, so that zooming and panning in one plot simultaneously zooms and pans in another? The key function is called linkaxes.

clf
ax(1) = subplot(1,2,1);
imshow(bw)
title('Original image')

ax(2) = subplot(1,2,2);
imshow(bw2)
title('After area opening')

Link the two axes:

linkaxes(ax)  % Zoom and pan