This example shows how to use the Graph Cut option in the Image Segmenter app to segment an image. Graph cut is a semiautomatic segmentation technique that you can use to segment an image into foreground and background elements. Graph cut segmentation does not require good initialization. You draw lines on the image, called scribbles, to identify what you want in the foreground and what you want in the background. The Image Segmenter segments the image automatically based on your scribbles and displays the segmented image. You can refine the segmentation by drawing more scribbles on the image until you are satisfied with the result.
The Graph Cut technique applies graph theory to image processing to achieve fast segmentation. The technique creates a graph of the image where each pixel is a node connected by weighted edges. The higher the probability that pixels are related the higher the weight. The algorithm cuts along weak edges, achieving the segmentation of objects in the image. The Image Segmenter uses a particular variety of the Graph Cut algorithm called lazysnapping. For information about another segmentation technique that is related to graph cut, see Segmentation Using Local Graph Cut (Grabcut) in Image Segmenter.
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.
b = imread('baby.jpg'); imageSegmenter(b)
Expand the Create Mask toolstrip group and click Graph Cut. The Image Segmenter app opens the Graph Cut tab.
As a first step, you must mark the elements of the image that you want to be in the foreground. (When the Graph Cut opens, the Mark Foreground option is preselected.) Marking an object is simply drawing a line (also called a scribble) over the object. When you draw a line, try to include all the different values in the object. You can draw as many separate lines as you like. If you are not satisfied with the lines you draw, you can always edit them. Click Erase and move the cursor over any part of the line you want to remove. If you have drawn many lines and want to start over, click Clear Markings.
Next, mark the elements of the image you want to be in the background. Again, simply draw a line over the image. When you finish drawing the line, the Image Segmenter immediately performs the segmentation (shown in blue).
Continue drawing foreground and background lines to improve the segmentation. For example, the baby’s left hand (lower right corner of the image) is not well-defined. There are also several spots on the baby’s right arm that need to be included in the foreground. To fix these problems, draw additional foreground and background lines on these parts of the image.
To get a better look at the segmentation, click Show Binary.
When you are satisfied with the segmentation, click Create Mask. The Image Segmenter closes the Graph Cut tab. You can use tools in the Refine the Mask toolstrip group to perfect the mask image, such as Morphology and Active Contours. To save the mask image, click Export. You can also use the Export option to obtain the code used for the segmentation. For more information about saving the mask image, see Save Mask Image Created Using Image Segmenter.