Switch to unified view

a b/Image Segmentation/region growing/runRegionGrowing.m
1
function [ allImages ] = runRegionGrowing( directoryName,...
2
                            thresholdBegin, thresholdEnd, increment )
3
%runRegionGrowing 
4
%   Summary: Run the region growing code based on the parameters provided
5
%       at the end, an array of all image segmentations will be returned
6
%   param directoryName
7
%       pass a string directory name that contains dicom images to be used
8
%       for segmentation
9
%   param thresholdBegin
10
%       pass the beginning threshold value that will do the following:
11
%           "Start region growing until distance between region and posible 
12
%           new pixels become higher than a certain treshold"
13
%       From here, the threshold will be passed to regiongrowing and
14
%       incremented on by the increment variable
15
%   param thresholdEnd
16
%       pass the end threshold value that will do the following:
17
%       "Start region growing until distance between region and posible 
18
%           new pixels become higher than a certain treshold"
19
%       This will mark the end of the incrementation, no more region
20
%       growing will be run
21
%   param increment
22
%       pass value in which the threshold will be incremented on.
23
%       Example: thresholdBegin = 0.2 and increment = .05 
24
%             first iteration threshold = 0.2
25
%             second iteration threshold = 0.25
26
%             third iteration threshold = 0.3
27
%             etc...
28
%
29
%   return allSegmentations
30
%       returns all segmentations created by region growth as a
31
%       two-dimensional cell array
32
%
33
%   return originalImages
34
%       return all original dicom images as a one-dimensional cell array
35
%
36
%   Author: Patrick Stein 7/1/2013
37
38
39
    %% Parameter validation
40
41
    % Validate the following parameters: 
42
    % thresholdBegin, thresholdEnd, increment
43
    % thresholdBegin and thresholdEnd > 0
44
    % thresholdBegin != thresholdEnd
45
    % thresholdEnd > thresholdBegin
46
    % increment > 0
47
    % Directory is an actual directory
48
49
    %Test the following conditions: 
50
    %"thresholdBegin != thresholdEnd && thresholdEnd > thresholdBegin"
51
    if(thresholdEnd <= thresholdBegin)
52
        invalidParam = MException('RunRegionGrowing:InvalidParam:p2_3', ...
53
            'Invalid Parameter: thresholdEnd must be >= thresholdBegin');
54
        throw(invalidParam);
55
    end
56
57
    %Test the following condition: increment > 0
58
    if(increment <= 0)
59
        invalidParam = MException('RunRegionGrowing:InvalidParam:p4', ...
60
            'Invalid Parameter: increment must be >= 0'); 
61
        throw(invalidParam);
62
    end
63
64
    %Test the following condition: thresholdBegin and thresholdEnd > 0
65
    if(thresholdBegin <= 0 || thresholdEnd <= 0)
66
        invalidParam = MException('RunRegionGrowing:InvalidParam:p2_3', ...
67
        'Invalid Parameter: thresholdBegin/End must >= 0');
68
        throw(invalidParam);
69
    end
70
71
    %Test to see if directoryName is a folder
72
    %exists docs: http://www.mathworks.com/help/matlab/ref/exist.html
73
    if(exist(directoryName,'dir') ~= 7)
74
        invalidParam = MException('RunRegionGrowing:InvalidParam:p1', ...
75
        'Invalid Parameter: directoryName is not a  valid directory');
76
        throw(invalidParam);
77
    end
78
79
    %% Pre-processing
80
    
81
    %append \*.dcm to the end of the string so it will only load dicom
82
    %files
83
    directoryNameNew = strcat(directoryName,'\*.dcm');
84
    
85
    % set the directory where nodule crops are stored
86
    imageFiles = dir(directoryNameNew);
87
    
88
  
89
    %get size of directory
90
    sizeDir = size(imageFiles,1);
91
   
92
    %create structure array of all dicom info and segmentations
93
    allImages = struct('imageNumber',0,'imageName',char,...
94
                       'originalImage',[],'segmentations',cell(1,1),...
95
                       'dicomInfo',struct([]),'map',[],'alpha',[],...
96
                       'overlay',[],'segmentationType',char,'masks',...
97
                       cell(1,1));
98
    %Segmentation type    
99
    sg = 'rg';
100
    %% DICOM Segmentation
101
    
102
    %for each file in the directory, do the following
103
    parfor i = 1:sizeDir
104
        
105
        %get image name
106
        in = char(imageFiles(i).name);
107
        
108
        %Construct the file name and path needed to open the file
109
        fn = sprintf('%s/%s', directoryName, in);
110
        
111
        %Load dicom image into matlab, see loadDicom for more information
112
        try
113
            [dicomImage,info,m,a,o] = loadDicom(fn);
114
        catch ME
115
            % TODO: Handle this exception
116
            %rethrow(ME);
117
        end 
118
             
119
        %save segmentation type
120
        allImages(i).segmentationType = sg; 
121
        %save name
122
        allImages(i).imageName = in;
123
        %save image number
124
        allImages(i).imageNumber = str2num( in(1:(strfind(in,'.')-1)) );           
125
        %save dicom image
126
        allImages(i).originalImage = dicomImage;
127
        %save org dicom information
128
        allImages(i).dicomInfo = info;
129
        %save org dicom map
130
        allImages(i).map = m;
131
        %save org dicom alpha
132
        allImages(i).alpha = a;
133
        %save org dicom overlay
134
        allImages(i).overlay = o;
135
        allImages(i).segmentations = getAllSegsRG(dicomImage,thresholdBegin,...
136
                                        thresholdEnd, increment);
137
    end
138
139
    %% Wrap up
140
    %sort the structure array
141
    allImages = nestedSortStruct(allImages, 'imageNumber');
142
       
143
end 
144