首页 > 解决方案 > 为什么这个图像对我来说是错误的?

问题描述

我已按照本笔记本教程中的代码进行操作,但我一直在渲染 3d 肺分割

https://www.kaggle.com/gzuidhof/full-preprocessing-tutorial

除了以下更改外,我已按照教程进行操作:

  1. 我已经导入了 pydicom 而不是 dicom
  2. 我随后将 dicom.read_file() 更改为 pydicom.dcmread()

这是预期的图像,这是我的肺部图像必须呈现的方式 在此处输入图像描述

这就是我得到的渲染 在此处输入图像描述

plot_3d(segmented_lungs, 0)

但是,扫描的简单 3d 图看起来不错。

这是数据的教程渲染: 在此处输入图像描述

这是我对数据的渲染。所以在肺分割发生之前一切都很好。 在此处输入图像描述

肺分割代码出了点问题。

def largest_label_volume(im, bg=-1):
vals, counts = np.unique(im, return_counts=True)

counts = counts[vals != bg]
vals = vals[vals != bg]

if len(counts) > 0:
    return vals[np.argmax(counts)]
else:
    return None

def segment_lung_mask(image, fill_lung_structures=True): 

# not actually binary, but 1 and 2. 
# 0 is treated as background, which we do not want
binary_image = np.array(image > -320, dtype=np.int8)+1
labels = measure.label(binary_image)

# Pick the pixel in the very corner to determine which label is air.
#   Improvement: Pick multiple background labels from around the patient
#   More resistant to "trays" on which the patient lays cutting the air 
#   around the person in half
background_label = labels[0,0,0]

#Fill the air around the person
binary_image[background_label == labels] = 2


# Method of filling the lung structures (that is superior to something like 
# morphological closing)
if fill_lung_structures:
    # For every slice we determine the largest solid structure
    for i, axial_slice in enumerate(binary_image):
        axial_slice = axial_slice - 1
        labeling = measure.label(axial_slice)
        l_max = largest_label_volume(labeling, bg=0)

        if l_max is not None: #This slice contains some lung
            binary_image[i][labeling != l_max] = 1


binary_image -= 1 #Make the image actual binary
binary_image = 1-binary_image # Invert it, lungs are now 1

# Remove other air pockets insided body
labels = measure.label(binary_image, background=0)
l_max = largest_label_volume(labels, bg=0)
if l_max is not None: # There are air pockets
    binary_image[labels != l_max] = 0

return binary_image


segmented_lungs = segment_lung_mask(pix_resampled, False)
segmented_lungs_fill = segment_lung_mask(pix_resampled, True)


plot_3d(segmented_lungs, 0)

标签: image-processingkaggleimage-preprocessing

解决方案


推荐阅读