欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CT的原始图像.dcm文件的读取

程序员文章站 2022-05-04 14:09:28
...

在做CT图像的深度学习研究时,需要用到大量的CT数据,我从TCIA上下载的CT图像是.dcm文件的,每个.dcm文件代表一个slice,如图所示,下载的是Head-neck数据,

CT的原始图像.dcm文件的读取

用matlab读取时,使用到dicomread()函数,如下

filepath = '000000.dcm'
info = dicominfo(fullfile(filepath));   
image = dicomread(info);

这里只读取了一个slice,我们该如何批量读取.dcm文件呢?又如何把每层的图像数据进行排序?以下是参考代码:

主要有几点需要注意:

  1. 读取info中的层数信息,用于排序
  2. 把多余的空白数据删除


clear,clc

%% reading files path
folder = './4/';         %%% 所有slice的文件夹

ext = {'*.dcm'};         
filepaths = [];

for i = 1 : length(ext)
    filepaths = cat(1,filepaths,dir(fullfile(folder, ext{i})));   %%% 读取所有.dcm的文件
end

a = length(filepaths);
data = zeros(512,512,a);     %%% 图片大小
Num = zeros(1,a);

for i = 1:a 
    info = dicominfo(fullfile(folder,filepaths(i).name));      %%% 读取CT图像的扫描信息
    No = info.SliceLocation;                                   %%% 读取该层的序号
    Num(i) = floor(No);
end

value = min(Num(:));

%% extrating number of slices
for i = 1:a 
    info = dicominfo(fullfile(folder,filepaths(i).name));
    No = info.SliceLocation;
    Num0 = floor(No)+ abs(value)+1;                            %%% 对slicelocation进行从小到大的排序
    image = dicomread(info);
    data(:,:,Num0) = image;     % saving data, including blanks  
end

%% delete the blank
n_dim = size(data,3);
t = 1;
for j = 1: n_dim
    img = data(:,:,j);
    if mean(img)==0
        rem(t) = j;
        t  = t + 1;
    else   
    end
end

data(:,:,rem) = [];     %% 删掉空白的层
n = size(data,3);

temp = data;

%% show and save the result
for i = 1:n
    data(:,:,n-i+1) = temp(:,:,i);
end
for i = 1:n
    imshow(data(:,:,i),[])
end

save pat4.mat data    %%% 保存数据

 

相关标签: 医学图像处理