如何解决仅获取肺部的二进制图像
我有一个问题,我正在努力制作肺部的纯二进制掩码,其中像素值是肺部内部的 1,肺部外部的 1。我使用了 kmeans 和 otsu 以及其他一些方法来分割肺部。我将附上一些示例图片。
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?),如果有人能指导我,我将非常感谢您的帮助。
这是我用来分割肺的代码(制作二进制掩码):
定义 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]
#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
解决方法
由于肺部位于面罩上一个大的负区域的中间,我通过对图像中最大负区域内的区域进行 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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。