0
votes

Am new to matlab and am trying to write a code that converts CT lung DICOM images to Hounsfield Units(HU). I've already created a function to do so and saved it in an M-file. I would like to know how can i apply this function to a series of dicom images altogether (each patient folder contains around 200 images and there are multiple folders!) or how to apply a function to a series of dicom images in general. thanks in advance! Here's the function:

function [z,y] = med (i)
z = dicominfo(i);
x = dicomread(z);

if isa(x,'int16')
    y = x * z.RescaleSlope + z.RescaleIntercept;
else
    a = int16(x);
    y = a * z.RescaleSlope + z.RescaleIntercept;
end
1

1 Answers

0
votes

Please add this code in the same folder where your function. save this file start.m

 FD1=getAllFiles('Dataset\your_data_folder');
   for f=1:size(FD1)
   im=dicomread(FD1{f});
   [z,y] = med (f)  %your function

 end

save this file getAllFiles.m

function fileList = getAllFiles(dirName)

dirData = dir(dirName);      %# Get the data for the current directory
dirIndex = [dirData.isdir];  %# Find the index for directories
fileList = {dirData(~dirIndex).name}';  %'# Get a list of the files
if ~isempty(fileList)
fileList = cellfun(@(x) fullfile(dirName,x),...  %# Prepend path to files
fileList,'UniformOutput',false);

end

subDirs = {dirData(dirIndex).name};  %# Get a list of the subdirectories
validIndex = ~ismember(subDirs,{'.','..'});  %# Find index of     subdirectories
                                    %#   that are not '.' or '..'
for iDir = find(validIndex)        %# Loop over valid subdirectories
nextDir = fullfile(dirName,subDirs{iDir});  %# Get the subdirectory path
fileList = [fileList; getAllFiles(nextDir)];%# Recursively call getAllFiles
end

end

now you can take hunderds of images from the same folder.