0

我有一个问题,我正在努力制作一个纯粹的肺部二进制蒙版,其中像素值是肺部内部的 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
4

1 回答 1

1

由于肺部位于面具上一个大的负区域的中间,我通过对图像中最大负区域内的区域进行 bitwise_and 过滤掉了面具的其余部分。

在此处输入图像描述

在此处输入图像描述

编辑:我根本没有更改代码的主体,但我修改它以将 numpy 数组作为一系列图像。

在此处输入图像描述

import cv2
import numpy as np

# load numpy array
images = np.load("array.npy");

# do the lung thing
counter = 0;
for img in images:
    # convert to uint8
    img *= 255;
    inty = img.astype(np.uint8);

    # dilate
    kernel = np.ones((3,3), np.uint8);
    mask = cv2.dilate(inty, kernel, iterations = 1);

    # invert
    mask = cv2.bitwise_not(mask);

    # contours # OpenCV 3.4, this returns (contours, _) on OpenCV 2 and 4
    _, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE);

    # find biggest
    biggest = None;
    big_size = -1;
    for con in contours:
        area = cv2.contourArea(con);
        if area > big_size:
            big_size = area;
            biggest = con;

    # draw fill mask
    mask2 = np.zeros_like(mask);
    cv2.drawContours(mask2, [biggest], -1, (255), -1);

    # combine
    lungs_mask = cv2.bitwise_and(inty, mask2);

    # show
    cv2.imshow("Lungs", inty);
    cv2.imshow("Mask", lungs_mask);
    cv2.waitKey(30);
于 2021-02-26T16:19:06.990 回答