in:(luna16)

*.mhd

*.raw

标注文件:annotations.csv

out:

大小为512*512,只包含一张scan的图片和掩膜

from __future__ import print_function, division
import SimpleITK as sitk
import numpy as np
import csv
from glob import glob
import os
import pandas as pd
try:from tqdm import tqdm # long waits are not fun
except:print('TQDM does make much nicer wait bars...')tqdm = lambda x: x#Some helper functionsdef make_mask(center,diam,z,width,height,spacing,origin):'''
Center : centers of circles px -- list of coordinates x,y,z
diam : diameters of circles px -- diameter
widthXheight : pixel dim of image
spacing = mm/px conversion rate np array x,y,z
origin = x,y,z mm np.array
z = z position of slice in world coordinates mm'''mask = np.zeros([height,width]) # 0's everywhere except nodule swapping x,y to match img#convert to nodule space from world coordinates# Defining the voxel range in which the nodule fallsv_center = (center-origin)/spacingv_diam = int(diam/spacing[0]+5)v_xmin = np.max([0,int(v_center[0]-v_diam)-5])v_xmax = np.min([width-1,int(v_center[0]+v_diam)+5])v_ymin = np.max([0,int(v_center[1]-v_diam)-5])v_ymax = np.min([height-1,int(v_center[1]+v_diam)+5])v_xrange = range(v_xmin,v_xmax+1)v_yrange = range(v_ymin,v_ymax+1)# Convert back to world coordinates for distance calculationx_data = [x*spacing[0]+origin[0] for x in range(width)]y_data = [x*spacing[1]+origin[1] for x in range(height)]# Fill in 1 within sphere around nodulefor v_x in v_xrange:for v_y in v_yrange:p_x = spacing[0]*v_x + origin[0]p_y = spacing[1]*v_y + origin[1]if np.linalg.norm(center-np.array([p_x,p_y,z]))<=diam:mask[int((p_y-origin[1])/spacing[1]),int((p_x-origin[0])/spacing[0])] = 1.0return(mask)def matrix2int16(matrix):'''
matrix must be a numpy array NXN
Returns uint16 version'''m_min= np.min(matrix)m_max= np.max(matrix)matrix = matrix-m_minreturn(np.array(np.rint( (matrix-m_min)/float(m_max-m_min) * 65535.0),dtype=np.uint16))############
#
# Getting list of image files
luna_path = "C:/Users/jiaop/Desktop/luna16_dataset/"
luna_subset_path = luna_path+"set1/"
output_path = luna_path+"output_set1/"
file_list=glob(luna_subset_path+"*.mhd")#####################
#
# Helper function to get rows in data frame associated
# with each file
def get_filename(file_list, case):for f in file_list:if case in f:return(f)
#
# The locations of the nodes
df_node = pd.read_csv(luna_path+"annotations.csv")
df_node["file"] = df_node["seriesuid"].map(lambda file_name: get_filename(file_list, file_name))
df_node = df_node.dropna()#####
#
# Looping over the image files
#
for fcount, img_file in enumerate(tqdm(file_list)):mini_df = df_node[df_node["file"]==img_file] #get all nodules associate with fileif mini_df.shape[0]>0: # some files may not have a nodule--skipping those# load the data onceitk_img = sitk.ReadImage(img_file)img_array = sitk.GetArrayFromImage(itk_img) # indexes are z,y,x (notice the ordering)num_z, height, width = img_array.shape        #heightXwidth constitute the transverse planeorigin = np.array(itk_img.GetOrigin())      # x,y,z  Origin in world coordinates (mm)spacing = np.array(itk_img.GetSpacing())    # spacing of voxels in world coor. (mm)# go through all nodes (why just the biggest?)for node_idx, cur_row in mini_df.iterrows():node_x = cur_row["coordX"]node_y = cur_row["coordY"]node_z = cur_row["coordZ"]diam = cur_row["diameter_mm"]# just keep 3 slices# imgs = np.ndarray([3,height,width],dtype=np.float32)# masks = np.ndarray([3,height,width],dtype=np.uint8)# # try 1 slice# imgs = np.ndarray([1, height, width], dtype=np.float32)# masks = np.ndarray([1,height,width],dtype=np.uint8)#原版imgs = np.ndarray([height, width], dtype=np.float32)masks = np.ndarray([height, width], dtype=np.uint8)  # 原版# masks = np.ndarray([1, height, width], dtype=np.float32)#修改center = np.array([node_x, node_y, node_z])   # nodule centerv_center = np.rint((center-origin)/spacing)  # nodule center in voxel space (still x,y,z ordering)# print(np.arange(int(v_center[2])-1,#                  int(v_center[2])+2).clip(0, num_z-1))# for i, i_z in enumerate(np.arange(int(v_center[2])-1,#                  int(v_center[2])+2).clip(0, num_z-1)): # clip prevents going out of bounds in Z#     print('i=')#     print(i)#     print('i_z=')#     print(i_z)#     mask = make_mask(center, diam, i_z*spacing[2]+origin[2],#                      width, height, spacing, origin)#     masks[i] = mask#     imgs[i] = img_array[i_z]for i, i_z in enumerate(np.arange(int(v_center[2])-1,int(v_center[2])+2).clip(0, num_z-1)): # clip prevents going out of bounds in Zprint('i=')print(i)print('i_z=')i_z=i_z+1print(i_z)mask = make_mask(center, diam, i_z*spacing[2]+origin[2],width, height, spacing, origin)# masks[i] = mask# imgs[i] = img_array[i_z]masks= maskimgs = img_array[i_z]breaknp.save(os.path.join(output_path,"images_%04d_%04d.npy" % (fcount, node_idx)),imgs)np.save(os.path.join(output_path,"masks_%04d_%04d.npy" % (fcount, node_idx)),masks)print(imgs.shape)

查看生成的npy文件:

import numpy as npfrom matplotlib import pyplot as pltworking_path = "C:/Users/jiaop/Desktop/luna16_dataset/output_set1/"imgs = np.load(working_path + 'masks_0008_0105.npy')print(imgs.shape)plt.imshow(imgs, cmap='gray')#灰度图展示plt.show()

luna16肺部CT数据集预处理(根据标注信息生成掩膜)相关推荐

  1. 【自己制作数据集】制作标注并生成mask

    在深度学习的时候,我们可能需要自己对图片进行标注,这篇博客记录自己对图片进行标注并且生成mask的过程 首先使用pip安装标注工具 labelme pip install labelme 安装完成后直 ...

  2. 奖池90万!阿里天池发起肺部CT多病种智能诊断大赛

    点击我爱计算机视觉标星,更快获取CVML新技术 全球数据智能大赛(2019)--"数字人体"赛场一: 肺部CT多病种智能诊断 大赛概况 全球数据智能大赛(2019)由广西壮族自治区 ...

  3. 基于ITK-SNAP实现肺部CT图像中肺叶的标注

    时间:2019/06/12-2019/06/15 内容:肺叶图像标注,完成了10个病例的标注 材料:原始的肺部CT图像,以及采用U-net分割的肺实质的mask. 工具:itk-snap, paint ...

  4. MNIST数据集提取图片和标注信息

    MNIST数据集 简介 MNIST数据集(http://yann.lecun.com/exdb/mnist/)是著名的手写数字分类数据集,主要由一下四部分组成: 训练集图片:train-images. ...

  5. matlab气管分割,一种基于区域生长法与水平集相融合的肺部CT图像的分割

    摘要: 为将肺实质区域从含有背景.噪声的胸腔区域里分割出来,首先,应用传统的区域生长法初步定位肺部边界轮廓:其次,去除肺部边界噪声,采用自适应曲率阈值法修复肺部边界:最后,应用水平集法中的DRLSE模 ...

  6. 【玩转华为云】手把手教你利用ModelArts实现数据集的图像标注

    本篇推文共计2000个字,阅读时间约3分钟. 华为云-华为公司倾力打造的云战略品牌,2011年成立,致力于为全球客户提供领先的公有云服务,包含弹性云服务器.云数据库.云安全等云计算服务,软件开发服务, ...

  7. 新冠肺炎CT识别COVID-CT(一):新冠肺炎CT识别方法与CT数据集

    前言   前几天浏览器突然给我推送了一个文章,是介绍加州大学圣地亚哥分校.Petuum 的研究者构建了一个开源的 COVID-CT 数据集的.我看了一下代码其开源的代码,比较适合我们这种新手学习,当做 ...

  8. 肺部ct重建_肺部CT血管分割及三维重建

    摘要: 现代社会人们的生活水平不断改善,由于膳食不合理以及锻炼的缺乏,我国血管类疾病发生率不断攀高,传统的逐张读片的诊断方式效率低下且依赖于医生个人的知识储备,本文以肺部CT序列为着重点,对其中的血管 ...

  9. Criteo数据集预处理

    Criteo数据集介绍和下载 数据集包含各个特征取值和点击率,共39个特征,其中13个数字型特征,26个类别特征.Criteo是CTR模型的benchmark数据集,曾被用于kaggle竞赛. (Ka ...

最新文章

  1. flutter开发小程序_为什么我认为Flutter是移动应用程序开发的未来
  2. php_标准类型-学习笔记
  3. echarts无数据时显示无数据_钣金无腻子数据还原
  4. 依赖注入Bean属性——手动装配Bean
  5. Spring@主要注释
  6. 打开chrome控制台的快捷键
  7. JavaScriptCore框架在iOS7中的对象交互和管理
  8. 冲刺一团队五亲亲精英队
  9. 软件构造 git 图形界面看Object Graph
  10. 蓝桥杯单片机电路图讲解-74HC138-74HC02和74HC573的联合使用讲解
  11. 解决Windows照片查看器加载慢和颜色问题
  12. 计算机共享找不到网络连接失败,局域网电脑无法访问共享文件网络共享失败如何解决...
  13. 苹果 WWDC21 发布会全汇总,iOS 15更个性化,全家桶协作更有生产力
  14. 软件工程结课论文 敏捷开发在软件工程中的应用 大学编程作业(TUST 天津科技大学 2022年)
  15. 《隐忍的老虎司马懿》
  16. 关于Handle的一些介绍
  17. AF monitor tuning <2>
  18. 对标Zoom和Twilio,百家云还要走多久?
  19. 感性负载产生负压的影响分析
  20. Excel测试多个链接的简易方式

热门文章

  1. Android官网 打不开 解决办法
  2. 我的创业日记(序)——人生在于一种体验
  3. C#发送会议(约会)邀请
  4. JS、html中单引号与双引号的区别
  5. 计算机科学与技术专业考博课程,清华计算机科学与技术系 攻读博士学位研究生培养课程...
  6. 2021金山前端实习生笔试
  7. 秒杀实现原理及实现方式(转)
  8. 网络基本功:路由、抓包工具
  9. macOS系统(Linux同)终端输入操作的一些快捷操作!
  10. 人工智能眼中的2017全球最美100张面孔(上)