This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Segment Image Using Graph Cut

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 display 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 techniques 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.

Read an image into the MATLAB workspace.

b = imread('baby.jpg');

Open the Image Segmenter app. From the MATLAB® Toolstrip, open the Apps tab and under Image Processing and Computer Vision, click Image Segmenter . You can also open the Image Segmenter from the command line:


In the Image Segmenter app, click Load Image, and then select Load Image from Workspace, since you have already read the image into the workspace.

In the Import From Workspace dialog box, select the image you read into the workspace, and click OK.

The Image Segmenter app displays the image.

Select Graph Cut in the Segmentation Tools section of the Tool strip.

The Image Segmenter app opens the Graph Cut tab, displaying a toolstrip for this technique.

Mark the elements of the image you want to be in the foreground. When the Image Segmenter opens the Graph Cut, the Mark Foreground option is selected. Marking an object is simply drawing a line (also called a scribble) over the element. 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).

Refine the segmentation. With the Graph Cut technique, you can simply draw more 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.

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. There a few small white spots in the background area of the image. These can be cleaned up using morphological processing available through the Image Segmenter app. Click Close Graph Cut.

Clear the small white spots from the mask image. Click Morphology. The Image Segmenter opens the Morphology tab. Select Erosion from the menu of morphological operations. Increase the radius of the structuring element to 5 to remove the white spots. Click Apply and Close Morphology.

When you are done segmenting the image, you can save the binary mask. Click Export and select Export Image.

Was this topic helpful?