Code covered by the BSD License  

Highlights from
Mia 2.4

image thumbnail

Mia 2.4

by

 

02 Apr 2004 (Updated )

Medical Image Analysis GUI.

loaddcmdir(dicomdirpath)
function [dcmSeries, dcmPatient, SeriesList, SeriesListNumInfo, ImageInfos, SeriesInfos] = loaddcmdir(dicomdirpath)
%function dcmSeries = loaddcmdir(dicomdirpath)
%
% LOADDICOMDIR reads metadata from DICOMDIR file and shows list of
% the different DICOM SERIES in Pop-up menus. After selecting a dicom 
% series the program creates the dcmSeries structure containig the 
% related file names and path.
% 
% Input: (OPTIONAL)
%   dicomdirpath  - the path where the DICOMDIR file is located                       
%   
% Output:
%   dcmSeries.Images        - cell array containing the file names of the images
%   dcmSeries.Path          - directory of the image files
%   dcmSeries.dicomdirPath  - directory of DICOMDIR file 
%   dcmPatient              - the full dicom structure from DICOMDIR
%   SeriesList              - the full list of the available series from DICOMDIR
%                               (cell array)
%   SeriesListNumInfo       - structure containing the patients, study and series 
%                               number relating to the series list numbers  
%
% Matlab library function for MIA_gui utility. 
% University of Debrecen, PET Center/LB 2005

try
    hm = [];
    dcmSeries = []; dcmPatient = []; SeriesList = [];  SeriesListNumInfo = []; 
    ImgPrcTlbx = ver('images');
    ImgPrcTlbxVersion = str2double(ImgPrcTlbx.Version(1));
    if ImgPrcTlbxVersion < 4
        disp('The required minimum version of ImgProc. Toolbox is 4!');
        return; 
    end
    
	if nargin == 0 | strcmp(dicomdirpath,'')  
         dicomdirpath = uigetdir('','Select DICOMDIR folder');
         if dicomdirpath == 0
             return;
         end
    end
    filename1 = fullfile(dicomdirpath,'dicomdir');
    dirres1 = dir(filename1);
    filename2 = fullfile(dicomdirpath,'DICOMDIR');
    dirres2 = dir(filename2);
    if ~isempty(dirres1)
        dcmdir_path = filename1;
    elseif ~isempty(dirres2) 
        dcmdir_path = filename2;
    else
        hm = msgbox('No DICOMDIR in the selected folder !','MIA Info','warn' );
        disp('No DICOMDIR in the selected folder !')
        return;
    end
	
	% Show up message window and set the cursor type to "watch"  
	hm = msgbox('Reading the DICOMDIR structure. It takes time. Please wait!','MIA Info' );
    set(hm, 'tag', 'Msgbox_MIA Info');
	SetData=setptr('watch');set(hm,SetData{:});
	hmc = (get(hm,'children'));
	set(hmc(2),'enable','inactive');
    pause(1);
    
    % open DICOMDIR file & read DirectoryRecordSequence structure 
	dcmhdr = dicominfo(dcmdir_path);
    delete(hm);
    
    % Read the DirectoryRecordSequence structure until end,
    % and read the all included DirectoryRecordType item sequentially
    DirRecords = dcmhdr.DirectoryRecordSequence;
    DirRecordsFieldNames = fieldnames(DirRecords);
    NumOfField = size(DirRecordsFieldNames,1);
    CurrentPatient = 0;
    SeriesListNum = 0;
    for i = 1: NumOfField
        CurrentItem = getfield(DirRecords,DirRecordsFieldNames{i});
        CurrentItemType = getfield(CurrentItem,'DirectoryRecordType');
        if strcmp(CurrentItemType,'PATIENT')
            CurrentPatient = CurrentPatient + 1;
            CurrentStudy = 0;
            if ImgPrcTlbxVersion > 4
                dcmPatient(CurrentPatient,1).PatientName = ...
                    CurrentItem.PatientName.FamilyName;
            elseif ImgPrcTlbxVersion == 4
                dcmPatient(CurrentPatient,1).PatientName = ...
                    CurrentItem.PatientsName.FamilyName;
            end
        elseif strcmp(CurrentItemType,'STUDY')
            CurrentStudy = CurrentStudy + 1; 
            CurrentSeries = 0;
            dcmPatient(CurrentPatient,1).Study(CurrentStudy,1).StudyDescription = ...
                CurrentItem.StudyDescription;
        elseif strcmp(CurrentItemType,'SERIES')
            if CurrentSeries > 0 % create summary about the previous read series for popupmenu
                SeriesListNum = SeriesListNum + 1;
                Pname = dcmPatient(CurrentPatient,1).PatientName;
                Modality = dcmPatient(CurrentPatient,1).Study(CurrentStudy,1).Series(CurrentSeries,1).Modality;
                SeriesDescription = dcmPatient(CurrentPatient,1).Study(CurrentStudy,1). ...
                    Series(CurrentSeries,1).SeriesDescription;
                NumOfImages = num2str(CurrentImage);
                SeriesList{SeriesListNum} = [Pname,',  ',Modality,',  Decription: ', SeriesDescription,',  Number of Images: ',NumOfImages];
                SeriesListNumInfo(SeriesListNum).PatientNum = CurrentPatient;
                SeriesListNumInfo(SeriesListNum).StudyNum = CurrentStudy;
                SeriesListNumInfo(SeriesListNum).SeriesNum = CurrentSeries;
            end
            CurrentSeries = CurrentSeries + 1;
            CurrentImage = 0;
            dcmPatient(CurrentPatient,1).Study(CurrentStudy,1).Series(CurrentSeries,1).Modality = ...
                CurrentItem.Modality;
            if isfield(CurrentItem,'SeriesDescription')
                dcmPatient(CurrentPatient,1).Study(CurrentStudy,1).Series(CurrentSeries,1). ...
                    SeriesDescription = CurrentItem.SeriesDescription;
            else
                dcmPatient(CurrentPatient,1).Study(CurrentStudy,1).Series(CurrentSeries,1). ...
                    SeriesDescription = '';
            end
            %%%
            SeriesInfos{CurrentSeries,1} = CurrentItem.SeriesInstanceUID;
            SeriesInfos{CurrentSeries,2} = CurrentItem.SeriesNumber;
            %%%
        elseif strcmp(CurrentItemType,'IMAGE')
            CurrentImage = CurrentImage + 1;
            [pathstr, dcmfname, ext] = fileparts(CurrentItem.ReferencedFileID);
            if CurrentImage == 1
                dcmPatient(CurrentPatient,1).Study(CurrentStudy,1).Series(CurrentSeries,1). ...
                    ImagePath = pathstr;
            end
            dcmPatient(CurrentPatient,1).Study(CurrentStudy,1).Series(CurrentSeries,1). ...
                ImageNames{CurrentImage,1} = dcmfname;
            dcmPatient(CurrentPatient,1).Study(CurrentStudy,1).Series(CurrentSeries,1). ...
                ReferencedSOPInstanceUIDInFile{CurrentImage,1} = CurrentItem.ReferencedSOPInstanceUIDInFile;
            %%%
            ImageInfos{i,1} = [pathstr,filesep,dcmfname];
            ImageInfos{i,2} = CurrentItem.InstanceNumber;
            ImageInfos{i,3} = CurrentItem.ImageType;
            ImageInfos{i,4} = CurrentItem.ReferencedSOPInstanceUIDInFile;
            ImageInfos{i,5} = SeriesInfos{CurrentSeries,1};
            ImageInfos{i,6} = CurrentSeries;
            ImageInfos{i,7} = SeriesInfos{CurrentSeries,2};
            %%%%
        end
        
    end
    % create summary about the last read series for popupmenu
    SeriesListNum = SeriesListNum + 1;
    Pname = dcmPatient(CurrentPatient,1).PatientName;
    Modality = dcmPatient(CurrentPatient,1).Study(CurrentStudy,1).Series(CurrentSeries,1).Modality;
    SeriesDescription = dcmPatient(CurrentPatient,1).Study(CurrentStudy,1). ...
        Series(CurrentSeries,1).SeriesDescription;
    NumOfImages = num2str(CurrentImage);
    SeriesList{SeriesListNum} = [Pname,',  ',Modality,',  Decription: ', SeriesDescription,',  Number of Images: ',NumOfImages];
    SeriesListNumInfo(SeriesListNum).PatientNum = CurrentPatient;
    SeriesListNumInfo(SeriesListNum).StudyNum = CurrentStudy;
    SeriesListNumInfo(SeriesListNum).SeriesNum = CurrentSeries;
    
    % create popupmenu for dicomseries
    global dcmdirlistVal;
    dcmdirlistVal = 0;
    dcmdirlistfh = figure('menubar','none','NumberTitle','off','name','DICOMDIR info');
    lbh = uicontrol('Style','listbox','Position',[10 60 520 340],'tag','dcmdirlist_popupmenu');
    set(lbh,'string',SeriesList);
    OKCallback = 'global dcmdirlistVal; dcmdirlistVal = get(findobj(''tag'',''dcmdirlist_popupmenu''),''value''); delete(findobj(''name'',''DICOMDIR info''));';
    CancelCallback = 'delete(findobj(''name'',''DICOMDIR info''));';
    OK_h = uicontrol('Style', 'pushbutton', 'String', 'OK','Position', [440 10 80 30], 'Callback', OKCallback);
    Cancel_h = uicontrol('Style', 'pushbutton', 'String', 'Cancel','Position', [340 10 80 30], 'Callback', CancelCallback);
    
    uiwait(dcmdirlistfh);
    
    if dcmdirlistVal == 0;
        disp('No DICOM Series was selected!');
        return;
    end
    
    % create the outputs
    dcmSeriesPath = dcmPatient(SeriesListNumInfo(dcmdirlistVal).PatientNum). ...
        Study(SeriesListNumInfo(dcmdirlistVal).StudyNum). ...
        Series(SeriesListNumInfo(dcmdirlistVal).SeriesNum). ...
        ImagePath;
    dcmSeries.Path = [dicomdirpath, filesep, dcmSeriesPath, filesep];
    dcmSeries.dicomdirpath = dicomdirpath;
    dcmSeries.Images = dcmPatient(SeriesListNumInfo(dcmdirlistVal).PatientNum). ...
        Study(SeriesListNumInfo(dcmdirlistVal).StudyNum). ...
        Series(SeriesListNumInfo(dcmdirlistVal).SeriesNum). ...
        ImageNames;
    
catch %in case of any error
    ErrorOnDicomDirOpening = lasterr
    if isobject(hm)
        delete(hm);
    end
end

Contact us