Clear Filters
Clear Filters

How can I locate dicom images?

2 views (last 30 days)
Tomislav
Tomislav on 13 Nov 2012
So now I have:
[filename, pathname] = uigetfile();
set(handles.edit5, 'String', pathname);
img = zeros(165,127,168,'uint8');
for ii = 1:168;
img(:,:,ii) = dicomread([pathname '\' num2str(ii,'%04i') '.dcm']);
end;
And it only works if there are exactly 168 images (165x127 pixels) in the folder and if they are numbered as: 0001, 0002, 0003, and so on. And it stores it as a 3D matrix name img.
How can I make code that it works with any number of dicom images of any size and any name and that it stores them as now, in 3D matrix called img.
Thanks.

Accepted Answer

Jan
Jan on 13 Nov 2012
Edited: Jan on 13 Nov 2012
[filename, pathname] = uigetfile();
if ~ischar(filename)
return;
end
set(handles.edit5, 'String', pathname);
fileList = dir(fullfile(pathname, '*.dcm'));
fileList = fileList(not([fileList.isdir]));
nFile = numel(fileList);
imgC = cell(1, nFile);
for ii = 1:nFile;
imgC{ii} = dicomread(fullfile(pathname, fileList(ii).name));
end
Of course you can store pictures in a 3D-array only, if they all have the same size. If this is true:
imgSize = size(imgC{1});
if all(cellfun('size', imgC, 1) == imgSize(1)) && ...
all(cellfun('size', imgC, 2) == imgSize(2))
img = cat(3, imgC{:});
else
error('Pictures have different sizes.');
end

More Answers (0)

Categories

Find more on DICOM Format in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!