How do I compare 2 nrrd images out of a CT?
조회 수: 3 (최근 30일)
이전 댓글 표시
So i have 2 .nrrd files, both of a brain CT, one that I previously segmented with a script i made to make the segmentation more automatic, and the other one that I segmented manually in STKSnap, and I need to compare those 2 files and get all the areas in the CT that make an intersection with each other. I have a function to read .nrrd figures, but i haven't still figured it out how to use it.
Here's the .nrrdread function i have:
% Current limitations/caveats:
% * "Block" datatype is not supported.
% * Only tested with "gzip" and "raw" file encodings.
% * Very limited testing on actual files.
% * I only spent a couple minutes reading the NRRD spec.
%
% See the format specification online:
% http://teem.sourceforge.net/nrrd/format.html
% Copyright 2012 The MathWorks, Inc.
% Open file.
fid = fopen(filename, 'rb');
assert(fid > 0, 'Could not open file.');
cleaner = onCleanup(@() fclose(fid));
% Magic line.
theLine = fgetl(fid);
assert(numel(theLine) >= 4, 'Bad signature in file.')
assert(isequal(theLine(1:4), 'NRRD'), 'Bad signature in file.')
% The general format of a NRRD file (with attached header) is:
%
% NRRD000X
% <field>: <desc>
% <field>: <desc>
% # <comment>
% ...
% <field>: <desc>
% <key>:=<value>
% <key>:=<value>
% <key>:=<value>
% # <comment>
%
% <data><data><data><data><data><data>...
meta = struct([]);
% Parse the file a line at a time.
while (true)
theLine = fgetl(fid);
if (isempty(theLine) || feof(fid))
% End of the header.
break;
end
if (isequal(theLine(1), '#'))
% Comment line.
continue;
end
% "fieldname:= value" or "fieldname: value" or "fieldname:value"
parsedLine = regexp(theLine, ':=?\s*', 'split','once');
assert(numel(parsedLine) == 2, 'Parsing error')
field = lower(parsedLine{1});
value = parsedLine{2};
field(isspace(field)) = '';
meta(1).(field) = value;
end
datatype = getDatatype(meta.type);
% Get the size of the data.
assert(isfield(meta, 'sizes') && ...
isfield(meta, 'dimension') && ...
isfield(meta, 'encoding') && ...
isfield(meta, 'endian'), ...
'Missing required metadata fields.')
dims = sscanf(meta.sizes, '%d');
ndims = sscanf(meta.dimension, '%d');
assert(numel(dims) == ndims);
data = readData(fid, meta, datatype);
data = adjustEndian(data, meta);
% Reshape and get into MATLAB's order.
X = reshape(data, dims');
X = permute(X, [2 1 3]);
function datatype = getDatatype(metaType)
% Determine the datatype
switch (metaType)
case {'signed char', 'int8', 'int8_t'}
datatype = 'int8';
case {'uchar', 'unsigned char', 'uint8', 'uint8_t'}
datatype = 'uint8';
case {'short', 'short int', 'signed short', 'signed short int', ...
'int16', 'int16_t'}
datatype = 'int16';
case {'ushort', 'unsigned short', 'unsigned short int', 'uint16', ...
'uint16_t'}
datatype = 'uint16';
case {'int', 'signed int', 'int32', 'int32_t'}
datatype = 'int32';
case {'uint', 'unsigned int', 'uint32', 'uint32_t'}
datatype = 'uint32';
case {'longlong', 'long long', 'long long int', 'signed long long', ...
'signed long long int', 'int64', 'int64_t'}
datatype = 'int64';
case {'ulonglong', 'unsigned long long', 'unsigned long long int', ...
'uint64', 'uint64_t'}
datatype = 'uint64';
case {'float'}
datatype = 'single';
case {'double'}
datatype = 'double';
otherwise
assert(false, 'Unknown datatype')
end
function data = readData(fidIn, meta, datatype)
switch (meta.encoding)
case {'raw'}
data = fread(fidIn, inf, [datatype '=>' datatype]);
case {'gzip', 'gz'}
tmpBase = tempname();
tmpFile = [tmpBase '.gz'];
fidTmp = fopen(tmpFile, 'wb');
assert(fidTmp > 3, 'Could not open temporary file for GZIP decompression')
tmp = fread(fidIn, inf, 'uint8=>uint8');
fwrite(fidTmp, tmp, 'uint8');
fclose(fidTmp);
gunzip(tmpFile)
fidTmp = fopen(tmpBase, 'rb');
cleaner = onCleanup(@() fclose(fidTmp));
meta.encoding = 'raw';
data = readData(fidTmp, meta, datatype);
case {'txt', 'text', 'ascii'}
data = fscanf(fidIn, '%f');
data = cast(data, datatype);
otherwise
assert(false, 'Unsupported encoding')
end
function data = adjustEndian(data, meta)
[~,~,endian] = computer();
needToSwap = (isequal(endian, 'B') && isequal(lower(meta.endian), 'little')) || ...
(isequal(endian, 'L') && isequal(lower(meta.endian), 'big'));
if (needToSwap)
data = swapbytes(data);
end
댓글 수: 3
Rik
2019년 11월 20일
It seems pretty basic to me: put in the filenames of the files you want to compare and compare the two different outputs in whatever way you require. What is your exact issue?
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 DICOM Format에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!