How to resize multiple dicom images in subfolders and save them?

I have multiple subfolders with many MR dicom images with different size. How can I resize them into 128x128 and save them in the same subfolders at once? Thank you in advance!

 Accepted Answer

fd = fullfile(pwd, 'dcms');
fs = ls(fullfile(fd, '*.dcm'));
for i = 1 : size(fs,1)
im = dicomread(fullfile(fd, strtrim(fs(i,:))));
im = imresize(im, [128 128], 'bilinear');
imwrite(im, fullfile(fd, [strtrim(fs(i,:))) '_128x128.dcm']));
end

11 Comments

@yanqi liu Thank you for your response. I tried the code and I am getting the following error.
Error in dicomread>newDicomread (line 194)
fileDetails = images.internal.dicom.getFileDetails(filename, verifyIsDICOM);
Error in dicomread (line 91)
[X, map, alpha, overlays] = newDicomread(msgname, frames, useVRHeuristic);
Error in resize (line 4)
im = dicomread(fullfile(fd, strtrim(fs(i,:))));
That error message is not quite complete.
Did you verify your files are valid DICOM files that Matlab can read?
@Rik Thank you for your response. They are in .dcm format and it says unable to load the file_name.dcm and throws that error. I dont understand why.
But I did resize them using these lines and it worked. I am not sure if its right.
folder = 'E:/Project/Dataset/Images_1';
filelist = dir(fullfile(folder,'*.dcm'));
total_images = numel(filelist);
op_folder = 'E:/Project/Dataset/op_images';
for n = 1:total_images
f = fullfile(folder, filelist(n).name);
my_images = dicomread(f);
resize = imresize(my_images, [128 128]);
output = fullfile(op_folder, filelist(n).name);
dicomwrite(resize, output);
end
yes,sir,can you upload one file to debug
@yanqi liu thank you for your response. I tried uploading but website says file format not supported. I have changed it to .mat not sure if it helps. Thank you.
Hi I tried the above code on multiple folders with dcm images and I am getting the following error. Can someone please help me?
(Posted the code I am refering to)
folder = 'E:/Project/Dataset';
filelist = dir(fullfile(folder,'**/*.dcm'));
total_images = numel(filelist);
op_folder = 'E:/Project/Dataset/op_images';
for n = 1:total_images
f = fullfile(folder, filelist(n).name);
my_images = dicomread(f);
resize = imresize(my_images, [128 128]);
output = fullfile(op_folder, filelist(n).name);
dicomwrite(resize, output);
end
--------------------------------------
Error:
Dot indexing is not supported for variables of this type.
Error in Prog_2 (line 7)
f = fullfile(folder, filelist(n).name);
yes,sir,may be use
f = fullfile(filelist(n).folder, filelist(n).name);
to replace line 7
Hello @yanqi liu. Thank you for your response. The code works, but it stops at a certain point and displays the warning below.
Warning: Not enough data imported. Attempted to read 117886 bytes at position 2138. Only read 13222. > In dicomread>newDicomread (line 204)
I was unable to resize all images. Sorry for the trouble, but I'm not sure what went wrong.
Can someone please help me with this warning and problem. As mentioned above I am not able to resize and save all the images. I get the following warning and the code terminates.
Warning: Not enough data imported. Attempted to read 117886 bytes at position 2138. Only read 13222. > In dicomread>newDicomread (line 204)
Your error seems due to your dicom file, so not something we can debug for you remotely. Unless you use a custom dicom reader and haven't told us yet.
@Rik Thank you for your response. I am not using any dicom reader. I am not sure what is the issue. I just have MR images saved in .dcm format. They are just image files without any metadata. I don't know how to confirm if the images are readable, there are thousands of images with many folders.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 24 Mar 2022

Edited:

on 14 Apr 2022

Community Treasure Hunt

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

Start Hunting!