Multiple DICOM files from folder to png conversion
15 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Janice Cruz
il 9 Nov 2021
Commentato: Walter Roberson
il 13 Nov 2021
I am trying to convert a folder of 200+ DICOM files (.dcm) to a folder of PNG images and save them, but am stuggling with proper execution of the code (Beginner)
This is what I have so far:
i = 0;
foldername = 'LowerLeg';
outdir = 'lowerleg_imgs';
dicomlist = dir(fullfile(foldername,'*.dcm'));
for cnt = 1 : numel(dicomlist)
i= i+1;
thisfile = fullfile(foldername, dicomlist(i).name);
I{cnt} = dicomread(thisfile);
end
out_file = [filename, '.', 'png']
imwrite(I{cnt}, [outdir, out_file])
0 Commenti
Risposta accettata
Walter Roberson
il 10 Nov 2021
foldername = 'LowerLeg';
outdir = 'lowerleg_imgs';
dicomlist = dir(fullfile(foldername,'*.dcm'));
num_files = numel(dicomlist);
I = cell(num_files, 1);
for cnt = 1 : num_files
thisfile = fullfile(foldername, dicomlist(cnt).name);
[~, basename] = fileparts(thisfile);
outfile = fullfile(outdir, basename + ".png");
I{cnt} = dicomread(thisfile);
imwrite(I{cnt}, outfile);
end
However, if you do not need all of the data together afterwards, then do not bother to save it:
foldername = 'LowerLeg';
outdir = 'lowerleg_imgs';
dicomlist = dir(fullfile(foldername,'*.dcm'));
num_files = numel(dicomlist);
for cnt = 1 : num_files
thisfile = fullfile(foldername, dicomlist(cnt).name);
[~, basename] = fileparts(thisfile);
outfile = fullfile(outdir, basename + ".png");
I = dicomread(thisfile);
imwrite(I, outfile);
end
2 Commenti
Walter Roberson
il 13 Nov 2021
Check num_files afterwards. I suspect that it is not locating any dcm files inside the folder.
Più risposte (1)
yanqi liu
il 10 Nov 2021
Modificato: yanqi liu
il 11 Nov 2021
clc; clear all; close all;
foldername = './LowerLeg';
outdir = './lowerleg_imgs';
if ~exist(outdir, 'dir')
mkdir(outdir);
end
dicomlist = dir(fullfile(foldername,'*.dcm'));
for cnt = 1 : numel(dicomlist)
thisfile = fullfile(foldername, dicomlist(cnt).name);
I{cnt} = dicomread(thisfile);
out_file = [dicomlist(cnt).name, '.', 'png']
imwrite(mat2gray(I{cnt}), fullfile(outdir, out_file))
end
1 Commento
Walter Roberson
il 10 Nov 2021
filename is undefined. It obviously needs to be updated each time you read a new file.
I is not being pre-allocated, so it has to grow in place each time you read another image.
Vedere anche
Categorie
Scopri di più su DICOM Format in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!