-
Notifications
You must be signed in to change notification settings - Fork 1
/
EFMain.m
257 lines (198 loc) · 8.7 KB
/
EFMain.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
function [dataset] = EFMain(nameDataset, sizeSubImage, vetDescriptors, normalize, vetOutputFiles, saveIndexFile)
% *************************************************************************
% EFMain: extract subimage features from image files in folder 'images'
% (labels came from ground truth files (in 'GT' folder).
% 'nameDataset' is the name of the dataset as output in
% folder 'data'.
%
% Example: [dataset] = EFMain('dataset1', 16, [1,0,0,0], true, [0,0,1], 1);
%
% Author: Luiz F. S. Coletta ([email protected]) - 30/01/18
% Update: Luiz F. S. Coletta - 30/01/19
% *************************************************************************
nDataset = 'dataset'; % name of the dataset
if (nargin >= 1)
nDataset = nameDataset;
end
sSubImage = 16; % square pixel matrix order for subimages
if (nargin >= 2)
sSubImage = sizeSubImage;
end
vDescriptors = [1, ... % CIE-LAB (2 features)
0, ... % BIC
0]; % Haralick6
if (nargin >= 3)
vDescriptors = vetDescriptors;
end
normAttrib = true; % normalize attributes to [0, 1]
if (nargin >= 4)
normAttrib = normalize;
end
vOutputFiles = [1, ... % .txt
1, ... % .csv
1]; % .arff
if (nargin >= 5)
vOutputFiles = vetOutputFiles;
end
sIndexFile = true; % outputs a index file for the data
if (nargin >= 6)
sIndexFile = saveIndexFile;
end
outputPath = [pwd, '/data/']; % path where datasets will be placed
inputImagePath = [pwd, '/images/']; % path with images to be analyzed
inputGroundTruthPath = [pwd, '/images/GT/']; % path with ground truth
%if (exist([outputPath, nDataset], 'file'))
% dataset = load([outputPath, nDataset], '\t');
%else
dataset = [];
allFiles = dir(inputImagePath);
allNames = {allFiles(~[allFiles.isdir]).name};
%r = randperm(size(allNames,2));
r = 1:size(allNames,2);
position = [];
nameFileOld = [];
for i = 1:size(allNames,2) % iterate files in 'data'
left = 0;
top = left;
width = sSubImage;
height = width;
nameFile = strjoin(allNames(r(i)));
if (strcmp(nameFile(size(nameFile,2)-6:size(nameFile,2)-4),'RGB'))
fullImage = imread([inputImagePath, nameFile]);
if (~strcmp(nameFile,nameFileOld))
nFRED = [nameFile(1:strfind(nameFile,'_RGB')),'RED.tif'];
nFNIR = [nameFile(1:strfind(nameFile,'_RGB')),'NIR.tif'];
fullFNRed = fullfile(inputImagePath, nFRED);
fullFNNIR = fullfile(inputImagePath, nFNIR);
RED = imread(fullFNRed);
RED = RED(:,:,1);
NIR = imread(fullFNNIR);
NIR = NIR(:,:,1);
matNDVI = EFNDVI(NIR, RED);
end
%%%%%%%%%%%%%%%%%%%%%
red = fullImage(:,:,1); % Red channel
green = fullImage(:,:,2); % Green channel
blue = fullImage(:,:,3); % Blue channel
fullImage = cat(3, red, green, blue);
[rows, cols, ~] = size(fullImage);
% Getting ground truth image if exists
nameFileGT = ['gt', strjoin(allNames(r(i)))];
if (exist([outputPath, nameFileGT], 'file'))
withGT = 1;
else
withGT = 0;
end
if (withGT)
fullImageGT = imread([inputGroundTruthPath, nameFileGT]);
end
cont = 1;
for j = 1:rows/width % iterate getting subimages from current file (lines)
left = 0;
width = sSubImage;
for k = 1:cols/width % iterate getting subimages from current file (columns)
p = [nameFile, ';', int2str(j), ';', int2str(k), ';', int2str(cont)];
position = [position; [p, repmat(' ', [1,50-size(p,2)])]];
fprintf('Extracting features of %s - [%d x %d]\n', nameFile, j, k);
subImage = imcrop(fullImage, [left, top, width, height]); % left, top, width, height]
if (cont == 3653)
%figure, imshow(subImage)
end
if (withGT)
subImageGT = imcrop(fullImageGT, [left, top, width, height]);
if (max(max(subImageGT))/255 == 1)
label = 1;
elseif (max(max(subImageGT)) == 0)
label = 0;
else
label = -1;
end
end
%figure, imshow(subImageGT)
left = left + width + 1;
if (k == 1)
width = width - 1;
end
% *************************************
% **** DESCRIPTORS FUNCTIONS HERE *****
% *************************************
featureVector = [];
if (vDescriptors(1))
% generates 3 features (avg(l*) avg(a*) and avg(b*))
lab_features = EFLAB(subImage, [1,1,1]);
lab_features(isnan(lab_features)) = 0;
featureVector = lab_features;
end
if (vDescriptors(2))
% default quantization = 64 (128 features); 16 (32 features)
% reduced because sparse matrices
bic_features = EFBIC(subImage, 8);
featureVector = [featureVector, bic_features'];
end
if (vDescriptors(3))
% default neighbors = 8 (48 features);
img_gray = rgb2gray(subImage);
%angle = [[0 1]; [-1 1]; [-1 0]; [-1 -1]; [0 -1]; [1 -1]; [1 0]; [1 1]];
angle = [[0 1]; [-1 0]; [0 -1]; [1 0]];
haralick6 = [];
for l = 1:size(angle,1)
glcms = graycomatrix(img_gray, 'offset', angle(l,:), 'Symmetric', true);
HF = EFGLCM(glcms);
vecHF = [HF.maximumProbability; HF.correlation; HF.contrast; HF.energy; HF.homogeneity; HF.entropy];
vecHF(isnan(vecHF)) = 0;
haralick6 = [haralick6; vecHF];
end
featureVector = [featureVector, haralick6'];
end
if (vDescriptors(4))
NDVIsubImage = imcrop(matNDVI, [left, top, width, height]);
NDVIMedio = mean(mean(NDVIsubImage));
featureVector = [featureVector, NDVIMedio];
end
if (withGT)
featureVector = [featureVector, label];
end
% CONCATENATING FEATURES AND LABELS
dataset = [dataset; featureVector];
% *******************************************************************
% *******************************************************************
% *******************************************************************
cont = cont + 1;
end
top = top + height + 1;
if (j == 1)
height = height - 1;
end
end
end
nameFileOld = nameFile;
end
if (normAttrib)
minVal = min(dataset);
maxVal = max(dataset);
norm_data = [];
for m = 1:size(dataset,2)-withGT
vecN = (dataset(:, m) - minVal(m))/(maxVal(m) - minVal(m));
vecN(isnan(vecN)) = 0;
norm_data = [norm_data, vecN];
end
if (withGT)
dataset = [norm_data, dataset(:,size(dataset,2))];
else
dataset = [norm_data];
end
end
% Saving output files
if (vOutputFiles(1))
dlmwrite([outputPath, nDataset, '.txt'], dataset, 'delimiter', '\t', 'precision', 4);
end
if (vOutputFiles(2))
csvwrite([nDataset, '.csv'], dataset)
end
if (vOutputFiles(3))
ArffWriter(outputPath, nDataset, dataset, withGT);
end
if (sIndexFile)
dlmwrite([outputPath, nDataset, '-Index.txt'], position, 'delimiter', '');
end
end