Main Content

Create Binary Mask Using Volume Segmenter

This example shows how to segment a volume in the Volume Segmenter app. The Volume Segmenter app offers many ways to explore a volume and segment objects in the volume. For example, you can view the volume slice-by-slice or as a 3-D representation. To segment an object, you can draw a region of interest (ROI) using ROI drawing tools or a paint brush tool. This example creates a binary mask that isolates a small region of the brain.

Load Volumetric Data into the Workspace

Load a volume into the workspace. This example uses a stack of MRI brain images, stored in the MAT-file vol_001.mat. The MRI data is a modified subset of the BraTS data set [1].


This command loads a 240-by-240-by-155 volume named vol into the workspace.

whos vol
  Name        Size                    Bytes  Class     Attributes

  vol       240x240x155            17856000  uint16              

Open the Volume Segmenter

Open the Volume Segmenter app. Click the Apps tab on the MATLAB® toolstrip. In the Image Processing and Computer Vision section, click Volume Segmenter.


Load the Volume into the Volume Segmenter

To load the volume in the Volume Segmenter app, click Open Volume in the app toolstrip. For this example, select Open from Workspace. In the Import Volume dialog box, select the volume you loaded into the workspace, vol, and click OK. (You can also specify a volume when you open the app by using the volumeSegmenter command: volumeSegmenter(vol).)

The Volume Segmenter app displays a 3-D representation of the volume in the 3-D Display pane and displays individual slices of the data set in the Slice pane.


By default, the Slice pane displays the first slice of your data. The app displays the number of the slice displayed at the top of the image, for example, 1/155. In this dataset, the first few slices do not contain images of the brain.

The app also automatically creates a label for the segmentation in the Labels pane, using the default name Label1. You can define multiple labels in the Labels pane. However, to create a binary mask, you must use only one label.

To change the name of the label, double-click the label name. To change the color associated with the label, double-click the color square displayed in the Labels pane. You can optionally load an existing set of labels into the app using the Open Labels button.

Explore the Volume

To determine what you want to segment, explore the volume using the 3-D Display pane and the Slice pane.

In the 3-D Display pane, you can rotate the volume to examine the data from every angle, using the mouse. You can also customize the display of the volume in the 3-D Display tab in the app toolstrip. For example, if you have metadata that describes the relative size of the voxels, you can specify it in the Spatial Referencing part of the 3-D Display tab in the app toolstrip. To improve your view of the data, you can change the background color used in the 3-D display, modify the threshold and opacity of the display, and include orientation axes with the display, as shown in the figure below. With the brain MRI data, you can see the tumor in the temporal lobe that you want to segment.


You can also view each slice of the volume in the Slice pane. Use the slider at the bottom of the pane to move from slice to slice. You can see the tumor on slice 35 through slice 88. By default, the Slice pane displays the volume oriented along the X-Y axis, but you can change this using buttons in the Orientation section of the toolstrip on the Segmenter tab. The Slice pane is also where you use drawing tools to define the mask.


Use Drawing Tools to Define the Mask

Once you have identified the object you want to segment, you can use the tools on the Draw tab in the app toolstrip to define the region. Select the drawing tool you want to use from the ROI tools: Freehand, Assisted Freehand, and Polygon, and a Paint Brush tool.

In the Slice pane, navigate to the slice where the object first appears, slice 35, and draw an outline around the object. For this example, use the Polygon drawing tool. Click to create a vertex, then move the cursor and click again to create a second vertex with a straight line connecting them. Continue this process to create a connected line. To add additional vertices after you finish drawing, double-click on the ROI edge.


Using Interpolation to Speed Object ROI Creation

You can move through the volume, slice-by-slice, and draw an ROI on each slice where the object appears. However, the Volume Segmenter app provides several automated interpolation tools that can help with segmenting an object across slices.

To use interpolation, you must first manually define the region on two slices. You have already defined the region on the first slice where the object appears, slice 35. Use the same process to define the region on the last slice where it appears, slice 88. The app places two bars on top of the slider, using the color associated with the label, to indicate the slices with ROIs.


With the object defined on two slices, click Auto Interpolate. The app automatically defines the ROI on all the intervening slides. The app uses blue bars to indicate all the slices that have ROIs, which now appear as a solid bar from slice 35 to slice 88.


Alternatively, after defining an ROI on two slices, you can click Manually Interpolate. With this option, the app opens the Manually Interpolate dialog box. You select the two regions from which you want to interpolate, Region One and Region Two. To select the first region, use the slider at the bottom of the dialog box to navigate to the first slice with an ROI, slice 35, and then click inside the ROI displayed. To select the second region, click Region Two, navigate to slide 88, and click inside the ROI displayed. After selecting both regions, click Run to interpolate the ROI on all intervening slices.


Refine the Interpolated ROIs

After using interpolation, check the individual slices to see if the interpolation created satisfactory ROIs. Note that the ROI on slice 71 does not fill the entire object that you want to segment. You can manually adjust the ROI using the Paint Brush tool. Alternatively, you can use one of the tools on Automate tab. For example, you can use Active Contours to grow the ROIs on the slices where it does not fill the full size of the tumor. You can also use the Add Algorithm to specify your own algorithm to operate on the ROIs.


Perform Custom Processing

You can add your own algorithm to operate on the ROIs. On the Automate tab, click Add Algorithm. Choose whether you want your processing to operate on each 2-D slice (Slice-based) or on the entire 3-D volume (Volume-based).


For this example, under Slice-Based, select the New option and click Function Template to create a new function that operates on each 2-D slice. The app opens the template in the MATLAB editor. Replace the sample code in the template with code that you want to use. Your function must accept two arguments: each slice as a separate image and a mask. Your function must also return a mask image.

When you are done editing the template, save the file. The Volume Segmenter app automatically creates a button in the Automate tab toolstrip for your function. To test your function on one slice, click Run. By default, the app applies the function to only the current slice.


After testing your function on a single slice, you can run it on all of the slices or a subset of the slices. You can run it from the current slice to the end (the highest numbered slice) or from the current slice back to the beginning (slice 1). You can also specify a range of slices by specifying the starting slice and the ending slice.


When you choose one of the directional options, the app updates the slice numbers in the display. You can use this display to view the progress of processing.


Create the Binary Mask Volume

To create the binary mask volume, click Save Labels on the Segmenter tab. You can save the mask to a MAT-file or to a workspace variable. For this example, click Save As Workspace Variable. In the Save to Workspace dialog box, specify whether you want to save the segmentation as a logical or categorical mask. Choose logical (the default when there is only one label), give the variable a name, my_mask_volume, and click OK. The app creates a 3-D volume of class logical with the same dimensions as the original volume.

To view the mask, use the volshow function: volshow(my_mask_volume);.



[1] Medical Segmentation Decathlon. "Brain Tumours." Tasks. Accessed May 10, 2018.

The BraTS data set is provided by Medical Segmentation Decathlon under the CC-BY-SA 4.0 license. All warranties and representations are disclaimed. See the license for details. MathWorks® has modified the subset of data used in this example. This example uses the MRI data of one scan from the original data set, saved to a MAT file.

See Also

Related Topics