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.

 採用された回答

Jan
Jan 2017 年 4 月 4 日
編集済み: Jan 2017 年 4 月 4 日

1 投票

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 件のコメント

Veronika
Veronika 2017 年 4 月 5 日
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.
Jan
Jan 2017 年 4 月 5 日
編集済み: Jan 2017 年 4 月 5 日
@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
Veronika
Veronika 2017 年 4 月 5 日
Thank you.:-)
Jan
Jan 2017 年 4 月 5 日
You are welcome.
mohd akmal masud
mohd akmal masud 2017 年 11 月 8 日
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?
Walter Roberson
Walter Roberson 2017 年 11 月 9 日
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
mohd akmal masud
mohd akmal masud 2017 年 11 月 17 日
編集済み: Walter Roberson 2017 年 11 月 17 日
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...
Walter Roberson
Walter Roberson 2017 年 11 月 17 日
Notice my lines near the beginning,
first_to_read = 48;
last_to_read = 86;
mohd akmal masud
mohd akmal masud 2017 年 11 月 17 日
編集済み: Walter Roberson 2017 年 11 月 17 日
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?
Walter Roberson
Walter Roberson 2017 年 11 月 17 日
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.
mohd akmal masud
mohd akmal masud 2017 年 11 月 17 日
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)
Walter Roberson
Walter Roberson 2017 年 11 月 17 日
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]) )

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeConvert Image Type についてさらに検索

質問済み:

2017 年 4 月 4 日

コメント済み:

2017 年 11 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by