Я следовал коду в этом уроке для ноутбука и застрял в рендеринге 3d сегментации легких
https://www.kaggle.com/gzuidhof/full-preprocessing-tutorial
Я ознакомился с руководством, за исключением следующих изменений:
- Я импортировал pydicom вместо dicom
- Впоследствии я изменил dicom.read_file () на pydicom.dcmread ()
Это ожидаемое изображение, так должно отображаться мое изображение легкого.
Это то, что я получаю вместо
plot_3d(segmented_lungs, 0)
Тем не менее, простой трехмерный график сканирования выглядит хорошо.
Это учебное рендеринг данных:
Это мой рендеринг данных. так что все было хорошо, пока не произошла сегментация легких.
Что-то идет не так в коде сегментации легких.
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)