Search code examples
matlabimage-processingsavepngdicom

Matlab: Saving multiple Dicom files as jpeg or png format


I can read a Dicom format file and display it using imshow(). Then, manualy I can save the file into jpeg or png using saveas option. This works for one file at a time. However, I have around 1000 such dicom files. How can I save multiple dicom data files as jpeg format into another folder?

This is how I read one file at a time:

 X = dicomread('C:\Users\skm\Desktop\DicomRaw\578A0BF9');
imshow(X);
%Then from the figure I go to saveas option to save the file as jpeg.

This is what I have tried for multiple files

%read multiple images

FileList = dir('C:\Users\skm\Desktop\DicomRaw\*.*');
Converted_jpeg = dir('C:\Users\skm\Desktop\Jpeg_file\*.*');

N = size(FileList,1);

for k = 1:N

   % get the file name:
   filename = FileList(k).name
   disp(filename);

end

pic


Solution

  • You may use the following code sample:

    indir = 'C:\Users\skm\Desktop\DicomRaw\';  %Input folder name with DICOM files.
    outdir = 'C:\Users\skm\Desktop\Jpeg_file\'; %Output folder name for storing jpg files.
    FileList = dir([indir, '*.*']); %Get list of all files in input folder.
    
    for k = 1:N
        % get the file name:
        filename = FileList(k).name;
    
        %Verify there is no dot in file name (in case folder contains other files).
        if (~any(filename == '.'))
            X = dicomread([indir, filename]);
    
            %Add .jpg extension to file name
            out_filename = [filename, '.', 'jpg'];
    
            %Use imwrite for saving (better than using imshow and saveas).
            imwrite(X, [outdir, out_filename]);
        end
    end