Multiple DICOM files from folder to png conversion

15 visualizzazioni (ultimi 30 giorni)
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])

Risposta accettata

Walter Roberson
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
Janice Cruz
Janice Cruz il 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 il 13 Nov 2021
Check num_files afterwards. I suspect that it is not locating any dcm files inside the folder.

Accedi per commentare.

Più risposte (1)

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

Accedi per commentare.

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!

Translated by