0
votes

I am working with lung data sets in matlab, but I need to sort the slices correctly and show them.

I knew that can be done using the "instance number" parameter in Dicom header, but I did not manage to run the correct code.

How can I do that?

Here is my piece of code:

Dicom_directory = uigetdir();
sdir = strcat(Dicom_directory,'\*.dcm');
files = dir(sdir);
I = strcat(Dicom_directory, '\',files(i).name);
x = repmat(double(0), [512 512 1 ]);
x(:,:,1) = double(dicomread(I));
axes(handles.axes1);
imshow(x,[]);
1

1 Answers

2
votes

First of all, to get the DICOM header, you need to use dicominfo which will return a struct containing each of the fields. If you want to use the InstanceNumber field to sort by, then you can do this in such a way.

%// Get all of the files
directory = uigetdir();
files = dir(fullfile(directory, '*.dcm'));
filenames = cellfun(@(x)fullfile(directory, x), {files.name}, 'uni', 0);

%// Ensure that they are actually DICOM files and remove the ones that aren't
notdicom = ~cellfun(@isdicom, filenames);
files(notdicom) = [];

%// Now load all the DICOM headers into an array of structs
infos = cellfun(@dicominfo, filenames);

%// Now sort these by the instance number
[~, inds] = sort([infos.InstanceNumber]);
infos = infos(inds);

%// Now you can loop through and display them
dcm = dicomread(infos(1));
him = imshow(dcm, []);

for k = 1:numel(infos)
    set(him, 'CData', dicomread(infos(k)));
    pause(0.1)
end

That being said, you have to be careful sorting DICOMs using the InstanceNumber. This is not a robust way of doing it because the "InstanceNumber" can refer to the same image acquired over time or different slices throughout a 3D volume. If you want one or the other, I would choose something more specific.

If you want to sort physical slices, I would recommend sorting by the SliceLocation field (if available). If sorting by time, you could use TriggerTime (if available).

Also you will need to consider that there could also potentially be multiple series in your folder so maybe consider using the SeriesNumber to differentiate these.