我在数据库里找到的是.nii.gz一群类似的文字信息,我应该怎样下载导入?还是直接粘贴到什么地方呢,有没有懂得帮我解答一下,我太愚了
1条回答 默认 最新
关注 nii.gz文件是用0-33的数字来标记不同部位。推荐先按普通文件下载,再对nii.gz格式文件进行解析
nii.gz格式文件解析的例程:
import SimpleITK as sitk import numpy as np import os from PIL import Image def read_nii(file_path): ds = sitk.ReadImage(file_path) # 读取nii数据的第一个函数sitk.ReadImage # print('ds: ', ds) data = sitk.GetArrayFromImage(ds) # 把itk.image转为array # print('data: ', data) print('shape_of_data', data.shape) spacing = ds.GetSpacing() # 三维数据的间隔 # print('spacing_of_data', spacing) return data # 从十六进制的颜色得到RGB颜色 def color(value): digit = list(map(str, range(10))) + list("ABCDEF") if isinstance(value, tuple): string = '#' for vi in value: a1 = vi // 16 a2 = vi % 16 string += digit[a1] + digit[a2] return string elif isinstance(value, str): a1 = digit.index(value[1]) * 16 + digit.index(value[2]) a2 = digit.index(value[3]) * 16 + digit.index(value[4]) a3 = digit.index(value[5]) * 16 + digit.index(value[6]) return [a1, a2, a3] def getRGBColor(colorArray): colorMapRGB = [] for i in range(len(colorArray)): colorMapRGB.append(color(colorArray[i])) return colorMapRGB # niiDataArray(读取nii文件获得的三维数组) # type(需要得到的图片的类型有1:横断面上下切、2矢状面左右切、3冠状面前后切) # imgId(获得的图片在该面的位置) def getImgFromNiiDataArray(niiDataArray, type, imgId, colorMap): shape = niiDataArray.shape if type == 1: imgData = np.array(niiDataArray[imgId, :, :]) elif type == 2: imgData = np.array(niiDataArray[:, imgId, :]) elif type == 3: imgData = np.array(niiDataArray[:, :, imgId]) imgR = np.zeros(imgData.shape) imgG = np.zeros(imgData.shape) imgB = np.zeros(imgData.shape) imgA = np.zeros(imgData.shape) for i in range(1, 33): imgR[imgData == i] = colorMap[i - 1][0] imgG[imgData == i] = colorMap[i - 1][1] imgB[imgData == i] = colorMap[i - 1][2] imgA[imgData == i] = 255 r = Image.fromarray(imgR).convert('L') g = Image.fromarray(imgG).convert('L') b = Image.fromarray(imgB).convert('L') a = Image.fromarray(imgA).convert('L') image = Image.merge('RGBA', (r, g, b, a)) return image # 以png格式输出nii文件中三种视图的所有图片 # 会在target目录下生成三个文件夹(横断面,矢状面,冠状面) # 里面装了对应得一系列png图片 def exportAllImgByPNG(niiDataArray, targetPath): if os.path.exists(os.path.join(targetPath, '横断面')) == False: os.mkdir(os.path.join(targetPath, '横断面')) if os.path.exists(os.path.join(targetPath, '矢状面')) == False: os.mkdir(os.path.join(targetPath, '矢状面')) if os.path.exists(os.path.join(targetPath, '冠状面')) == False: os.mkdir(os.path.join(targetPath, '冠状面')) dataShape = niiDataArray.shape # for i in range(dataShape[0]): imge = getImgFromNiiDataArray(niiDataArray, 1, i, rgbColorMap) imge.save(os.path.join(targetPath, '横断面', '_' + str(i) + '.png')) for i in range(dataShape[1]): imge = getImgFromNiiDataArray(niiDataArray, 2, i, rgbColorMap) imge.save(os.path.join(targetPath, '矢状面', '_' + str(i) + '.png')) for i in range(dataShape[2]): imge = getImgFromNiiDataArray(niiDataArray, 3, i, rgbColorMap) imge.save(os.path.join(targetPath, '冠状面', '_' + str(i) + '.png')) # 33个颜色,分别对应牙槽骨和32颗牙齿的颜色,0为牙槽骨的颜色,1~32为牙齿的颜色 colorMap = ['#00AA00', '#F93408', '#F57A34', '#F7951E', '#F6C238', '#FBE92F', '#E5F827', '#B6F313', '#97F922', '#75F72A', '#35F80A', '#23FD23', '#1DF645', '#37F57C', '#03FE8C', '#29FEC4', '#36FBE9', '#2CEBFE', '#29C1FA', '#048BFB', '#1B6CFA', '#153FFE', '#2F2FFD', '#3A10F4', '#610DF4', '#8F0AFD', '#B714F4', '#DE11F3', '#FE30EB', '#FD19BF', '#FB279B', '#FD1C6E', '#FD0532' ] if __name__ =='__main__': rgbColorMap = getRGBColor(colorMap) # 得到各部位颜色 # print(rgbColorMap) niifile_path = "./lvsili_label_tooth.nii.gz" # file_path = "./lvsiling_label_alveolar.nii.gz" niiDataArray = read_nii(niifile_path) exportAllImgByPNG(niiDataArray, './teethImg') # imge = getImgFromNiiDataArray(niiDataArray, 1, 100, rgbColorMap) # imge.show() # imge.save('./dog.png')
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 PADS Logic 图标
- ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
- ¥15 DruidDataSource一直closing
- ¥20 气象站点数据求取中~
- ¥15 如何获取APP内弹出的网址链接
- ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
- ¥50 STM32单片机传感器读取错误
- ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
- ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
- ¥50 sft下载大文阻塞卡死