-
Notifications
You must be signed in to change notification settings - Fork 4
/
getGLCM_Symmetric.m
154 lines (132 loc) · 5.68 KB
/
getGLCM_Symmetric.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
function coocMat = getGLCM_Symmetric(varargin)
%inputStr = {TumorVolume,'Distance',[],'Direction',[],'numgray',levelsM+1};
%
%Southern Medical University
%
%Default settings
coocMat= NaN;
distance = [1;2;4;8];
numLevels = 16;
offSet = [1 0 0; 1 1 0; 0 1 0; -1 1 0]; %2D Co-Occurrence directions 0,45,90,135 degrees
%the additional 9 directions of 3D volume
dimension3 = [0 0 1; 1 0 1; -1 0 1; 0,1,1; 0 -1 1; 1 1 1; -1 1 1; 1 1 -1; 1 1 -1];
offSet = cat(1,offSet,dimension3);%13 directions
%checking inputs
data = varargin{1};
temp = size(data);
if size(temp)<3
disp('Error: This program is designed for 3 dimensional data')
return;
end
numInput = size(varargin,2);
for inputs =2:numInput
temp = varargin{1,inputs};
if ~ischar(temp)
continue;
end
temp = upper(temp);
switch (temp)
case 'DIRECTION'
temp2 = int8(varargin{1,inputs+1});
if size(size(temp2),2) ~=2
disp('Error: Direction input is formatted poorly')
return;
end
if size(temp2,2) ~=3
disp(['Error: Incorrect number of columns in ' ...
'direction variable'])
return;
end
if max(max(temp2))>1 | min(min(temp2))<-1
disp('Error: Direction values can only be {-1,0,1}')
return;
end
offSet = temp2;
case 'DISTANCE'
temp2 = int8(varargin{1,inputs+1});
if size(size(temp2)) ~= 2
disp('Error: Incorrect formatting of distance variable')
return;
end
if sum(sum(size(temp2))) ~= max(size(temp2)+1)
disp(['Error: Distance variable is to be a one ' ...
'dimensional array'])
return;
end
distance = temp2;
case 'NUMGRAY'
temp2 = varargin{1,inputs+1};
if temp2<1
disp('The number of graylevels must be positive')
return;
end
numLevels = uint16(temp2);
end
end
noDirections = size(offSet,1); %number of directions, currently 13
coocMat = zeros(numLevels, numLevels, noDirections, size(distance,2));
for dist =1:size(distance,2) %distance
[coocMat(:,:,:,dist)] = graycooc3d(data(:,:,:),distance(dist),numLevels,offSet);
end
return
function [new_coMat]= graycooc3d(I,distance,numLevels,offSet)
%I = the 3D image matrix
%distance = a vector of the distances to analyze in
%numLevels = the number of graylevels to be used
%offSet = a matrix of the directions to analyze in
%coMat the Co-Occurrence matrices produced%%
%**************Variable initialization/Declaration**********************
%harMat =0;
noDirections = size(offSet,1); %number of directions, currently 13
coMat = zeros(numLevels,numLevels,noDirections);
%**************************Beginning analysis*************************
%Order of loops: Direction, slice, graylevel, graylevel locations
for direction =1:noDirections %currently 13 (for the 3d image)
tempMat = zeros(numLevels,numLevels,size(I,3));
for slicej =1:size(I,3)
for j=1:numLevels %graylevel
%find all the instances of that graylevel
[rowj,colj] = find(I(:,:,slicej)==j);
%populating the Cooc matrix.
for tempCount = 1:size(rowj,1)
rowT = rowj(tempCount) + distance*offSet(direction,1);
colT = colj(tempCount) + distance*offSet(direction,2);
sliceT = slicej + distance*offSet(direction,3);
rowTnegative = rowj(tempCount) - distance*offSet(direction,1);%the symmetry of GLCM.
colTnegative = colj(tempCount) - distance*offSet(direction,2);%the symmetry of GLCM.
sliceTnegative = slicej - distance*offSet(direction,3);%the symmetry of GLCM.
[I1, I2, I3] = size(I);
if rowT <= I1 && colT <= I2 && sliceT <= I3
if rowT > 0 && colT > 0 && sliceT > 0
%Error checking for NANs and Infinite numbers
IIntensity = I(rowT,colT,sliceT);
if ~isnan(IIntensity)
if ~isinf(IIntensity)
tempMat(j,IIntensity,slicej)= tempMat...
(j,IIntensity,slicej)+1;
end
end
end
end
if rowTnegative <= I1 && colTnegative <= I2 && sliceTnegative <= I3
if rowTnegative > 0 && colTnegative > 0 && sliceTnegative > 0
%Error checking for NANs and Infinite numbers
IIntensitynegative = I(rowTnegative,colTnegative,sliceTnegative);% added by
if ~isnan(IIntensitynegative)
if ~isinf(IIntensitynegative)
tempMat(j,IIntensitynegative,slicej)= tempMat...
(j,IIntensitynegative,slicej)+1;
end
end
end
end
end
end
end
for slicej =1:size(I,3)
coMat(:,:,direction)= coMat(:,:,direction)+tempMat(:,:,slicej);
end
end
new_coMat=coMat(:,:,:);
return