% Function:
%
% 《数字图像技术处理及应用》课程大作业预处理程序及特征提取程序
% Writer: Wu Chengpeng
% Time: 2017/12/12
clear;clc;close all
%% ====================Set the Input and Output Data Path============
% 该路径为测试数据的输入路径,下面包括HGG和LGG两个子文件夹
test_path = 'D:\Data\MICCAI_BraTS17_Data_Training\';
% 设置输出路径
feature_name = {'Intensity','Texture','Gradient','GradientMean','GradientVariance','SingleRay','DoubleRay','Symmetry'}; % 特征名称,建议每种特征单独处理,以便后期选择使用哪些特征
% feature_length = 3; % 特征长度,也就是每个点的该特征需要用几个数存储,如方差用3个(3^3,5^3,7^3)就行
output_path = strcat(test_path,'Features\',feature_name,'\');
HGG_path = strcat(test_path,'HGG\');
LGG_path = strcat(test_path,'LGG\');
% 列出HGG子文件夹下的所有子文件夹,也就是每个病人一个文件夹
HGG_dir = fullfile(HGG_path);
HGG_dir_out = dir(fullfile(HGG_dir,'*'));
HGG_dir_name = {HGG_dir_out(3:end).name}';
% HGG_name是后面需要用的一个路径前缀
HGG_name = HGG_dir_name;
for ii = 1:length(HGG_dir_name)
HGG_name{ii} = strcat(HGG_path,HGG_dir_name{ii},'\',HGG_dir_name{ii});
end
% 列出LGG子文件夹下的所有子文件夹,也就是每个病人一个文件夹
LGG_dir = fullfile(LGG_path);
LGG_dir_out = dir(fullfile(LGG_dir,'*'));
LGG_dir_name = {LGG_dir_out(3:end).name}';
% LGG_name是后面需要用的一个路径前缀
LGG_name = LGG_dir_name;
for ii = 1:length(LGG_dir_name)
LGG_name{ii} = strcat(LGG_path,LGG_dir_name{ii},'\',LGG_dir_name{ii});
end
% 将HGG和LGG的两部分病人合并,也就是全部的病人文件夹路径
total_dir_name = {HGG_name{:},LGG_name{:}};
total_output_dir = cell(1,length(feature_name));
for ii = 1:length(feature_name)
if ~exist(output_path{ii},'dir')
mkdir(output_path{ii});
end
if ~exist(strcat(output_path{ii},'HGG'),'dir')
mkdir(strcat(output_path{ii},'HGG'));
end
if ~exist(strcat(output_path{ii},'LGG'),'dir')
mkdir(strcat(output_path{ii},'LGG'));
end
output_HGG_path = HGG_dir_name;
for jj = 1:length(HGG_dir_name)
output_HGG_path{jj} = strcat(output_path{ii},'HGG\',HGG_dir_name{jj},'\');
if ~exist(output_HGG_path{jj},'dir')
mkdir(output_HGG_path{jj});
end
end
output_LGG_path = LGG_dir_name;
for jj = 1:length(LGG_dir_name)
output_LGG_path{jj} = strcat(output_path{ii},'LGG\',LGG_dir_name{jj},'\');
if ~exist(output_LGG_path{jj},'dir')
mkdir(output_LGG_path{jj});
end
end
% 将HGG和LGG的两部分病人合并,也就是全部的病人输出文件夹路径
total_output_dir{ii} = {output_HGG_path{:},output_LGG_path{:}};
end
%% ====================Loading Data==================================
sigma = [2.5,3.0,4.5];
% sigma = 3.0;
% 通过parfor对每个病例进行循环,并行处理加快速度
parfor kk = 61:70
tic
nii_path = total_dir_name{kk};
t1_struct = load_nii(strcat(nii_path,'_t1.nii.gz'));
t2_struct = load_nii(strcat(nii_path,'_t2.nii.gz'));
t1ce_struct = load_nii(strcat(nii_path,'_t1ce.nii.gz'));
flair_struct = load_nii(strcat(nii_path,'_flair.nii.gz'));
[img_w,img_h,img_d] = size(t1_struct.img); % 三维数据大小
toc
disp(strcat('Finish Loading Data!',nii_path));
tic
%% Feature 1: Intensity
intensity_feature = IntensityFeature(t1_struct,t2_struct,t1ce_struct,flair_struct);
output_matname = strcat(total_output_dir{1}{kk},'intensity.mat');
name_t1 = 'intensity_feature';
parsave(output_matname,name_t1,intensity_feature);
toc
disp(strcat('Finish Extract Intensity Features!',nii_path));
%% ====================Part 1: Smooth and Normalized=================
% 高斯平滑
tic
% 下面XX_smooth是一个n*1的cell,n是sigma的个数,此处n=3
t1_smooth = SmoothGauss(t1_struct,sigma);
% 为了避免高斯平滑有问题,同时保留一个原始数据,此处XX_smooth变成(n+1)*1的cell
% t1_smooth = {t1_struct,t1_smooth{:}};
t2_smooth = SmoothGauss(t2_struct,sigma);
% t2_smooth = {t2_struct,t2_smooth{:}};
t1ce_smooth = SmoothGauss(t1ce_struct,sigma);
% t1ce_smooth = {t1ce_struct,t1ce_smooth{:}};
flair_smooth = SmoothGauss(flair_struct,sigma);
% flair_smooth = {flair_struct,flair_smooth{:}};
toc
disp(strcat('Finish Smoothing Data!',nii_path));
% 归一化
tic
t1_smooth = ThreeDNorm(t1_smooth);
% 为了避免三维归一化有问题,同时保留一个原始数据,此处XX_smooth变成(n+2)*1的cell
% t1_smooth = {t1_struct,t1_smooth{:}};
t2_smooth = ThreeDNorm(t2_smooth);
% t2_smooth = {t2_struct,t2_smooth{:}};
t1ce_smooth = ThreeDNorm(t1ce_smooth);
% t1ce_smooth = {t1ce_struct,t1ce_smooth{:}};
flair_smooth = ThreeDNorm(flair_smooth);
% flair_smooth = {flair_struct,flair_smooth{:}};
toc
disp(strcat('Finish Normalizing Data!',nii_path));
%% ====================Part 2: Extract Features======================
for nn = 1:length(t1_smooth)
t1_nii_struct = t1_smooth{nn};
t2_nii_struct = t2_smooth{nn};
t1ce_nii_struct = t1ce_smooth{nn};
flair_nii_struct = flair_smooth{nn};
t1_smooth{nn} = [];
t2_smooth{nn} = [];
t1ce_smooth{nn} = [];
flair_smooth{nn} = [];
if nn==2
%% T1 NII Struct
tic
% Feature 2: Texture
t1_texture = Extract3DTexture(t1_nii_struct);
output_matname_part1 = strcat(total_output_dir{2}{kk},'texture_','sigma',num2str(sigma(nn)),'_t1_3x3.mat');
output_matname_part2 = strcat(total_output_dir{2}{kk},'texture_','sigma',num2str(sigma(nn)),'_t1_5x5.mat');
output_matname_part3 = strcat(total_output_dir{2}{kk},'texture_','sigma',num2str(sigma(nn)),'_t1_7x7.mat');
name_t1_part1 = 't1_texture_3x3';
name_t1_part2 = 't1_texture_5x3';
name_t1_part3 = 't1_texture_7x7';
t1_texture_3x3 = squeeze(t1_texture(1,:,:,:,:));
parsave(output_matname_part1,name_t1_part1,t1_texture_3x3);
t1_texture_5x5 = squeeze(t1_texture(2,:,:,:,:));
parsave(output_matname_part2,name_t1_part2,t1_texture_5x5);
t1_texture_7x7 = squeeze(t1_texture(3,:,:,:,:));
parsave(output_matname_part3,name_t1_part3,t1_texture_7x7);
t1_texture = [];
t1_texture_3x3 = [];
t1_texture_5x5 = [];
t1_texture_7x7 = [];
toc
disp(strcat('Finish Extract T1 Texture Feature!',nii_path));
% Feature 3: Gradient
tic
img_tep=double(t1_nii_struct.img);
[px,py,pz]=gradient(img_tep);
gra=sqrt((px.*px)+(py.*py)+(pz.*pz));
t1_gradient=gra;%gradiant
output_matname = strcat(total_output_dir{3}{kk},'gradient_','sigma',num2str(sigma(nn)),'_t1.mat');
name_t1 = 't1_gradient';
parsave(output_matname,name_t1,t1_gradient);
t1_gradient = [];
toc
disp(strcat('Finish Extract T1 Gradient Feature!',nii_path));
% Feature 4: Gradient Mean
tic
t1_gradientmean_3x3 = grad_mean( gra,3,3,3 ); %grad_mean3
t1_gradientmean_5x5 = grad_mean( gra,5,5,5 ); %grad_mean5
t1_gradientmean_7x7 = grad_mean( gra,7,7,7 ); %grad_mean7
output_matname_part1 = strcat(total_output_dir{4}{kk},'gradientmean_','sigma',num2str(sigma(nn)),'_t1_3x3.mat');
output_matname_part2 = strcat(total_output_dir{4}{kk},'gradientmean_','sigma',num2str(sigma(nn)),'_t1_5x5.mat');
output_matname_part3 = strcat(total_output_dir{4}{kk},'gradientmean_','sigma',num2str(sigma(nn)),'_t1_7x7.mat');
name_t1_part1 = 't1_gradientmean_3x3';
name_t1_part2 = 't1_gradientmean_5x5';
name_t1_part3 = 't1_gradientmean_7x7';
评论2