Image Action Set

Annotating a Medical Image with Its Segmentation

This example is not available for the CAS programming language.

Annotating a Medical Image with Its Segmentation

This example is not available for the Lua programming language.

Annotating a Medical Image with Its Segmentation

This section contains Python code. For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.

The following Python code loads a 2-D CT image of human lungs (Armato et al. 2015a, 2015b; Armato et al. 2016; Clark et al. 2013), performs watershed segmentation, and overlays the segmentation on the CT image.[9]Figure 5 and Figure 6 show the input and output images.

# Load a medical image
imdata = s.CASTable('imdata', replace=True)
s.image.loadimages(path='lung_for_annotation.nii',
                   casout=imdata,
                  )

# Compute the gradient magnitudes, copy the original image
cvars = ['vcimage']
cvarspgm = "length vcimage varchar(*); vcimage=_image_;"
imdata.append_computed_columns(cvars, cvarspgm)
pdata = s.CASTable('pdata', replace=True)
s.biomedimage.processbiomedimages(
    images=dict(table=imdata),
    steps=[
        dict(stepparameters=dict(
            steptype='gradient_magnitude',
            gradientmagnitudeparameters=dict(
                gradienttype='basic'))),
    ],
    copyvars={'vcimage'},
    casout=pdata)

# Segment using the watershed method, copy the original image
sdata = s.CASTable('sdata', replace=True)
s.biomedimage.segmentbiomedimages(
    images=dict(table=pdata),
    method=dict(
        name='watershed',
        watershedparameters=dict(
        level=0.4)),
    copyvars={'vcimage'},
    casout=sdata)

# Export the segmentation, copy the original image
sdata_exp1 = s.CASTable('sdata_exp1', replace=True)
s.biomedimage.processbiomedimages(
    images=dict(table=sdata),
    steps=[
        dict(stepparameters=dict(
            steptype='export'))
    ],
    copyvars={'vcimage'},
    casout=sdata_exp1)

# Export the original image, copy the segmentation
cvars = ['vcimage_seg']
cvarspgm = "length vcimage_seg varchar(*); vcimage_seg=_image_;"
sdata_exp1.append_computed_columns(cvars, cvarspgm)
sdata_exp2 = s.CASTable('sdata_exp2', replace=True)
s.biomedimage.processbiomedimages(
    images=dict(table=sdata_exp1, image='vcimage'),
    steps=[
        dict(stepparameters=dict(
            steptype='export'))
    ],
    copyvars={'vcimage_seg'},
    casout=sdata_exp2)

# Do annotation
adata = s.CASTable('adata', replace=True)
s.image.annotateimages(
    images=dict(table=sdata_exp2),
    annotations=[
        dict(annotationparameters=dict(
            annotationtype='segmentation',
            image='vcimage_seg',
            transparency=75,
            colormap='jet'))
    ],
    decode=True,
    casout=adata)

Figure 5: The 2-D CT Image of Human Lungs

The 2-D CT Image of Human Lungs


Figure 6: The 2-D CT Image of Human Lungs Annotated with Its Watershed Segmentation

The 2-D CT Image of Human Lungs Annotated with Its Watershed Segmentation


Annotating a Medical Image with Its Segmentation

This example is not available for the R programming language.



[9] Disclaimer: SAS may reference other websites or content or resources for use at Customer's sole discretion. SAS has no control over any websites or resources that are provided by companies or persons other than SAS. Customer acknowledges and agrees that SAS is not responsible for the availability or use of any such external sites or resources, and does not endorse any advertising, products, or other materials on or available from such websites or resources. Customer acknowledges and agrees that SAS is not liable for any loss or damage that may be incurred by Customer or its end users as a result of the availability or use of those external sites or resources, or as a result of any reliance placed by Customer or its end users on the completeness, accuracy, or existence of any advertising, products, or other materials on, or available from, such websites or resources.

Last updated: September 15, 2022