2017-04-18 40 views
0

我是matlab新手,正在嘗試編寫將CT肺DICOM圖像轉換爲Hounsfield單位(HU)的代碼。我已經創建了一個函數來將其保存在一個M文件中。我想知道如何將此功能完全應用於一系列dicom圖像(每個患者文件夾包含大約200張圖像,並有多個文件夾!)或如何將函數應用於一系列dicom圖像。提前致謝! 這裏的功能:如何將函數應用於Matlab中的一系列CT dicom圖像?

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 

回答

0

請在同一文件夾在您的函數中添加此代碼。保存此文件start.m

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

end 

保存此文件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 

現在你可以拍攝圖像的hunderds來自同一個文件夾中。

相關問題