记录学习
将ROI沿着边界扩展为矩形,并将病灶层面和有效ROI保存为nii格式
%文件说明:读取病人原始图像和mask,或者是沿着切线画矩形
clc;clear;
root_dir=‘D:\test\DWI‘;
fileList=dir(root_dir);
n=length(fileList);
cntpic=0;
files={};
%遍历文件夹
for i=1:n
if strcmp(fileList(i).name,‘.‘)==1||strcmp(fileList(i).name,‘..‘)==1
continue;
else
fileList(i).name;
full_name=[root_dir,‘\‘,fileList(i).name];
cntpic=cntpic+1;
files(cntpic)={full_name};
end
end
filename={};
c=0;
for i=1:length(files)
Modalityinfo=dir(fullfile(files{i},‘*.nii.gz‘));
c=c+1;
filename(c,:)={Modalityinfo.folder,Modalityinfo.name};
end
[num s]=size(filename);
for i=1:num
path=filename{i,1};
path_T2=char(strcat(path,‘/‘,filename{i,9}));
path_label=char(strcat(path,‘/‘,filename{i,10}));
T2=load_untouch_nii(path_T2);
label=load_untouch_nii(path_label);
img=double(T2.img);
label=double(label.img);
z=label(:,:,1);
d=label(:,:,1);%相当于初始化
img_sort=label(:,:,1);
img_out=label(:,:,1);
mask_out=label(:,:,1);
%判断mask是否为零,不为零输出i,保存图片相应层面
[x y m]=size(label)
j=1;
%l=1;
for k = 1:m % 开始切片数据轮寻
z(:,:,k)=label(:,:,k);
temp=sum(sum(z(:,:,k)));
if temp~=0
d(:,:,j)=label(:,:,k);%保存d,d为含有ROI的图片
img_sort(:,:,j)=img(:,:,k);
%mask_sort(:,:,j)=label
A=d(:,:,j);
[r,c]=find(A==1)
min(r)
max(r)
min(c)
max(c)
for i1=min(r):max(r)%计算ROI的边缘坐标点
for j1=min(c):max(c)
d(i1,j1,j)=1;
%img_out(:,:,j)=imrotate(img_sort(:,:,j).*d(:,:,j),180);
% img_out(:,:,j)=img_sort(:,:,j);
% mask_out(:,:,j)=d(:,:,j);
img_out(:,:,j)=img_sort(:,:,j);
mask_out(:,:,j)=d(:,:,j);
end
end
j=j+1;
%l=l+1;
end
end
str=filename{i,3};
A=isstrprop(str,‘digit‘)
B=str(A)
%写入nii文件
path1=char(strcat(‘D:\test\DWI‘,‘\‘,B));
path_mask=char(strcat(path1,‘\‘,B,‘_ADC_recROI.nii.gz‘));
path_T2=char(strcat(path1,‘\‘,B,‘_ADC_disease.nii.gz‘));
T2_nii=make_nii(img_out);
mask_nii=make_nii(mask_out);
save_nii(T2_nii,path_T2);
save_nii(mask_nii,path_mask);
end