Segmentation Using Local Graph Cut (Grabcut) in Image Segmenter

This example shows how to segment an image using the Local Graph Cut option (known as grabcut) in the Image Segmenter app. Like the Graph Cut option, 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 segments the image automatically based on the contents of the ROI. 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. Ensure that the object is completely inside the ROI.

Then, as with Graph Cut, you can 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 Segmentation Using Graph Cut in Image Segmenter.

Read an image into the MATLAB® workspace and load it into the Image Segmenter. For more information about loading images into the app, see Open Image Segmenter App and Load Image.

car = imread('car2.jpg');
imageSegmenter(car)

Select Local Graph Cut in the Add to Mask section of the toolstrip. You can expand this group to see all of the options available.

As a first step, draw an ROI around the object in the image that you want to segment. When the Image Segmenter opens the Local Graph Cut tab, the Draw ROI button is enabled. 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 rectangle or a polygon 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, create a vertex at each click. Double-click to finish the polygon. As soon as you finish the ROI, the Image Segmenter segments the object in the ROI. If you are not satisfied with the shape you drew, you can always edit it. Right-click the ROI and choose Delete.

Next, draw scribbles to mark any parts of the foreground that weren't included in the automatic segmentation. After you draw the ROI, the Image Segmenter activates the Mark Foreground button automatically. Using the mouse, draw lines to identify parts of the image that you want to be included in the foreground.

Continue by marking elements that you want to be parts of the background. Click Mark Background and draw lines inside the ROI to identify which parts are the background. You can trim parts of the automatic segmentation that should be part of the background by drawing a line through them.

When you are satisfied with the segmentation, click Apply. The Image Segmenter 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. For more information about saving the mask image, see Save Mask Image Created Using Image Segmenter.

See Also

|

Related Topics