how to read and save dicom images in different directories

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)

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

well, thank you here is the new code ... please let me know if it is not correct:
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(fullfile(source_dir, 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
and here is the error:
Error using title (line 21)
Incorrect number of input arguments
Besides, there is no output in
C:\Users\Shan\Desktop\CT Images\Median
Please see title for how this function is used. In your case, I suspect you want to combine the title message and number into a string that you then pass as the input to title. Using sprintf you could do
title(sprintf('median filtered image number %d',i));
As for no files being written to the output folder, please confirm that FilteredFileName is valid. Also, use breakpoints to step through the code to get an idea of what is happening. When the dicomwrite function has been called, check the console (command window) for any errors or warnings.
Hi, thanks for your time. I changed it again several time: here is the code:
source_dir='C:\Users\Shan\Desktop\CT Images';
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);
indicom=dicomread(fullfile(source_dir, fname));
subplot(1,2,1), imshow(indicom,[]);
title(sprintf('original image number %d',i));
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(sprintf('median filtered image number %d',i));
end
there is a warning on line 11:
the variable medianfilter appears to change size on every loop iteration (within a script) .consider preallocation for speed
and the error is:
Cell contents reference from a non-cell array object.
which line is generating the error? is it this line?
medianfilter{i}=medfilt2(indicom{i},[4 4]);
why are you treating indicom as if it were a cell array? Wouldn't you just do
medianfilter{i}=medfilt2(indicom,[4 4]);
instead?
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);
@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.

Categories

Asked:

on 23 Aug 2018

Edited:

on 24 Aug 2018

Community Treasure Hunt

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

Start Hunting!