This example shows how to segment an image using Local Graph Cut (also known as grabcut) in the Image Segmenter app. Like Graph Cut, Local Graph Cut is a semiautomatic segmentation technique that you can use to segment an image into foreground and background elements. With Local Graph Cut, you first draw a region-of-interest around the object you want to segment. The Image Segmenter app segments the image automatically based on the contents of the ROI.
Then, as with Graph Cut, you refine the automatic segmentation by drawing lines, called scribbles, on the image inside the ROI. The lines you draw identify what you want in the foreground and what you want in the background. The Local Graph Cut option only segments elements within the boundaries of the ROI.
The Local Graph Cut technique, similar to the Graph Cut technique, applies graph theory to image processing to achieve fast segmentation. The algorithm 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. For information about the Graph Cut technique, see Segment Image Using Graph Cut in Image Segmenter.
Read an image into the workspace.
car = imread('car2.jpg');
From the MATLAB® Toolstrip, open the Image Segmenter app. On the Apps tab, in the Image Processing and Computer Vision section, click Image Segmenter .
On the app toolstrip, click Load Image, and then select Load Image from Workspace. In the Import from Workspace dialog box, select the image you read into the workspace. The Image Segmenter app displays the image you selected.
You can also open the app using the
imageSegmenter command, specifying the name of the image variable.
On the Image Segmenter app toolstrip, select Local Graph Cut.
The Image Segmenter app opens a new tab for Local Graph Cut segmentation. As a first step in Local Graph Cut segmentation, draw an ROI around the object in the image that you want to segment. When the Image Segmenter app opens the Local Graph Cut tab, it preselects the Draw ROI button. Position the cursor over the image and draw an ROI that encompasses the entire object you want to segment. To get a good initial segmentation, make sure the ROI you draw completely surrounds the object, leaving a small amount of space between the object and the ROI boundary. Make sure the object you want to segment is completely inside the ROI.
You can choose to draw a rectangular or polygonal ROI. Use the ROI Style menu to choose. To draw a rectangle, position the cursor over the image and then click and drag. To draw a polygon, click and drag the mouse, creating a vertex at each click. Double-click to finish the polygon. If you are not satisfied with the shape you drew, you can always edit it. Right-click the ROI and choose Delete.
When you finish the ROI, the Image Segmenter app automatically segments the object in the ROI. The blue shading indicates the segmented area.
To refine the automatic segmentation, draw lines (scribbles) to mark any parts of the foreground that weren't included in the automatic segmentation. After you draw the ROI, the Image Segmenter selects the Mark Foreground button automatically.
To remove areas from the segmentation that are not part of the foreground, mark those areas as background. Select the Mark Background option and draw lines inside the ROI to identify parts of the segmentation that should be in the background.
When you are satisfied with the segmentation, click Apply. The Image Segmenter app changes the color of the segmented part of the image to yellow.
To view the mask image, click Show Binary. You can also view the binary mask image in the main Segmentation tab. To return to the main Image Segmenter app, click Close Local Graph Cut.
When you are done segmenting the image, you can save the binary mask, using the Export option. You can also obtain the code used for the segmentation.