This example shows how to use the Auto Cluster option in the Image Segmenter app to segment an image. The Auto Cluster option is an automatic segmentation technique that you can use to segment an image into foreground and background elements. The Auto Cluster option does not require initialization.
The Auto Cluster option requires the Statistics and Machine Learning Toolbox. If you don't have this toolbox, the Image Segmenter does not include this option. Generated code also requires Statistics and Machine Learning Toolbox.
Read an image into the MATLAB® workspace and load it into the Image Segmenter app. For more information about opening the Image Segmenter app and loading an image, see Open Image Segmenter App and Load Image.
coins = imread('coins.png'); imageSegmenter(coins)
Expand the Create Mask group of toolstrip buttons and click Auto Cluster.
The Image Segmenter app segments the image, displaying the result. To get a better view of the segmentation, invert the mask, using the Invert Mask option in the Refine Mask toolstrip group. The Auto Cluster option has correctly segmented all the circles. However, some of the circles have holes.
Clean up the holes in the segmentation using the Fill Holes option in the Refine Mask toolstrip group.
When you are satisfied with the segmentation, click Show Binary to see the mask. and then click Export to save the mask image. For more information about saving the mask image, see Save Mask Image Created Using Image Segmenter.