医疗影像处理:去除医疗影像中背景的影响2D/3D【numpy-code】| CSDN博文精选

news/2024/7/5 1:52:32

640?wx_fmt=jpeg

BDTC大会官网:https://t.csdnimg.cn/q4TY


作者 | chestnut--
来源 | CSDN博客


在医疗影像中特别是CT影像,包含大量的背景,在进行器官分割时,首先去除背景对分割的效果有很好的提升。本博客使用Python处理医疗影像并去除背景的影像。


使用的样例数据来自Multimodal Brain Tumor Segmentation Challenge 2018(https://www.med.upenn.edu/sbia/brats2018/registration.html)


读取数据的基本信息


import nibabel as nib
import numpy as np
from nilearn.image.image import _crop_img_to as crop_img_to
file = 'flair.nii.gz'
image = nib.load(file)
print(image.shape)


(240, 240, 155)


print(np.max(image.get_data()),np.min(image.get_data()))


470, 0


注意:0为背景像素值


显示其中的一个slice


import matplotlib.pyplot as plt
data = image.get_data()[:,:,52]
plt.imshow(data)


640?wx_fmt=jpeg

得到裁剪的空间位置坐标


def get_slice_index(data, rtol=1e-8):

    infinity_norm = max(-data.min(), data.max())
    passes_threshold = np.logical_or(data < -rtol * infinity_norm,
                                     data > rtol * infinity_norm)  ##
    if data.ndim == 4:
        passes_threshold = np.any(passes_threshold, axis=-1)

    coords = np.array(np.where(passes_threshold))
    start = coords.min(axis=1)
    end = coords.max(axis=1) + 1

    # pad with one voxel to avoid resampling problems
    start = np.maximum(start - 10)
    end = np.minimum(end + 1, data.shape[:3])

    slices = [slice(s, e) for s, e in zip(start, end)]
    return slices


使用True 和False标记背景区域,背景像素值为False。


def have_back(image):
    background_value=0
    tolerance=0.00001
    is_foreground = np.logical_or(image.get_data() < (background_value - tolerance),
                                  image.get_data()> (background_value + tolerance))
    foreground = np.zeros(is_foreground.shape, dtype=np.uint8)
    foreground[is_foreground] = 1
    return foreground


调用,得到背景图标记,通过背景图标记得到坐标位置


foreground = have_back(image)
crop = get_slice_index(foreground)
print(crop)


[slice(45, 192, None), slice(47, 210, None), slice(0, 137, None)]

分别代表X,Y,Z的裁剪坐标


裁剪


image_o = crop_img_to(image, crop, copy=True)


image_o shape (147, 163, 137)


显示裁剪之后的结果


import matplotlib.pyplot as plt
data_o = image_o.get_data()[:,:,52]
plt.imshow(data_o)


对比着看一下:


640?wx_fmt=jpeg

640?wx_fmt=jpeg


以上参考的是[1]中的代码。


[2]中相同的处理方式,同时能够处理2D和3D。


整体的代码如下,主要使用到了get_none_zero_region和crop_ND_volume_with_bounding_box这两个函数:


def get_none_zero_region(im, margin):
    """
    get the bounding box of the non-zero region of an ND volume
    """

    input_shape = im.shape
    if(type(margin) is int ):
        margin = [margin]*len(input_shape)
    assert(len(input_shape) == len(margin))
    indxes = np.nonzero(im)
    idx_min = []
    idx_max = []
    for i in range(len(input_shape)):
        idx_min.append(indxes[i].min())
        idx_max.append(indxes[i].max())

    for i in range(len(input_shape)):
        idx_min[i] = max(idx_min[i] - margin[i], 0)
        idx_max[i] = min(idx_max[i] + margin[i], input_shape[i] - 1)
    return idx_min, idx_max

def crop_ND_volume_with_bounding_box(volume, min_idx, max_idx):
    """
    crop/extract a subregion form an nd image.
    """

    dim = len(volume.shape)
    assert(dim >= 2 and dim <= 5)
    if(dim == 2):
        output = volume[np.ix_(range(min_idx[0], max_idx[0] + 1),
                               range(min_idx[1], max_idx[1] + 1))]
    elif(dim == 3):
        output = volume[np.ix_(range(min_idx[0], max_idx[0] + 1),
                               range(min_idx[1], max_idx[1] + 1),
                               range(min_idx[2], max_idx[2] + 1))]
    elif(dim == 4):
        output = volume[np.ix_(range(min_idx[0], max_idx[0] + 1),
                               range(min_idx[1], max_idx[1] + 1),
                               range(min_idx[2], max_idx[2] + 1),
                               range(min_idx[3], max_idx[3] + 1))]
    elif(dim == 5):
        output = volume[np.ix_(range(min_idx[0], max_idx[0] + 1),
                               range(min_idx[1], max_idx[1] + 1),
                               range(min_idx[2], max_idx[2] + 1),
                               range(min_idx[3], max_idx[3] + 1),
                               range(min_idx[4], max_idx[4] + 1))]
    else:
        raise ValueError("the dimension number shoud be 2 to 5")
    return output


使用:


margin = 5 
bbmin, bbmax = get_none_zero_region(arr, margin)
bbmin, bbmax


([34, 17], [171, 191])


arr为2D或者3D的矩阵。


volume = crop_ND_volume_with_bounding_box(arr, bbmin, bbmax)


640?wx_fmt=png

参考

3DUnetCNN

https://github.com/ellisdg/3DUnetCNN

3DUnet-Tensorflow-Brats18

https://github.com/tkuanlun350/3DUnet-Tensorflow-Brats18/blob/master/utils.py


扫码查看原文
▼▼▼

640?wx_fmt=jpeg


(*本文为AI科技大本营转载文章,转载联系原作者)



精彩推荐



2019 中国大数据技术大会(BDTC)再度来袭!豪华主席阵容及百位技术专家齐聚,15 场精选专题技术和行业论坛,超强干货+技术剖析+行业实践立体解读,深入解析热门技术在行业中的实践落地。6.6 折票限时特惠(立减1400元),学生票仅 599 元!


640?wx_fmt=jpeg
推荐阅读

http://lihuaxi.xjx100.cn/news/276692.html

相关文章

win7怎么配置程序服务器错误日志文件,win7怎么配置程序服务器

win7怎么配置程序服务器 内容精选换一换园区智能体服务的边缘算法作业会下发到边缘节点服务器运行&#xff0c;需要在IEF侧注册并纳管边缘节点。园区智能体的算法作业是以容器应用的方式下发到边缘节点运行的&#xff0c;因此需要在边缘节点服务器上安装Docker并检查Docker状态…

目录忽略_宣传册设计中目录的构思方法和运用

宣传册设计中有一项最容易被忽略&#xff0c;却也极为重要的内容就是目录。目录的设计导向着整个宣传册内容的坐标和布局。就好比阅读受众进入了购物中心&#xff0c;如何能快速准确的找到自己所心仪的内容并达成目的就要通过良好的指引。合理并优秀的宣传册目录设计既承担着导…

逼自己玩命学了3个多月,吃透了Python技术核心!分享给你,让你今年进个大厂!...

魔幻的2020年&#xff0c;疫情下就业大受影响&#xff0c;很多岗位缩招&#xff0c;而数据分析相关工作岗位&#xff08;如数据分析师、数据挖掘师等岗位&#xff09;却在增加。非专业数据分析岗位&#xff08;如运营、市场、销售等岗位&#xff09;也要求“数据分析”能力。大…

case when then else多个条件_SQL巡礼之CASE用法

使用CASE表达式使SQL语句的条件判断形式变得十分丰富&#xff0c;也因为CASE表达式不依赖于具体的数据库技术&#xff0c;所以它的可移植性也会更高。现在就让我们一起来领略一下CASE语句的用法吧。CASE表达式语法我们先创建一个Table用来举例&#xff08;本文全部代码在MySQL …

Redis是如何实现点赞、取消点赞的?

点击上方“方志朋”&#xff0c;选择“设为星标”回复”666“获取新整理的面试资料作者&#xff1a;solocoderjuejin.im/post/5bdc257e6fb9a049ba410098本文基于 SpringCloud, 用户发起点赞、取消点赞后先存入 Redis 中&#xff0c;再每隔两小时从 Redis 读取点赞数据写入数据库…

HttpClient连接池设置引发的一次雪崩

点击上方“方志朋”&#xff0c;选择“设为星标”回复”666“获取新整理的面试资料来源&#xff1a;http://i7q.cn/50G6cx-1-事件背景我在凤巢团队独立搭建和运维的一个高流量的推广实况系统&#xff0c;是通过HttpClient 调用大搜的实况服务。最近经常出现Address already in …

如何用 OpenCV、Python 和深度学习实现面部识别?

点击上方“小白学视觉”&#xff0c;选择加"星标"或“置顶”重磅干货&#xff0c;第一时间送达本文转自|新机器视觉Face ID 的兴起带动了一波面部识别技术热潮。本文将介绍如何使用 OpenCV、Python 和深度学习在图像和视频中实现面部识别&#xff0c;以基于深度识别的…

plsql执行command命令控制台出现乱码_设计模式系列 — 命令模式

点赞再看&#xff0c;养成习惯&#xff0c;公众号搜一搜【一角钱技术】关注更多原创技术文章。本文 GitHub org_hejianhui/JavaStudy 已收录&#xff0c;有我的系列文章。前言23种设计模式速记单例&#xff08;singleton&#xff09;模式工厂方法&#xff08;factory method&am…