Multiple DICOM files from folder to png conversion
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Janice Cruz
am 9 Nov. 2021
Kommentiert: Walter Roberson
am 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 Kommentare
Akzeptierte Antwort
Walter Roberson
am 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 Kommentare
Walter Roberson
am 13 Nov. 2021
Check num_files afterwards. I suspect that it is not locating any dcm files inside the folder.
Weitere Antworten (1)
yanqi liu
am 10 Nov. 2021
Bearbeitet: yanqi liu
am 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 Kommentar
Walter Roberson
am 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.
Siehe auch
Kategorien
Mehr zu DICOM Format finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!