how to read and save dicom images in different directories
Show older comments
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
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
Geoff Hayes
on 24 Aug 2018
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.
Shel
on 24 Aug 2018
Geoff Hayes
on 24 Aug 2018
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?
Shel
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},...)
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!