我有一个问题,我正在努力制作一个纯粹的肺部二进制蒙版,其中像素值是肺部内部的 1 并且是肺部外部的 1。我使用了 kmeans 和 otsu 以及其他一些方法来分割肺部。我将附上一些示例图片。
第二个例子,相同的患者/CT。我不知道为什么这个周围有一个圆圈
这是 3d numpy 数组的链接。它是所有切片的,所以你可能只想尝试一个切片。
https://drive.google.com/file/d/1nktGBYZGz1iJDR_-yarzlRs-c4xOp__9/view?usp=sharing
如您所见,肺被很好地分割。(图片中间是白色的)。有什么方法可以让我识别出中间的白色斑点(肺)并将其外部的每个像素都变成黑色(0?)如果有人可以指导我,我将非常感谢您的帮助。
这是我用来分割肺的代码(制作二进制掩码):
def HUValueSegmentation(图像,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