首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅获取肺部的二值图像

仅获取肺部的二值图像
EN

Stack Overflow用户
提问于 2021-02-26 12:56:38
回答 1查看 166关注 0票数 0

我遇到了一个问题,我正在努力制作肺的纯二进制掩模,其中像素值在肺内是1,在肺外是1。我使用了kmeans和otsu以及其他一些方法来分割肺部。我会附上一些例子图片。

First Example

Second example, same patient/CT. I have no idea why this one has a circle around it

这是一个3d numpy数组的链接。它是所有切片中的一个,所以您可能只想尝试一个切片。

https://drive.google.com/file/d/1nktGBYZGz1iJDR_-yarzlRs-c4xOp__9/view?usp=sharing

如你所见,肺被分割得很好。(图片中间是白色的)。有没有办法让我识别中间的白色斑点(肺),并使其外部的每个像素都变黑(0?)如果有人能给我指路,我将非常感激。

下面是我用来分割肺部的代码(生成一个二进制掩码):

def HUValueSegmentation(图片,fill_lung_structures=True):

代码语言:javascript
复制
# 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
EN

回答 1

Stack Overflow用户

发布于 2021-02-27 00:19:07

由于肺部在蒙版上的一个大的负片区域的中间,我通过对图像中最大的负片区域内的区域进行bitwise_and来过滤掉蒙版的其余部分。

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

代码语言:javascript
复制
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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66380360

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档