0

I have followed the code in this notebook tutorial and I am stuck in rendering the 3d lung segmentation

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

I have followed the tutorial except for the following changes:

  1. I have imported pydicom instead of dicom
  2. I have subsequently changed dicom.read_file() to pydicom.dcmread()

This is the expected image, this is how my lung image must render enter image description here

This is what I get rendered instead enter image description here

plot_3d(segmented_lungs, 0)

However, the simple 3d plot of the scan looks good.

This is the tutorial rendering of the data: enter image description here

This is my rendering of the data. so all was good till lung segmentation happened. enter image description here

Something is going wrong in the lung segmentation code.

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)
desertnaut
  • 57,590
  • 26
  • 140
  • 166
divinediu
  • 423
  • 1
  • 9
  • 33
  • My renders are far off from the tutorial @user1118321. It is the same dataset, the same code. I can't figure out why. I also added more detail in the text above – divinediu Jan 17 '19 at 06:46
  • 1
    Awesome! The extra detail should help readers understand the issue now. – user1118321 Jan 17 '19 at 06:51

0 Answers0