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 uses the
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
If you want to use Image Segmenter tools to refine an existing binary mask for an image that you have already segmented, first load the image. The Image Segmenter does not enable the Load Mask button until you load an image. After you load the image, use Load Mask to load the binary mask. See Refine an Existing Segmentation Mask Image for more information.
Because you already loaded the knee CT 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. The app displays 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 the following segmentation tools that you can use.
Auto Cluster—An automatic technique where the app groups image features into a binary segmentation. This option is only available if you have the Statistics and Machine Learning Toolbox. For more information, see Segment using Autoclustering.
Threshold—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. For more information, see Segment Using Threshold Technique.
Graph Cut—A semi-automatic technique that can segment foreground and background. This technique does not require careful seed points and you can refine the segmentation interactively. For more information, see Segment with Graph Cut Technique.
Draw Freehand—A manual technique where you draw regions outlining the objects you want to segment. Using the mouse, you can draw rectangles, ellipses, polygons, or freehand shapes. For more information, see Segment By Drawing Regions Freehand.
Flood Fill—An automatic technique where you specify starting points and the method segments areas with similar intensity values.
Find Circles—An automatic technique where you specify the minimum and maximum diameter of the circular objects you want to detect.
This example illustrates how segmentation is an iterative process where you might try several options until you achieve the result you want. Some techniques might work better with certain types of images than others. When using Auto Cluster, Graph Cut, and Flood Fill segmentation, you can also include texture as an additional consideration. Click Include Texture Features to turn the texture option on and off.
The Auto Cluster option requires the Statistics and Machine Learning Toolbox. If you don't have this toolbox, the Image Segmenter does not include this option. Generated code will also require Statistics and Machine Learning Toolbox.
The Auto Cluster option is an automatic technique that segments the foreground from the background. You can use the Include Texture Features option with Auto Cluster. The Auto Cluster does not work well the knee image used in this example. For an example of using Auto Cluster, see Segment Image Using Auto Cluster.
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: Global, Adaptive, and Manual. Try each option. Experiment with the optional parameters available with each option. For example, with the Manual option you can use a slider to specify the threshold value. The knee image does not have well-defined pixel intensity differences between foreground and background. Thresholding does not seem like the best choice to segment this image.
Click Cancel to return to the main segmentation app window without accepting the result and try one of the other segmentation options. If you had wanted to keep the thresholded mask image, click Create Mask.
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. You can use the Include Texture Features option with Graph Cut.
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 areas on the image that you want included as background. 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 refine this segmentation using the Active Contour method. See Use Active Contours to Refine a Segmentation for more information.
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 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.
The Find Circles option is an automatic technique that segments circular objects in an image. The Find Circles option does not work well the knee image used in this example. For an example of using Find Circles, see Segment Image Using Find Circles.
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, finish a rough approximation using Active Contours, and other operations. To illustrate, this example uses the result of the segmentation in Segment By Drawing Regions Freehand.
Load an existing image mask into the Image Segmenter. When you first load an image into the Image Segmenter, and before you start defining objects, the Image Segmenter enables the Load Mask option. You can load an existing segmentation mask image into the Image Segmenter and refine the segmentation using tools in the refine Refine Mask toolbar group. The segmentation mask image must be a logical image the same size as the image you are segmenting.
For example, you might have previously created a mask of an RGB image in the Color Thresholder and you want to refine the segmentation. Bring the RGB image into the Image Segmenter and then load the mask image. You can then use the tools available in the Image Segmenter to refine the mask.
For example, you might have previously created a segmentation mask image of an RGB color image and you want to refine the segmentation. Bring the RGB image into the Image Segmenter and then load the pre-existing mask image. You can then use the tools available to refine the mask.
Active contours is an automatic, iterative method where you mark locations in the image and active contours grows (or shrinks) the regions identified in the image. To use active contours, you must have a rough segmentation already. The accuracy of this initial seed mask can impact the final result after active contours. You can use the Include Texture Features option with Active Contours.
Draw seed shapes in the regions you want to segment. You can use the freehand tool to draw these regions. See Segment By Drawing Regions Freehand to see how this was accomplished.
Click the Active Contours option. The Image Segmenter opens the Active Contours tab.
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 Use Morphological Techniques to Refine a Segmentation.
The segmentation mask image you created in the segmentation step (Segment the Image in the Image Segmenter) might have slight imperfections that you'd like to fix. The Image Segmenter includes morphological tools, such as dilation and erosion, on the Morphology tab, and options like Fill Holes and Clear Borders on the Segmentation tab. You can use these tools to improve your mask image.
Upon close examination, one of the mask regions (created in Segment the Image in the Image Segmenter) contains a small hole.
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.