-
Notifications
You must be signed in to change notification settings - Fork 1
/
GetAllFeatures.m
389 lines (293 loc) · 16.7 KB
/
GetAllFeatures.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
function [CCs,CCstats,Features,FinalBinImages] = GetAllFeatures(BinImages,BinSizes,MAX_DISTANCE,StabilityCheckMatrix,getFinalBinImages) % Make 2nd one false for speed
%% Features Extracted
% 1. Lower Range Pixel Deviatiion ([0,Inf])
% 2. Higher Range Pixel Deviation ([0,Inf])
% 3. Lower Range change of Euler Number/100([0,Inf))
% 4. Higher Range change of Euler Number/100([0,Inf))
% 5. Lower Range Density Deviation ([0,Inf])
% 6. Higher Range Density Deviation ([0,Inf])
% 7. No. of Pixels/MaxPixels [0,Inf)
% 8. Height/MaxHeight (1,Inf)
% 9. Width/MaxWidth (1,Inf)
% 10. Solidity [0,1]
% 11. Euler/100 [0,Inf)
% 12 Eccentricity [0,1]
% 13. Extent [0,1]
% 14. SVT [0,1]
% 15. eHOG [0,1]
Features = zeros(1,15);
%% CODE
show_error = false; %% CHANGE TO TRUE TO SHOW ERRORS
[row,col,NUM_BIN_IMAGES] = size(BinImages);
cc_no = 1;
ccs_en = 1;
palate = zeros(row,col);
if getFinalBinImages
FinalBinImages = zeros(size(BinImages));
else
FinalBinImages = zeros(1,1); % NA IF FINAL BIN IMAGES NOT WANTED
end
q_offset = 0;
for i = 1:numel(BinSizes) %Must change Loop for change in Bin
if i~=1 && StabilityCheckMatrix(q_offset+1,1) ~= 0 && StabilityCheckMatrix(max(1,q_offset-2*main_offset+1),1) == 0
fprintf('GAP:: i = %d has a checking bin but before that does not',i);
continue;
end
main_offset = ceil(MAX_DISTANCE/BinSizes(i));
k = ceil((BinSizes(i)/2)) -1;
if StabilityCheckMatrix(q_offset+1,1) ~= 0 && StabilityCheckMatrix(max(1,q_offset-2*main_offset),1) == 0
fprintf('ALL IMAGES UNDER same I not equal');
continue;
end
%For 1st Level Bins
for img_no = (q_offset+1):(q_offset+main_offset)
close all
% fprintf('Processing Bin No.: %d\n',img_no);
if img_no ~= (q_offset+main_offset)
scan_img = logical(BinImages(:,:,img_no)+BinImages(:,:,(img_no+main_offset)));
else
scan_img = logical(BinImages(:,:,img_no));
end
CC_scan_img = bwconncomp(scan_img);
if StabilityCheckMatrix(img_no,1) == 0
continue;
end
if StabilityCheckMatrix(img_no,1) ~= 0 && StabilityCheckMatrix(max(q_offset+1,img_no-1),1) == 0
fprintf('ALL IMAGES UNDER same I not equal');
continue;
end
if CC_scan_img.NumObjects == 0
continue;
end
% label_scan_img = bwlabel(scan_img);
CCs(ccs_en) = CC_scan_img;
ccs_en = ccs_en+1;
lower_overlap_bin_no = img_no + main_offset-1;
upper_overlap_bin_no = img_no+main_offset;
if((img_no > NUM_BIN_IMAGES) || ((img_no+main_offset)> NUM_BIN_IMAGES && img_no~=q_offset+main_offset))
fprintf('\nLoop Error,img_value >134;printing loop at Bin Index: %d main_offset = %d ,q_offset = %d\n' ,main_offset,q_offset);
end
%The Smaller range against which to check stability
if img_no > q_offset+1 && img_no<(q_offset+main_offset)
lower_range_check_img = logical(BinImages(:,:,img_no)+BinImages(:,:,lower_overlap_bin_no) + BinImages(:,:,upper_overlap_bin_no));
else
if img_no == q_offset+main_offset
lower_range_check_img = logical(BinImages(:,:,img_no)+ BinImages(:,:,lower_overlap_bin_no));
else
lower_range_check_img = logical(BinImages(:,:,img_no)+BinImages(:,:,upper_overlap_bin_no));
end
end
lower_range_check_CC = bwconncomp(lower_range_check_img);
upper_range_check_img_no_1= StabilityCheckMatrix(img_no,6);
upper_range_check_img_no_2= StabilityCheckMatrix(img_no,7);
upper_range_check_img = logical(BinImages(:,:,upper_range_check_img_no_1)+BinImages(:,:,upper_range_check_img_no_2));
upper_range_check_CC = bwconncomp(upper_range_check_img);
lower_range_bwimage = bwlabel(lower_range_check_img);
upper_range_bwimage = bwlabel(upper_range_check_img);
stats = regionprops(CC_scan_img,'EulerNumber','Solidity','BoundingBox', 'Eccentricity', 'Extent');
lower_check_stats = regionprops(lower_range_check_img,'EulerNumber','Solidity');
upper_check_stats = regionprops(upper_range_check_img,'EulerNumber','Solidity');
for comp = 1:CC_scan_img.NumObjects
FeatureValues(:,:) = 0;
lower_range_overlap_comp = findLabels(lower_range_bwimage(CC_scan_img.PixelIdxList{comp}),2); %Make to 1 for speed
upper_range_overlap_comp = findLabels(upper_range_bwimage(CC_scan_img.PixelIdxList{comp}),2);
if (lower_range_overlap_comp(1,2) ~= 0 || lower_range_overlap_comp(1,1) == 0)
if show_error
fprintf('\n Wrong Calculation in LOWER Range Check Image');
figure('Name','ERROR:No Overlap Component !! K-means Component being scanned');
error_figure(CC_scan_img.PixelIdxList{comp}) = 1;
imshow(error_figure);
figure('Name','ERROR:Overlap Component !! The Lower Range Overlap Image');
error_figure(:,:) = 0;
imshow(lower_range_check_img);
end
continue;
end
if (upper_range_overlap_comp(1,2) ~= 0 || upper_range_overlap_comp(1,1) == 0 )
if show_error
fprintf('\n Wrong Calculation in LOWER Range Check Image');
figure('Name','ERROR:No Overlap Component!! K-means Component being scanned');
error_figure(CC_scan_img.PixelIdxList{comp}) = 1;
imshow(error_figure);
figure('Name','ERROR:No Overlap Component!! The Upper Range Overlap Image');
error_figure(:,:) = 0;
imshow(upper_range_check_img);
end
continue;
end
comp_num_of_pixels = numel(CC_scan_img.PixelIdxList{comp});
upper_range_comp_no_pixels = numel(upper_range_check_CC.PixelIdxList{upper_range_overlap_comp(1,1)});
lower_range_comp_no_pixels = numel(lower_range_check_CC.PixelIdxList{lower_range_overlap_comp(1,1)});
if show_error && (comp_num_of_pixels > lower_range_comp_no_pixels)
fprintf('\nError in Finding Correct LOWER Range Component: Size of Overlap reduced ');
figure('Name','ERROR: Size Reduction!! K-means Component being scanned');
error_figure(CC_scan_img.PixelIdxList{comp}) = 1;
imshow(error_figure);
figure('Name','ERROR:Size Reduction!! The Lower Range Overlap Image');
error_figure(:,:) = 0;
imshow(lower_range_check_img);
figure('Name','ERROR:Size Redution!! The Lower Range Overlap Component');
error_figure(lower_range_check_CC.PixelIdxList{lower_range_overlap_comp(1,1)}) = 1;
imshow(error_figure);
error_figure(:,:) = 0;
continue;
end
if show_error && (comp_num_of_pixels > upper_range_comp_no_pixels)
fprintf('\nError in Finding Correct UPPER Range Component: Size of Overlap reduced ');
figure('Name','ERROR: Size Reduction!! K-means Component being scanned');
error_figure(CC_scan_img.PixelIdxList{comp}) = 1;
imshow(error_figure);
figure('Name','ERROR:Size Reduction!! The Upper Range Overlap Image');
error_figure(:,:) = 0;
imshow(upper_range_check_img);
figure('Name','ERROR:Size Redution!! The Upper Range Overlap Component');
error_figure(upper_range_check_CC.PixelIdxList{upper_range_overlap_comp(1,1)}) = 1;
imshow(error_figure);
error_figure(:,:) = 0;
continue;
end
FeatureValues(1,1) = (abs(numel(lower_range_check_CC.PixelIdxList{lower_range_overlap_comp(1,1)}) - numel(CC_scan_img.PixelIdxList{comp})))/numel(CC_scan_img.PixelIdxList{comp});
FeatureValues(1,2) = (abs(numel(upper_range_check_CC.PixelIdxList{upper_range_overlap_comp(1,1)}) - numel(CC_scan_img.PixelIdxList{comp})))/numel(CC_scan_img.PixelIdxList{comp});
FeatureValues(1,3) = (lower_check_stats(lower_range_overlap_comp(1,1)).EulerNumber - stats(comp).EulerNumber)/100;
FeatureValues(1,4) = (upper_check_stats(upper_range_overlap_comp(1,1)).EulerNumber - stats(comp).EulerNumber)/100;
FeatureValues(1,5) = (abs(lower_check_stats(lower_range_overlap_comp(1,1)).Solidity - stats(comp).Solidity))/ stats(comp).Solidity;
FeatureValues(1,6) = (abs(upper_check_stats(upper_range_overlap_comp(1,1)).Solidity - stats(comp).Solidity))/ stats(comp).Solidity;
FeatureValues(1,7) = numel(CC_scan_img.PixelIdxList{comp})/(row*col);
FeatureValues(1,8) = stats(comp).BoundingBox(4)/row;
FeatureValues(1,9) = stats(comp).BoundingBox(3)/col;
palate(CC_scan_img.PixelIdxList{comp}) = 1;
FeatureValues(1,10:15) = [ stats(comp).Solidity (stats(comp).EulerNumber)/100 stats(comp).Eccentricity stats(comp).Extent SWT(palate) eHOG(palate) ];
palate(:,:) = 0;
Features(cc_no,:) = FeatureValues;
try
CCstats(cc_no) = stats(comp);
catch
fprintf('\nimg_en: %d\n',cc_no);
end
cc_no = cc_no + 1;
end
if getFinalBinImages
FinalBinImages(:,:,img_no) = scan_img;
end
end
% For 2nd Level Bins
for img_no = (q_offset+main_offset+1):(q_offset+2*main_offset-1)
% fprintf("Processing Bin No: %d\n",img_no);
close all
scan_img = logical(BinImages(:,:,img_no)+BinImages(:,:,(img_no-main_offset+1)));
CC_scan_img = bwconncomp(scan_img);
if StabilityCheckMatrix(img_no,1) == 0
continue;
end
if StabilityCheckMatrix(img_no,1) ~= 0 && StabilityCheckMatrix(max(q_offset+main_offset+1,img_no-1),1) == 0
fprintf('ALL IMAGES UNDER same I not equal');
continue;
end
if CC_scan_img.NumObjects == 0
continue;
end
% label_scan_img = bwlabel(scan_img);
CCs(ccs_en) = CC_scan_img;
ccs_en = ccs_en+1;
%The Smaller range against which to check stability
lower_overlap_bin_no = img_no -main_offset;
upper_overlap_bin_no = img_no-main_offset+1;
if((img_no > NUM_BIN_IMAGES) || (lower_overlap_bin_no> NUM_BIN_IMAGES))
fprintf('\nLoop Error,img_value >134;printing loop at Bin Index: %d main_offset = %d ,q_offset = %d\n' ,main_offset,q_offset);
end
lower_range_check_img = logical(BinImages(:,:,img_no)+BinImages(:,:,lower_overlap_bin_no) + BinImages(:,:,upper_overlap_bin_no));
lower_range_check_CC = bwconncomp(lower_range_check_img);
upper_range_check_img_no_1= StabilityCheckMatrix(img_no,6);
upper_range_check_img_no_2= StabilityCheckMatrix(img_no,7);
upper_range_check_img = logical(BinImages(:,:,upper_range_check_img_no_1)+BinImages(:,:,upper_range_check_img_no_2));
upper_range_check_CC = bwconncomp(upper_range_check_img);
lower_range_bwimage = bwlabel(lower_range_check_img);
upper_range_bwimage = bwlabel(upper_range_check_img);
stats = regionprops(CC_scan_img,'EulerNumber','Solidity','BoundingBox', 'Eccentricity', 'Extent');
lower_check_stats = regionprops(lower_range_check_img,'EulerNumber','Solidity');
upper_check_stats = regionprops(upper_range_check_img,'EulerNumber','Solidity');
for comp = 1:CC_scan_img.NumObjects
lower_range_overlap_comp = findLabels(lower_range_bwimage(CC_scan_img.PixelIdxList{comp}),2); %Make to 1 for speed
upper_range_overlap_comp = findLabels(upper_range_bwimage(CC_scan_img.PixelIdxList{comp}),2);
if (lower_range_overlap_comp(1,2) ~= 0 || lower_range_overlap_comp(1,1) == 0 )
if show_error
fprintf('\n Wrong Calculation in LOWER Range Check Image');
figure('Name','ERROR:No Overlap Component !! K-means Component being scanned');
error_figure(CC_scan_img.PixelIdxList{comp}) = 1;
imshow(error_figure);
figure('Name','ERROR:Overlap Component !! The Lower Range Overlap Image');
error_figure(:,:) = 0;
imshow(lower_range_check_img);
end
continue;
end
if (upper_range_overlap_comp(1,2) ~= 0 || upper_range_overlap_comp(1,1) == 0 )
if show_error
fprintf('\n Wrong Calculation in LOWER Range Check Image');
figure('Name','ERROR:No Overlap Component!! K-means Component being scanned');
error_figure(CC_scan_img.PixelIdxList{comp}) = 1;
imshow(error_figure);
figure('Name','ERROR:No Overlap Component!! The Upper Range Overlap Image');
error_figure(:,:) = 0;
imshow(upper_range_check_img);
end
continue;
end
comp_num_of_pixels = numel(CC_scan_img.PixelIdxList{comp});
upper_range_comp_no_pixels = numel(upper_range_check_CC.PixelIdxList{upper_range_overlap_comp(1,1)});
lower_range_comp_no_pixels = numel(lower_range_check_CC.PixelIdxList{lower_range_overlap_comp(1,1)});
if show_error && (comp_num_of_pixels > lower_range_comp_no_pixels)
fprintf('\nError in Finding Correct LOWER Range Component: Size of Overlap reduced ');
figure('Name','ERROR: Size Reduction!! K-means Component being scanned');
error_figure(CC_scan_img.PixelIdxList{comp}) = 1;
imshow(error_figure);
figure('Name','ERROR:Size Reduction!! The Lower Range Overlap Image');
error_figure(:,:) = 0;
imshow(lower_range_check_img);
figure('Name','ERROR:Size Redution!! The Lower Range Overlap Component');
error_figure(lower_range_check_CC.PixelIdxList{lower_range_overlap_comp(1,1)}) = 1;
imshow(error_figure);
error_figure(:,:) = 0;
continue;
end
if show_error && (comp_num_of_pixels > upper_range_comp_no_pixels)
fprintf('\nError in Finding Correct UPPER Range Component: Size of Overlap reduced ');
figure('Name','ERROR: Size Reduction!! K-means Component being scanned');
error_figure(CC_scan_img.PixelIdxList{comp}) = 1;
imshow(error_figure);
figure('Name','ERROR:Size Reduction!! The Upper Range Overlap Image');
error_figure(:,:) = 0;
imshow(upper_range_check_img);
figure('Name','ERROR:Size Redution!! The Upper Range Overlap Component');
error_figure(upper_range_check_CC.PixelIdxList{upper_range_overlap_comp(1,1)}) = 1;
imshow(error_figure);
error_figure(:,:) = 0;
continue;
end
FeatureValues(1,1) = (abs(numel(lower_range_check_CC.PixelIdxList{lower_range_overlap_comp(1,1)}) - numel(CC_scan_img.PixelIdxList{comp})))/numel(CC_scan_img.PixelIdxList{comp});
FeatureValues(1,2) = (abs(numel(upper_range_check_CC.PixelIdxList{upper_range_overlap_comp(1,1)}) - numel(CC_scan_img.PixelIdxList{comp})))/numel(CC_scan_img.PixelIdxList{comp});
FeatureValues(1,3) = (lower_check_stats(lower_range_overlap_comp(1,1)).EulerNumber - stats(comp).EulerNumber)/100;
FeatureValues(1,4) = (upper_check_stats(upper_range_overlap_comp(1,1)).EulerNumber - stats(comp).EulerNumber)/100;
FeatureValues(1,5) = (abs(lower_check_stats(lower_range_overlap_comp(1,1)).Solidity - stats(comp).Solidity))/ stats(comp).Solidity;
FeatureValues(1,6) = (abs(upper_check_stats(upper_range_overlap_comp(1,1)).Solidity - stats(comp).Solidity))/ stats(comp).Solidity;
FeatureValues(1,7) = numel(CC_scan_img.PixelIdxList{comp})/(row*col);
FeatureValues(1,8) = stats(comp).BoundingBox(4)/row;
FeatureValues(1,9) = stats(comp).BoundingBox(3)/col;
palate(CC_scan_img.PixelIdxList{comp}) = 1;
FeatureValues(1,10:15) = [ stats(comp).Solidity (stats(comp).EulerNumber)/100 stats(comp).Eccentricity stats(comp).Extent SWT(palate) eHOG(palate) ];
palate(:,:) = 0;
Features(cc_no,:) = FeatureValues;
try
CCstats(cc_no) = stats(comp);
catch
fprintf('\nimg_en: %d\n',cc_no);
end
cc_no = cc_no + 1;
end
if getFinalBinImages
FinalBinImages(:,:,img_no) = scan_img;
end
end
q_offset = q_offset+2*main_offset-1;
end
end