# Documentation

### This is machine translation

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

## Segment Lungs from 3-D Chest Scan and Calculate Lung Volume

This example shows how to perform a 3-D segmentation using active contours and view the results using the Volume Viewer app.

### Prepare the Data

This part of the example loads the human chest CT scan data into the MATLAB workspace. To run this example, you must download the sample data from the MathWorks using the Add-Ons Explorer. See Install Sample Data Using the Add-Ons Explorer.

Load the 3-D volumetric CT scan data into the MATLAB work space.

```load chestVolume whos```
```Name Size Bytes Class Attributes V 512x512x318 166723584 int16 ```

Convert the CT scan data from `int16` to `single` to normalize the values between ```[0 1]```.

```V = im2single(V); whos ```
``` Name Size Bytes Class Attributes V 512x512x318 333447168 single ```

Visualize the chest scans using the Volume Viewer app.

```volumeViewer(V) ```

### Step 1: Segment the Lungs

This part of the example shows how to segment the lungs in the CT scan data using the active contour technique. This is a region growing algorithm which requires initial seed points. The example uses the Image Segmenter app to create this seed mask by segmenting two, orthogonal 2-D slices, one in the XY plane and the other in the XZ plane. The example then inserts these two segmentations into a 3-D mask. The example passes this mask to the `activecontour` function to create a 3-D segmentation of the lungs in the chest cavity. (This example uses the active contour method but you could use other segmentation techniques to accomplish the same goal, such as, flood-fill.)

Extract the center slice in both the XY and XZ dimensions.

```XY = V(:,:,160); XZ = squeeze(V(256,:,:));```

Visualize the 2-D slices using the `imshow` function.

```figure, imshow(XY, []); figure, imshow(XZ, []);```

Open the XY slice in the Image Segmenter app and click Threshold to start the segmentation process.

`imageSegmenter(XY)`

On the Threshold tab, choose Manual Threshold and move the Threshold slider to adjust the segmentation until the lungs appear well-delineated. It is OK if other objects appear segmented in the thresholded region. Click Apply to save the result and then choose Close Threshold.

On the Segmentation tab, click Invert Mask to make the segmented lungs the foreground. Remove all the segmented parts that are not the lungs. Since these all touch the edges, use the Clear Borders option to remove them. Then fill the small holes that appear in the lung areas using the Fill Holes option.

Remove small extraneous areas of the segmentation using erosion. Click Morphology and on the Morphology tab, click Erode Mask from the Operation selections. After this processing, click Apply and close the Morphology tab. On the Segmentation tab, click Show Binary to view the mask you created. To save the mask, click Export and save the final segmentation under the name mask_XY.

Segment the XZ slice using the same procedure. Open the XZ slice in the Image Segmenter app. Choose Threshold. With this image, you can use the Global Thresholding option. Click Apply and Close Threshold. On the Segmentation tab, click Invert Mask to make the lungs the foreground. Click Clear Borders option to remove extraneous regions that were included in the thresholding. Fill small holes in the lung areas using the Fill Holes option. To remove the small segmented regions that remain that are not the lungs, click Morphology and select Erode Mask. Increase the radius until these extraneous regions are gone. Click Apply and close the Morphology tab. On the Segmentation tab, click Show Binary to view the mask you created. Click Export and save the final segmentation under the name mask_XZ.

Create a 3-D seed mask to be used with the `activecontour` function. First, create a logical 3-D volume the same size as the input volume and insert `mask_XY` and `mask_XY` at the appropriate spatial locations.

```mask = false(size(V)); mask(:,:, 160) = mask_XY; mask(256, :, :) = mask(256, :, :)|reshape(mask_XZ, [1, 512, 318]); ```

Perform a 3-D segmentation of the lungs using the active contour method. This can take a few minutes. To get a quality segmentation, use `histeq` to spread voxel values over the available range.

```V = histeq(V); BW = activecontour(V,mask,100,'Chan-Vese'); segmentedImage = V.*single(BW); ```

View the segmented lungs in the Volume Viewer app. Use the alphamap plot in the Rendering Editor to manipulate the opacity mapping until you get a good view of the lungs.

```volumeViewer(segmentedImage); ```

### Step 2: Compute the Volume of the Segmented Lungs

In this part of the example, use the `regionprops` function to calculate the volume (area) of the lungs.

Calculate the area of the lungs using the `regionprops` function with the `'area'` option.

`volLungsPixels = regionprops(logical(BW), 'area');`

Specify the spacing of the voxels in the x, y, and z dimensions. (Gathered from the file metadata previously.)

```xSpacing = 0.76; %(mm) ySpacing = 0.76; %(mm) zSpacing = 1.25; %(mm) ```

Calculate lung capacity, in liters.

`volLungsLiters = volLungsPixels.Area*xSpacing*ySpacing*zSpacing*1e-6;`
```volLungsLiters = 6.7157```