python - 如何使用分水岭去除脑MRI中的头骨
问题描述
我正在使用 python 检测脑肿瘤。我的代码中的分段过程需要一些帮助。我想在大脑 MRI 中去除头骨部分。我正在使用分水岭算法来定义 MRI 图像中的头骨。然而,肿瘤区域也被代码遮蔽。如何更改代码检测到的阈值,以保持整个大脑区域不受算法影响。下面显示的图片是我想要得到的样本。` import numpy as np import cv2 from matplotlib import pyplot as plt from skimage.morphology import extrema from skimage.morphology import watershed as skwater
def ShowImage(title,img,ctype):
plt.figure(figsize=(10, 10))
if ctype=='bgr':
b,g,r = cv2.split(img) # get b,g,r
rgb_img = cv2.merge([r,g,b]) # switch it to rgb
plt.imshow(rgb_img)
elif ctype=='hsv':
rgb = cv2.cvtColor(img,cv2.COLOR_HSV2RGB)
plt.imshow(rgb)
elif ctype=='gray':
plt.imshow(img,cmap='gray')
elif ctype=='rgb':
plt.imshow(img)
else:
raise Exception("Unknown colour type")
plt.axis('off')
plt.title(title)
plt.show()
img = cv2.imread('s1.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ShowImage('Brain MRI',gray,'gray')
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_OTSU)
ShowImage('Thresholding image',thresh,'gray')
ret, markers = cv2.connectedComponents(thresh)
#Get the area taken by each component. Ignore label 0 since this is the background.
marker_area = [np.sum(markers==m) for m in range(np.max(markers)) if m!=0]
#Get label of largest component by area
largest_component = np.argmax(marker_area)+1 #Add 1 since we dropped zero above
#Get pixels which correspond to the brain
brain_mask = markers==largest_component
brain_out = img.copy()
#In a copy of the original image, clear those pixels that don't correspond to the brain
brain_out[brain_mask==False] = (0,0,0)
img = cv2.imread('s1.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
# noise removal
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)
# sure background area
sure_bg = cv2.dilate(opening,kernel,iterations=3)
# Finding sure foreground area
dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
ret, sure_fg = cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0)
# Finding unknown region
sure_fg = np.uint8(sure_fg)
unknown = cv2.subtract(sure_bg,sure_fg)
# Marker labelling
ret, markers = cv2.connectedComponents(sure_fg)
# Add one to all labels so that sure background is not 0, but 1
markers = markers+1
# Now, mark the region of unknown with zero
markers[unknown==255] = 0
markers = cv2.watershed(img,markers)
img[markers == -1] = [255,0,0]
im1 = cv2.cvtColor(img,cv2.COLOR_HSV2RGB)
ShowImage('Watershed segmented image',im1,'gray')
brain_mask = np.uint8(brain_mask)
kernel = np.ones((8,8),np.uint8)
closing = cv2.morphologyEx(brain_mask, cv2.MORPH_CLOSE, kernel)
ShowImage('Closing', closing, 'gray')
brain_out = img.copy()
#In a copy of the original image, clear those pixels that don't correspond to the brain
brain_out[closing==False] = (0,0,0)`
解决方案
推荐阅读
- c++ - 使用 LASlib 时的 BOOL typedef 重新定义
- node.js - eslint 命令在 CI 服务器上失败,出现错误“ESLint 找不到要扩展的配置“标准””
- reactjs - 从 api 获取对象数组(嵌套),循环 3 次,意见,reactjs
- google-chrome - 为什么每次打开我的 PWA 时都会出现“点击以复制此应用的 URL”通知
- javascript - 仅将一些组合的 DIV 框悬停在一行中
- excel - Excel ADODB 连接到 csv 不返回所有记录
- java - System.out.print 语句的意外输出
- python - 读取具有 glob 重复列的多个文件
- reporting-services - 如何计算 SSRS 报告中的小时和分钟平均值
- maven - 在运行时范围内包括 test-jar