引文:前阵子林老师使用双能CT,导出文件Monaco导入报错,但在软件上可以正常读出,原因是方向[1,0,0,0,1,0]改动了,变成[0.999,0,0.01,0,0.99,0.01]这样。直接改回成[1,0,0,0,1,0]仍不能导入monaco,于是借尸还魂,使用替他头文件写入。
PS:先用ImageJ检测dcm顺序与图像顺序是否统一,不然需要做resort操作。我另一篇文章写过。
clc; clear all ; close all;
%%
for j = 1:5
path = ['G:\003\F', num2str(j)];
outpath = [path, '_output'];
mkdir(outpath);
dcmnames = dir(path);
dcminfo_base = dicominfo('-2.3.CT.DCM');
dcminfo_base.SliceThickness = 2.5;
% dcminfo_base2 = dicominfo('-7.3.CT.DCM');
sort_nat_name=sort_nat({dcmnames.name})';
for i = 3:length(dcmnames)
dcmname = sort_nat_name{i};
dcmpath = fullfile(path, dcmname);
dcmoutpath = fullfile(outpath, dcmname);
dcminfo = dicominfo(dcmpath);
dcmimg = dicomread(dcmpath);
dcminfo_base.ImagePositionPatient(3) = dcminfo_base.ImagePositionPatient(3)- 2.5;
dcminfo_base.SliceLocation = dcminfo_base.SliceLocation - 2.5;
dcminfo_base.PixelSpacing = dcminfo.PixelSpacing;
dcminfo_base.InstanceNumber = dcminfo_base.InstanceNumber + 1;
% disp(dcminfo.ImagePositionPatient(3))
% dcminfo.ImageOrientationPatient = [1,0,0,0,1,0]';
% dicomwrite(dcmimg, dcmoutpath, dcminfo_base);
end
end
这里使用了sort_nat_name.m函数,用来避免文件为1,10,11,2,21,3这种,排序成1,2,3,10,11,21
%sort_nat具体内容
function [cs,index] = sort_nat(c,mode)
%sort_nat: Natural order sort of cell array of strings.
% usage: [S,INDEX] = sort_nat(C)
%
% where,
% C is a cell array (vector) of strings to be sorted.
% S is C, sorted in natural order.
% INDEX is the sort order such that S = C(INDEX);
%
% Natural order sorting sorts strings containing digits in a way such that
% the numerical value of the digits is taken into account. It is
% especially useful for sorting file names containing index numbers with
% different numbers of digits. Often, people will use leading zeros to get
% the right sort order, but with this function you don't have to do that.
% For example, if C = {'file1.txt','file2.txt','file10.txt'}, a normal sort
% will give you
%
% {'file1.txt' 'file10.txt' 'file2.txt'}
%
% whereas, sort_nat will give you
%
% {'file1.txt' 'file2.txt' 'file10.txt'}
%
% See also: sort
% Version: 1.4, 22 January 2011
% Author: Douglas M. Schwarz
% Email: dmschwarz=ieee*org, dmschwarz=urgrad*rochester*edu
% Real_email = regexprep(Email,{'=','*'},{'@','.'})
% Set default value for mode if necessary.
if nargin < 2
mode = 'ascend';
end
% Make sure mode is either 'ascend' or 'descend'.
modes = strcmpi(mode,{'ascend','descend'});
is_descend = modes(2);
if ~any(modes)
error('sort_nat:sortDirection',...
'sorting direction must be ''ascend'' or ''descend''.')
end
% Replace runs of digits with '0'.
c2 = regexprep(c,'\d+','0');
% Compute char version of c2 and locations of zeros.
s1 = char(c2);
z = s1 == '0';
% Extract the runs of digits and their start and end indices.
[digruns,first,last] = regexp(c,'\d+','match','start','end');
% Create matrix of numerical values of runs of digits and a matrix of the
% number of digits in each run.
num_str = length(c);
max_len = size(s1,2);
num_val = NaN(num_str,max_len);
num_dig = NaN(num_str,max_len);
for i = 1:num_str
num_val(i,z(i,:)) = sscanf(sprintf('%s ',digruns{i}{:}),'%f');
num_dig(i,z(i,:)) = last{i} - first{i} + 1;
end
% Find columns that have at least one non-NaN. Make sure activecols is a
% 1-by-n vector even if n = 0.
activecols = reshape(find(~all(isnan(num_val))),1,[]);
n = length(activecols);
% Compute which columns in the composite matrix get the numbers.
numcols = activecols + (1:2:2*n);
% Compute which columns in the composite matrix get the number of digits.
ndigcols = numcols + 1;
% Compute which columns in the composite matrix get chars.
charcols = true(1,max_len + 2*n);
charcols(numcols) = false;
charcols(ndigcols) = false;
% Create and fill composite matrix, comp.
comp = zeros(num_str,max_len + 2*n);
comp(:,charcols) = double(s1);
comp(:,numcols) = num_val(:,activecols);
comp(:,ndigcols) = num_dig(:,activecols);
% Sort rows of composite matrix and use index to sort c in ascending or
% descending order, depending on mode.
[unused,index] = sortrows(comp);
if is_descend
index = index(end:-1:1);
end
index = reshape(index,size(c));
cs = c(index);