How to resize multiple dicom images in subfolders and save them?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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
yanqi liu
on 25 Mar 2022
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,:))));
Rik
on 25 Mar 2022
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
yanqi liu
on 26 Mar 2022
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.
Shourya
on 30 Mar 2022
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);
yanqi liu
on 31 Mar 2022
yes,sir,may be use
f = fullfile(filelist(n).folder, filelist(n).name);
to replace line 7
Shourya
on 5 Apr 2022
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.
Shourya
on 14 Apr 2022
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)
Rik
on 14 Apr 2022
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.
More Answers (0)
Categories
Find more on DICOM Format in Help Center and File Exchange
Tags
See Also
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)