Segment Image Using Graph Cut in Image Segmenter
R2026bThis example shows how to segment an image using the Graph Cut option in the Image Segmenter app. Graph cut is a semiautomatic segmentation technique in which 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 app segments the image automatically based on your scribbles. You can refine the segmentation by drawing more scribbles on the image until you are satisfied with the result.
Load Image into the Image Segmenter App
Read an image into the workspace, then load the image into the Image Segmenter app.
b = imread("baby.jpg");
imageSegmenter(b)
Use Graph Cut to Segment Image
On the Image Segmenter app toolstrip, select Graph Cut.
Image Segmenter opens a new tab for Graph Cut segmentation and preselects the Mark Foreground tool. As a first step in Graph Cut segmentation, mark the elements of the image that you want to be in the foreground. To mark an object as foreground, draw a line over the object. When you draw a line, try to include all the different values in the object you want to segment. You can draw as many separate lines as you like. If you are not satisfied with the lines you draw, you can erase them. Click Erase and move the cursor over any part of the line you want to remove. If you want to start over, click Clear Markings.
Next, click Mark Background and draw scribbles to mark the elements of the image that you want to be the background. When you finish drawing the lines, Image Segmenter immediately performs the segmentation (shown in blue).
To refine the segmentation, continue drawing foreground and background lines. For example, there are several areas near the bottom of the image that need to be removed from the foreground. To fix these problems, draw additional 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 in the toolstrip on the Graph Cut tab. The app closes the Graph Cut tab and returns you to the Segmentation tab. You can then refine and save the mask image.
See Also
Image Segmenter | lazysnapping