Multiple DICOM files from folder to png conversion

14 vues (au cours des 30 derniers jours)
Janice Cruz
Janice Cruz le 9 Nov 2021
Commenté : Walter Roberson le 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])

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (1)

yanqi liu
yanqi liu le 10 Nov 2021
Modifié(e) : yanqi liu le 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 commentaire
Walter Roberson
Walter Roberson le 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur DICOM Format dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by