nrrd文件read,另存为mat, matlab

这段代码主要实现批量处理E盘指定文件夹内的NRRD格式图像和对应的分割标签文件,进行数据维度转换和裁剪,然后将结果保存到另一个文件夹。同时,它利用NRRDFormatFileReader插件读取NRRD文件,并创建了三个不同的标签矩阵。整个过程涉及图像处理和数据存储。
摘要由CSDN通过智能技术生成
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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值