path1 = 'E:\***\';
path2 = 'E:\***\';
imgname = '**1.img.nrrd';
segname1 = '**1.seg.nrrd';
segname2 = '**2.seg.nrrd';
segname3 = '**3.seg.nrrd';
files = dir(strcat(path1,'*.*'));
len=length(files);
for i=3:len
i
pathname=files(i).name;
oldpath = strcat(path1,pathname);
newpath = strcat(path2,pathname);
%X ----volume data
%meta----header information
imgfiles = dir(fullfile(oldpath,'*.nrrd'));
for j = 1:length(imgfiles)
if (~(strcmp(imgfiles(j).name,segname1) || strcmp(imgfiles(j).name,segname2) || strcmp(imgfiles(j).name,segname3)))
imgname=imgfiles(j).name;
break;
end
end
[images, META]=nrrdread(fullfile(oldpath,imgname));
[S1, META1]=nrrdread(fullfile(oldpath,segname1));
[S2, META2]=nrrdread(fullfile(oldpath,segname2));
[S3, META3]=nrrdread(fullfile(oldpath,segname3));
%y,和x是颠倒的
images=permute(images,[2,1,3]);
S1=permute(S1,[2,1,3]);
S2=permute(S2,[2,1,3]);
S3=permute(S3,[2,1,3]);
[h,w,v]=size(images);
[h1,w1,v1]=size(S1);
[h2,w2,v2]=size(S2);
[h3,w3,v3]=size(S3);
offset1=META1.segmentation_referenceimageextentoffset;
offset2=META2.segmentation_referenceimageextentoffset;
offset3=META3.segmentation_referenceimageextentoffset;
idx1=strfind(offset1,' ');
idx2=strfind(offset2,' ');
idx3=strfind(offset3,' ');
%y,和x是颠倒的
os1 = [str2num(offset1(idx1(1)+1:idx1(2)-1)), str2num(offset1(1:idx1(1)-1)), str2num(offset1(idx1(2)+1:length(offset1)))];
os2 = [str2num(offset2(idx2(1)+1:idx2(2)-1)), str2num(offset2(1:idx2(1)-1)), str2num(offset2(idx2(2)+1:length(offset2)))];
os3 = [str2num(offset3(idx3(1)+1:idx3(2)-1)), str2num(offset3(1:idx3(1)-1)), str2num(offset3(idx3(2)+1:length(offset3)))];
label_sjg = zeros(h,w,v,'uint8');
label_ymn = zeros(h,w,v,'uint8');
label_zjp = zeros(h,w,v,'uint8');
label_sjg(os1(1)+1:os1(1)+h1, os1(2)+1:os1(2)+w1, os1(3)+1:os1(3)+v1) = S1;
label_ymn(os2(1)+1:os2(1)+h2, os2(2)+1:os2(2)+w2, os2(3)+1:os2(3)+v2) = S2;
label_zjp(os3(1)+1:os3(1)+h3, os3(2)+1:os3(2)+w3, os3(3)+1:os3(3)+v3) = S3;
mkdir(newpath);
save((fullfile(newpath,'images')), 'images');
save((fullfile(newpath,'label_sjg')), 'label_sjg');
save((fullfile(newpath,'label_ymn')), 'label_ymn');
save((fullfile(newpath,'label_zjp')), 'label_zjp');
end
nrrdread 的matlab 代码 是matlab 插件 NRRD Format File Reader https://www.mathworks.com/matlabcentral/fileexchange/34653-nrrd-format-file-reader
也可在我的资源中下载https://download.csdn.net/download/ok8821428/14034229