main content

segment lungs from 3-凯发k8网页登录

this example shows how to perform a 3-d segmentation using active contours (snakes) and view the results using the volume viewer app.

prepare the data

load the human chest ct scan data into the workspace. to run this example, you must download the sample data from mathworks™ using the add-on explorer. see .

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 to the range [0, 1].

v = im2single(v);

view the chest scans using the volume viewer app.

volumeviewer(v)

volume viewer has preset alphamaps that are intended to provide the best view of certain types of data. to get the best view of the chest scans, select the ct-bone preset.

segmentlungsfrom3dchestscanexample_01.png

segment the lungs

segment the lungs in the ct scan data using the active contour technique. active contours 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 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,:,:));

view the 2-d slices using the imshow function.

figure
imshow(xy,[],"border","tight")
imshow(xz,[],"border","tight")

you can perform the segmentation in the image segmenter app. open the app using the imagesegmenter command, specifying a 2-d slice as the input argument.

imagesegmenter(xy)

to start the segmentation process, click threshold to open the lung slice in the threshold tab. on the threshold tab, select the manual threshold option and move the threshold slider to specify a threshold value that achieves a good segmentation of the lungs. click create mask to accept the thresholding and return the segmentation tab.

the app executes the following code to threshold the image.

bw = xy > 0.5098;

after this initial lung segmentation, clean up the mask using options on the refine mask menu.

in the app, you can click each option to invert the mask image so that the lungs are in the foreground (invert mask), remove other segmented elements besides the lungs (clear borders), and fill holes inside the lung segmentation (fill holes). finally, use the morphology option to smooth the edges of the lung segmentation. on the morphology tab, select the erode mask operation. after performing these steps, select show binary and save the mask image to the workspace.

the app executes the following code to refine the mask.

bw = imcomplement(bw);
bw = imclearborder(bw);
bw = imfill(bw, "holes");
radius = 3;
decomposition = 0;
se = strel("disk",radius,decomposition);
bw = imerode(bw, se);
maskedimagexy = xy;
maskedimagexy(~bw) = 0;
imshow(maskedimagexy)

perform the same operation on the xz slice. using load image, select the xz variable. use thresholding to perform the initial segmentation of the lungs. for the xz slice, the global threshold option creates an adequate segmentation (the call to imbinarize in the following code). as with the xy slice, use options on the refine mask menu to create a polished segmentation of the lungs. in the erosion operation on the morphology tab, specify a radius of 13 to remove small extraneous objects.

to segment the xz slice and polish the result, the app executes the following code.

bw = imbinarize(xz);
bw = imcomplement(bw);
bw = imclearborder(bw);
bw = imfill(bw,"holes");
radius = 13;
decomposition = 0;
se = strel("disk",radius,decomposition);
bw = imerode(bw, se);
maskedimagexz = xz;
maskedimagexz(~bw) = 0;
imshow(maskedimagexz)

create seed mask and segment lungs using activecontour

create the 3-d seed mask that you can use with the activecontour function to segment the lungs.

create a logical 3-d volume the same size as the input volume and insert mask_xy and mask_xz at the appropriate spatial locations.

mask = false(size(v));
mask(:,:,160) = maskedimagexy;
mask(256,:,:) = mask(256,:,:)|reshape(maskedimagexz,[1,512,318]);

using this 3-d seed mask, segment the lungs in the 3-d volume using the active contour method. this operation can take a few minutes. to get a quality segmentation, use 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.

volumeviewer(segmentedimage)

by manipulating the alphamap settings in the rendering editor, you can get a good view of just the lungs.

compute the volume of the segmented lungs

use the function with the "volume" option to calculate the volume of the lungs.

vollungspixels = regionprops3(logical(bw),"volume");

specify the spacing of the voxels in the x, y, and z dimensions, which was gathered from the original file metadata. the metadata is not included with the image data that you download from the add-on explorer.

spacingx = 0.76;
spacingy = 0.76;
spacingz = 1.26*1e-6;
unitvol = spacingx*spacingy*spacingz;
vollungs1 = vollungspixels.volume(1)*unitvol;
vollungs2 = vollungspixels.volume(2)*unitvol;
vollungsliters = vollungs1   vollungs2
vollungsliters = 5.7726

see also

| |

网站地图