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

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.
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
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...
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?
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]) )

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Convert 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