This example shows how to use the Image Segmenter app to segment an image. The Image Segmenter app offers several segmentation methods including, various thresholding options, flood-fill, graph cut, and the active contours algorithm. Using the app to segment an image, you can try these options individually or use them in combination until you achieve the segmentation you want. The following example shows one possible path to achieving a segmentation with the app.
This part of the example shows how to open the Image Segmenter app and load an image.
Read an image into the MATLAB® workspace. The example
dicomread function to import CT scan
data of a knee joint. This example segments the three bony areas of
the image from the surrounding soft tissue.
I = dicomread('knee1'); imshow(I,)
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:
Load an image into the Image Segmenter app. Click Load
Image and choose whether you want to specify the name of
the file containing the image or the name of a workspace variable.
The Image Segmenter app can open any file that can be read by
Because you already loaded the image into the workspace, select the Load
Image From Workspace option. In the Import From Workspace
dialog box, select the variable you created.
The app displays the image you loaded and displays a thumbnail of the result of the segmentation (the mask image) in the Data Browser. Initially, the thumbnail is completely black because you have not started yet. You can perform multiple segmentations using the app and each segmentation appears, with a thumbnail, in the Data Browser. To start a new segmentation click New Segmentation. Note also that the app display a history of the steps you take while creating the segmentation in the History part of the Data Browser.
To start the segmentation process, select one of the options presented in the app toolstrip. The Image Segmenter app provides several segmentation tools that you can use. Segmentation is an iterative process where you might try several options until you achieve the result you want.
Thresholding—An automatic technique where you specify an intensity value that you want to isolate. This technique can be useful if the objects you want to segment in the image have similar pixel intensity values and these values are easily distinguished from other areas of the image, such as the background.
Graph cut—A semiautomatic technique to segment foreground and background, this method does not require careful seed points and you can refine the segmentation interactively.
Freehand—Manually draw regions outlining the objects you want to segment. Using the mouse, you can draw freehand regions or polygonal regions.
Flood-fill—An automatic technique where you specify starting points and method segments areas with similar intensity values.
Active contours—An automatic, iterative method where you mark locations in the image and active contours grows (or shrinks) the region to segment objects in the image. The accuracy of your initial see mask can impact your final result.
As a first attempt at the segmentation, try thresholding.
Click Threshold in the Segmentation Tools group. The app displays the Threshold tab with several thresholding options. You can choose between several thresholding methods, including manual thresholding in which you specify the value using a slider. However, the knee image does not have well-defined pixel intensity differences between foreground and background, and contains many other objects besides the bony objects. Thresholding does not seem like the best choice for this image.
Click Close Threshold, to return to the main app window without accepting the result and try one of the other options. (To keep your thresholded mask image, click Apply.)
Another technique supported by the Image Segmenter is Graph Cut segmentation. Graph Cut is a semiautomatic method where you place a mark on the image to indicate regions that you want as foreground and regions you want as background. A mark (also called a scribble) can be a simple line in the region.
Click the Graph Cut option. The Image Segmenter opens the Graph Cut tab.
Mark the bony areas as foreground regions. When you open the Graph Cut tab, Mark Foreground is preselected. Using the mouse, draw lines on the three regions of the image you want to segment.
Mark the background area of the image. Click Mark Background and draw lines over the area of the image you want as the background. After you draw your first line, the Image Segmenter performs the segmentation immediately. The areas in blue show the segmented region. With the Graph Cut method, you can continue to mark the area you want as background to refine the segmentation. Using this method, you can achieve a reasonable first pass at a segmentation. The intensity value differences between foreground and background in this image are too similar to achieve a good segmentation. You could finish this segmentation using the Active Contour method, as this example does after freehand segmentation in Segment with Active Contours Technique.
Another technique that you can try is to simply draw the regions that you want to include in the mask image. The Image Segmenter provides tools you can use to draw rectangles, ellipses, polygons, or draw freehand shapes.
Click the Draw freehand option in the Segmentation Tools area. The cursor changes to the cross hairs shape. Move anywhere over the image, press the mouse button, and draw a shape over the image that outlines the object you want to segment. In the following figure, the example uses the freehand option to draw regions. You can see the progress you are making toward your mask image in the History window.
Save the mask image. After drawing all the regions you want to segment, you have created a crude segmentation of the objects. To save this mask image, click Export.
Another technique supported by the Image Segmenter is active contours. With active contours, you draw seed regions that identify the objects you want to segment. Then, using an iterative process, the Image Segmenter grows the regions out to the object borders.
Click the Active Contours option. The Image Segmenter opens the Active Contours tab.
Draw seed shapes in the regions you want to segment. You can use the freehand tool to draw these regions.
Click Evolve to use active contours to grow the regions to fill the objects to their borders. Initially, use the default active contours method (Region-based) and the default number of iterations (100). The Image Segmenter displays the progress of the processing in the lower right corner. Looking at the results, you can see that this approach worked for two of the three objects but the segmentation bled into the background for one of the objects. The object boundary isn't as well defined in this area.
Repeat the active contours segmentation, this time changing the number of iterations. To redo the operation, click Apply, to save the current segmentation, and then choose the previous step in the segmentation History in the Data Browser. This displays the image with the original freehand regions. Change the number of iterations in the iterations box, specifying 35, and click Evolve again. When you are satisfied with the segmentation, click Apply. The color of the regions changes from blue to yellow, indicating that the changes have been applied. To see how to remove the small imperfection in the one of the regions, see Refine the Segmentation.
This part of the example shows how to refine the segmentation of the image. The Image Segmenter app provides several tools that you can use to fill holes and other operations. To illustrate, this example uses the result of the segmentation in Segment with Active Contours Technique.
Upon close examination, one of the mask regions contains a small hole. To improve the initialization mask, click Fill Holes.
Click Fill Holes and the Image Segmenter fills the hole in the region.
When you achieve the segmentation you want, you can create a mask image.
Click Export and select Export Images. In the Export to Workspace dialog box, you can assign names to the initial segmentation mask image, the evolved segmentation mask image, or a segmented version of the original image.
To get the MATLAB code the app used to segment the image, click Export and select the Generate Function option. The app opens the MATLAB editor containing a function with the code required to segment the image.