How to loop all slice locations from a image volume as a single column vector?

Hello, I am reading DICOM images from a folder and want to read all image slice locations such that I can have all slice locations as a column vector (I have 71 slices, so want to have 71 slice locations). Below is my attempt but could not able to figure out the way, can anyone help me?
%%
close all; clear; clc;
%%
% List all files in the folder
Files = dir('/etc/*');
for k = 1 : length(Files)
baseFileName = Files(k).name;
fullFileName = fullfile(Files(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
end
%%
info = dicominfo(fullFileName);
slLoc = info.SliceLocation; % this will throw the slice location of the last Dicom image
% I want to write slLoc in a column vector such that all image slice locations are listed

 Réponse acceptée

Voss
Voss le 28 Juin 2022
Modifié(e) : Voss le 28 Juin 2022
Move the last two lines inside the loop and assign info.SliceLocation to the kth element of slLoc:
%%
close all; clear; clc;
%%
% List all files in the folder
Files = dir('/etc/*');
% slLoc = zeros(numel(Files),1); % pre-allocate slLoc
slLoc = cell(numel(Files),1); % pre-allocate slLoc
for k = 1:numel(Files)
baseFileName = Files(k).name;
fullFileName = fullfile(Files(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
info = dicominfo(fullFileName);
% slLoc(k,1) = info.SliceLocation;
slLoc{k,1} = info.SliceLocation;
end
I don't know what class of data SliceLocation will be, so I've put it in a cell array there. Maybe it can be a numeric array or a string array.

8 commentaires

Running this throws the following error:
Error using fseek
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in dicom_getFileDetails>getFileLength (line 33)
fseek(fid, 0, 'eof');
Error in dicom_getFileDetails (line 21)
details.bytes = getFileLength(fid);
Error in dicominfo (line 55)
fileDetails = dicom_getFileDetails(filename);
Error in testhundred (line 36)
info = dicominfo(fullFileName);
Any hints on this?
Try this - it works for me.
% List all files in the folder
Files = dir('\etc\**\*.dcm')
% slLoc = zeros(numel(Files),1); % pre-allocate slLoc
slLoc = cell(numel(Files),1); % pre-allocate slLoc
for k = 1 : numel(Files)
baseFileName = Files(k).name;
fullFileName = fullfile(Files(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
info = dicominfo(fullFileName);
% slLoc(k,1) = info.SliceLocation;
if isfield(info, 'SliceLocation')
slLoc{k,1} = info.SliceLocation;
end
end
slLoc
Thank you - this works for me too. But when I tried to sort slLoc then it throws an error - Input argument must be a cell array of character vectors.
How can I sort this slLoc vector such that I can write an ascending vector?
I tried: slLoc = sort(slLoc); but this did not work!
Sounds like (maybe) slLoc is a cell array of numeric scalars, in which case you can make it a numeric array instead:
% List all files in the folder
Files = dir('\etc\**\*.dcm')
slLoc = zeros(numel(Files),1); % pre-allocate slLoc
for k = 1 : numel(Files)
baseFileName = Files(k).name;
fullFileName = fullfile(Files(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
info = dicominfo(fullFileName);
if isfield(info, 'SliceLocation')
slLoc(k,1) = info.SliceLocation;
end
end
slLoc = sort(slLoc)
Voss: I tried it but error says -- Conversion to cell from double is not possible.
Check the pre-allocation line, before the loop:
slLoc = zeros(numel(Files),1); % pre-allocate slLoc
(not a cell array - a vector of zeros).
This works great - thank you.
You're welcome! Glad you got it working!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur DICOM Format dans Centre d'aide et File Exchange

Question posée :

le 28 Juin 2022

Commenté :

le 29 Juin 2022

Community Treasure Hunt

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

Start Hunting!

Translated by