how to read and save dicom images in different directories

11 views (last 30 days)
I have series of 1600 dicom images with the name 6--12-1.dcm, 6--12-2.dcm, 6--12-3.dcm and so on. how can I read the sequence and do the filtering on images and save them in another folder? here is my code but it gives me errors, can you please help me to correct it:
source_dir=uigetdir([]); %I= dir('C:\Users\Shan\Desktop\CT Images')
pwd
d=dir([source_dir,'\6--12-*.dcm']);
totalfile=length(d);
resultfolder='C:\Users\Shan\Desktop\CT Images\Median'
for i=1:totalfile
fname=['6--12-',num2str(i), '.dcm'];
indicom=dicomread(fname);
subplot(1,2,1), imshow(indicom,[]);
title('original image number',(num2str(i)),'Interpreter','None')
medianfilter{i}=medfilt2(indicom{i},[4 4]);
BasedFileName=sprint('%d.dcm',i);
FilteredFileName=fullfile(resultfolder,BasedFileName)
dicomwrite(medianfilter, FilteredFileName);
subplot(1,2,2), imshow(medianfilter,[]);
title('median filtered image number',num2str(i),'Interpreter','None')
end

Answers (1)

Geoff Hayes
Geoff Hayes on 23 Aug 2018
Shel - you are using uigetdir to get the source directory source_dir which you use to query for the number of files in that folder which match the filter
d=dir([source_dir,'\6--12-*.dcm']);
but you are not using that source_dir when reading the file
fname=['6--12-',num2str(i), '.dcm'];
indicom=dicomread(fname);
You will want to include the source directory when reading that file
indicom=dicomread(fullfile(source_dir, fname));
Try the above and see what happens! If this doesn't help, then please copy and paste the full error message to this question.
  6 Comments
Shel
Shel on 24 Aug 2018
yes that was the line. I corrected the command as you stated there was an error which the function sprint is not defined to I changed
BasedFileName=sprint('%d.dcm',i);
to
BasedFileName=sprintf('%d.dcm',i);
and now it has the error:
Error using dicom_prep_ImagePixel>getPixelStorage (line 204)
Invalid datatype for image pixels.
Error in dicom_prep_ImagePixel (line 13)
[ba, bs, hb, pr] = getPixelStorage(X, txfr, useExistingBitDepths, metadata, dictionary);
Error in dicom_prep_metadata (line 51)
metadata = dicom_prep_ImagePixel(metadata, X, map, txfr, useMetadataBitDepths, dictionary);
Error in dicom_create_IOD (line 26)
metadata = dicom_prep_metadata(IOD_UID, metadata, X, map, options.txfr,
options.usemetadatabitdepths, dictionary);
Error in dicomwrite>write_message (line 274)
[attrs, status] = dicom_create_IOD(SOP_UID, X, map, ...
Error in dicomwrite (line 210)
[status, options] = write_message(X, filename, map, metadata, options);
Stephen23
Stephen23 on 24 Aug 2018
Edited: Stephen23 on 24 Aug 2018
@Shel: read the dicomwrite help, where it clearly lists the accepted data types for the image as "int8 | int16 | uint8 | uint16". Now look at your code: is medianfilter an integer array, as dicomwrite requires, or is it a cell array? (hint: it is a cell array, because that is what you specified it as).
It is not clear why you are putting all of this data into cell arrays as you do not seem to do any processing with them after the loop, so the cell array is probably just a pointless complication. In any case, if you are using cell arrays then you will need to access the data inside the cell array using indexing:
dicomwrite(medianfilter{i},...)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!