【医学图像处理】之腹部骨骼提取(SimpleITK)

【医学图像处理】之腹部骨骼提取(SimpleITK)

1.内容

步骤:
1.读取Dicom序列
2.设置固定阈值为100,把骨骼和心脏及主动脉都分割出来
3.形态学开运算+最大连通域提取,粗略的心脏和主动脉图像
4.将step1的结果与step2的结果相减,得到骨骼部分
5.最大连通域提取,去除小连接
6.将得到的图像与原始图像进行逻辑与操作
数据地址:
链接:https://pan.baidu.com/s/198H5g30LSKrKInJfgV1xFQ
提取码:a3nw

2.代码

import SimpleITK as sitk

# 最大连通域提取
def GetLargestConnectedCompont(binarysitk_image):
    cc = sitk.ConnectedComponent(binarysitk_image)
    stats = sitk.LabelIntensityStatisticsImageFilter()
    stats.SetGlobalDefaultNumberOfThreads(8)
    stats.Execute(cc, binarysitk_image)
    maxlabel = 0
    maxsize = 0
    for l in stats.GetLabels():
        size = stats.GetPhysicalSize(l)
        if maxsize < size:
            maxlabel = l
            maxsize = size
    labelmaskimage = sitk.GetArrayFromImage(cc)
    outmask = labelmaskimage.copy()
    outmask[labelmaskimage == maxlabel] = 255
    outmask[labelmaskimage != maxlabel] = 0
    outmask_sitk = sitk.GetImageFromArray(outmask)
    outmask_sitk.SetDirection(binarysitk_image.GetDirection())
    outmask_sitk.SetSpacing(binarysitk_image.GetSpacing())
    outmask_sitk.SetOrigin(binarysitk_image.GetOrigin())
    return outmask_sitk

# 逻辑与操作
def GetMaskImage(sitk_src, sitk_mask, replacevalue=0):
    array_src = sitk.GetArrayFromImage(sitk_src)
    array_mask = sitk.GetArrayFromImage(sitk_mask)
    array_out = array_src.copy()
    array_out[array_mask == 0] = replacevalue
    outmask_sitk = sitk.GetImageFromArray(array_out)
    outmask_sitk.SetDirection(sitk_src.GetDirection())
    outmask_sitk.SetSpacing(sitk_src.GetSpacing())
    outmask_sitk.SetOrigin(sitk_src.GetOrigin())
    return outmask_sitk


# 读取Dicom序列
pathDicom = 'D:/PyCharm 2019.3.3/data/LIDC_nodul'
reader = sitk.ImageSeriesReader()
filenamesDICOM = reader.GetGDCMSeriesFileNames(pathDicom)
reader.SetFileNames(filenamesDICOM)
sitk_src = reader.Execute()

# step1.设置固定阈值为100,把骨骼和心脏及主动脉都分割出来
sitk_seg = sitk.BinaryThreshold(sitk_src, lowerThreshold=100, upperThreshold=3000, insideValue=255, outsideValue=0)
sitk.WriteImage(sitk_seg, 'step1.mha')

# step2.形态学开运算+最大连通域提取,粗略的心脏和主动脉图像
sitk_open = sitk.BinaryMorphologicalOpening(sitk_seg != 0, 2)
sitk_open = GetLargestConnectedCompont(sitk_open)
sitk.WriteImage(sitk_open, 'step2.mha')

# step3.再将step1的结果与step2的结果相减,得到骨骼部分
array_open = sitk.GetArrayFromImage(sitk_open)
array_seg = sitk.GetArrayFromImage(sitk_seg)
array_mask = array_seg - array_open
sitk_mask = sitk.GetImageFromArray(array_mask)
sitk_mask.SetDirection(sitk_seg.GetDirection())
sitk_mask.SetSpacing(sitk_seg.GetSpacing())
sitk_mask.SetOrigin(sitk_seg.GetOrigin())
sitk.WriteImage(sitk_mask, 'step3.mha')

# step4.最大连通域提取,去除小连接
skeleton_mask = GetLargestConnectedCompont(sitk_mask)
sitk.WriteImage(skeleton_mask, 'step4.mha')

# step5.将得到的图像与原始图像进行逻辑与操作
sitk_skeleton = GetMaskImage(sitk_src, skeleton_mask, replacevalue=-1500)
sitk.WriteImage(sitk_skeleton, 'step5.mha')

3.结果

原图:

step1:

step2:

step3:

step4:

step5:

你可能感兴趣的:(医学影像)