Multiple DICOM files from folder to png conversion

10 Ansichten (letzte 30 Tage)
Janice Cruz
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])

Akzeptierte Antwort

Walter Roberson
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
Janice Cruz
Janice Cruz am 10 Nov. 2021
Thanks Walter! This code runs without errors but the outdir folder with the png images is still empty. Both the called folders are in the pathway.
foldername = 'Subject2_LowerLeg_Proximal';
outdir = 'Prox_lowerleg_png';
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
Walter Roberson
Walter Roberson am 13 Nov. 2021
Check num_files afterwards. I suspect that it is not locating any dcm files inside the folder.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

yanqi liu
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
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.

Melden Sie sich an, um zu kommentieren.

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!

Translated by