Problem with sprintf and read DICOM images

Dear all,
I have CT slices and I would like to load (imread) them to the Workspace. But I always read only one (first) slice (image). This is my code:
for p=1:38
filename = sprintf('C:/Users/ced0005/DP/DP_framework/DICOM/slicesCT/000048.dcm',p);
X(:,:,1,p) = dicomread(filename);
I don´t know, where mistake is.
Can you advise me? Thank you for your answers.

 Accepted Answer

Jan
Jan on 4 Apr 2017
Edited: Jan on 4 Apr 2017
There is no format specifier in the format string of sprintf:
sprintf('C:/Users/ced0005/DP/DP_framework/DICOM/slicesCT/000048.dcm',p);
This is the same string for all iterations. Perhaps you want:
filename = sprintf('C:/Users/ced0005/DP/DP_framework/DICOM/slicesCT/%06d.dcm', p);

12 Comments

Okay, it will help, but my first CT scan is 000048.dcm not 000001.dcm. Could I somehow ensure this?
Error using dicomread>getFileDetails (line 1458)
File "C:/Users/ced0005/DP/DP_framework/DICOM/slicesCT/000001.dcm" not found.
@Veronika: :-) Then start the loop at 48:
Folder = 'C:/Users/ced0005/DP/DP_framework/DICOM/slicesCT/'; % Nicer
for p = 48:86
filename = fullfile(Folder, sprintf('%06d.dcm', p));
X(:, : , 1, p - 47) = dicomread(filename);
end
Or:
for p = 1:38
filename = fullfile(Folder, sprintf('%06d.dcm', p + 47));
X(:, : , 1, p) = dicomread(filename)
end
You are welcome.
HI.. May i know what is the meaning for "p = 48:86" and "for p = 1:38" actually?
if i have 135 slice CT images, what should i writ the value of p?
first_to_read = 48;
last_to_read = 86;
num_to_read = last_to_read - first_to_read + 1;
Now you can use
for file_idx = 1 : num_to_read
img_number = file_idx + first_to_read - 1;
filename = fullfile(Folder, sprintf('%06d.dcm', img_number));
X(:, : , 1, file_idx) = dicomread(filename);
end
in which case file_idx refers to the relative number to read, which can be quite useful for several different reasons.
You could also use
for file_number = first_to_read : last_to_read
file_idx = file_number - first_to_read + 1;
filename = fullfile(Folder, sprintf('%06d.dcm', img_number));
X(:, : , 1, file_idx) = dicomread(filename);
end
which is sometimes easier to read. But in practice it turns out to be easier to work with relative indices in a for loop: when relative indices are used it is not necessary that the values be consecutive:
files_to_read = [48:63 72:78 85]; %does not need to be consecutive
num_to_read = length(files_to_read);
for file_idx = 1 : num_to_read
img_number = files_to_read(file_idx);
filename = fullfile(Folder, sprintf('%06d.dcm', img_number));
X(:, : , 1, file_idx) = dicomread(filename);
end
Dear Walter Roberson,
I used this command to read multiple image CT. i have 135 slice CT images (in format dicom). My first image name start with "CTAC001_CT001.dcm" until "CTAC001_CT135.dcm"
I used this command:
for file_number = first_to_read : last_to_read
file_idx = file_number - first_to_read + 1;
filename = fullfile(Folder, sprintf('%06d.dcm', img_number));
X(:, : , 1, file_idx) = dicomread(filename);
end
Undefined function or variable 'first_to_read'.
But the ERROR appear as below:
Error in trymultipleimages (line 1)
for file_number = first_to_read : last_to_read
Can you help me please...
Notice my lines near the beginning,
first_to_read = 48;
last_to_read = 86;
Meaning that, i have to write like this:
first_to_read = CTAC001_CT001.dcm;
last_to_read = CTAC001_CT135.dcm;
num_to_read = last_to_read - first_to_read + 1;
for file_number = first_to_read : last_to_read
file_idx = file_number - first_to_read + 1;
filename = fullfile(Folder, sprintf('%06d.dcm', img_number));
X(:, : , 1, file_idx) = dicomread(filename);
end
is it correct?
last_to_read = 135;
for file_number = 1 : last_to_read
filename = fullfile(Folder, sprintf('CTAC001_CT%03d.dcm', file_number));
X(:, : , 1, file_number) = dicomread(filename);
if file_number == 1 && last_to_read ~= 1
X(end, end, end, last_to_read) = 0;
end
end
The special test for file_number 1 is a performance optimization to avoid having to grow the array every iteration.
Dear Walter Roberson,
The code is successfully. I have try for 135 slices CT images and also 135 slices PET images.
But how come to me to show it in figure (i mean use command imshow)
It cannot be shown in a single figure using imshow. imshow() is not suitable for volume visualization.
If you have R2017a or newer, try
volumeViewer( permute(X, [1 2 4 3]) )

Sign in to comment.

More Answers (0)

Categories

Asked:

on 4 Apr 2017

Commented:

on 17 Nov 2017

Community Treasure Hunt

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

Start Hunting!