-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTrainer.m
347 lines (325 loc) · 14.7 KB
/
Trainer.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
classdef Trainer < matlab.System
% Trainer Add summary here
% Pre-computed constants
properties(Constant)
imgSize = [320,320];
objSize = [40,40];
defBlockSize = [8 8];
defNoiseThreshold = 120*120;
end
methods(Access = public)
function enhancedBinaryImg = imenhance(self, rawImgPath, noiseThreshold)
% image read
img = imread(rawImgPath);
% resize to fixed w & h
%img = imresize(img, self.imgSize);
% Convert to Gray image
if size(img,3)==3 %RGB image
img = rgb2gray(img);
end
% threshold or convert to binary image
img = imbinarize(img);
img = ~img; %negative
% Remove all object containing fewer than noiseThreshold pixels
if nargin < 3
noiseThreshold = self.defNoiseThreshold;
end
enhancedBinaryImg = bwareaopen(img, noiseThreshold);
end
function [imgObjects, rectPositions] = extractObjects(self, enhancedBinaryImg)
%Segment all object in the image based on 8 connectivity
objects = bwconncomp(enhancedBinaryImg,8);
%foreach extracted object
%initialize imgObjects as cell array
imgObjects = cell(objects.NumObjects, 1);
rectPositions = cell(objects.NumObjects, 1);
parfor obj=1:objects.NumObjects % Taha -TM
%get colored pixels indexes column
coloredPixelsIdx = objects.PixelIdxList(1,obj);
%create a black image with the same enhancedBinaryImg size
objImg = false(objects.ImageSize);
%draw the white obj pixel by pixel
for i=1:numel(coloredPixelsIdx)
%color the specified pixel by index
objImg(coloredPixelsIdx{i}) = 1;
end
%imgObjects{obj} = objImg;
%get the BoundingBox for the objImg
s = regionprops(objImg, 'BoundingBox');
%get the bounding rectangle
rect = s.BoundingBox;
%crop & resize the extracted object then add to imgObjects array
imgObjects{obj} = imresize(imcrop(objImg, rect), self.objSize);
rectPositions{obj} = rect;
%debug
%imshow(imgObjects{obj});
end
end
function Centroid = getCentroid(~, imgObject)
[m,n] = size(imgObject);
X_hist=sum(imgObject,1);
Y_hist=sum(imgObject,2);
X=1:n; Y=1:m;
if sum(X_hist) == 0
centX = 0;
else
centX=sum(X.*X_hist)/sum(X_hist);
end
if sum(Y_hist) == 0
centY = 0;
else
centY=sum(Y'.*Y_hist)/sum(Y_hist);
end
Centroid = [centX centY];
%get centroid pixel index
%roundedX = round(centX);
%roundedY = round(centY);
% create helper indexer matrix
%idxerMat = reshape(1:m*n, [m n]);
%Centroid = idxerMat(roundedX, roundedY);
end
function Medoid = getMedoid(~, imgObject)
imgObject = double(imgObject);
% Get logical medoid matrix
logical_med = imgObject==median(imgObject(:));
% find medoids indexes [doubles]
med_indexes = find(logical_med);
if numel(med_indexes) == 0
logical_med = ~logical_med;
med_indexes = find(logical_med);
end
% find medoids indexes [(X,Y) pairs]
[X,Y] = find(logical_med);
% Get median value of med_indexes
med_val = med_indexes(round(numel(med_indexes)/2));
% Get median index of med_indexes
med_index = find(med_indexes==med_val);
medX = X(med_index);
medY = Y(med_index);
Medoid = [medX, medY];
%[~, medX] = self.extractMedoidRow(imgObject');
%[~, medY] = self.extractMedoidRow(imgObject);
%Medoid = [medX medY];
%get medoid pixel index
%[m,n] = size(imgObject);
% create helper indexer matrix
%idxerMat = reshape(1:m*n, [m n]);
%Medoid = round(idxerMat(medX, medY)/2);
end
function Perimeter = getPerimeter(~, imgObject)
I=zeros(size(imgObject));
I(2:end-1,2:end-1)=1;
Perimeter = sum(reshape(imgObject.*I,1,[]));
end
function Area = getArea(~, imgObject)
Area = 0;
for i=1:numel(imgObject)
if(imgObject(i))
Area = Area + 1;
end
end
end
function [dataSet, dataSetClasses, rectPositions] = Train(self, dataClasses, imagePaths2D, noiseThreshold, blockSize)
dataSetClasses = cell(0,1);
rectPositions = cell(0,1);
dataSet_Initialized = 0;
for classIdx = 1 : numel(dataClasses)
classImgsPaths = imagePaths2D{classIdx};
for classImgPathIdx = 1 : numel(classImgsPaths)
curImgPath = classImgsPaths{classImgPathIdx};
if nargin < 4
enhancedBinImg = self.imenhance(curImgPath);
else
enhancedBinImg = self.imenhance(curImgPath, noiseThreshold);
end
[imgObjs, imgObjsPositions] = self.extractObjects(enhancedBinImg);
rectPositions = vertcat(rectPositions, imgObjsPositions);
for objIdx = 1 : numel(imgObjs)
curObj = imgObjs{objIdx};
if nargin < 5
curObjSegms = self.segment(curObj);
else
curObjSegms = self.segment(curObj, blockSize);
end
numOfFeatures = 11;
if ~dataSet_Initialized %initialize for first time only
dataSet = zeros(0, numel(curObjSegms)*numOfFeatures);
dataSet_Initialized = 1;
end
colRange = 1:numOfFeatures;
[m,~] = size(dataSet);
%foreach object segment
for segIdx = 1:numel(curObjSegms)
curObjSegm = curObjSegms{segIdx};
featureVector = zeros(1, numOfFeatures);
% get all features & add to featureVector
featureVector(1,1:2) = self.getCentroid(curObjSegm);
featureVector(1,3:4) = self.getMedoid(curObjSegm);
featureVector(1,5) = self.getPerimeter(curObjSegm);
featureVector(1,6) = self.getArea(curObjSegm);
s = regionprops(curObjSegm,'Euler');
try
featureVector(1,7) = s.EulerNumber;
catch
featureVector(1,7) = 0;
end
s = regionprops(curObjSegm,'Extent');
try
featureVector(1,8) = s.Extent;
catch
featureVector(1,8) = 0;
end
s = regionprops(curObjSegm,'MajorAxisLength');
try
featureVector(1,9) = s.MajorAxisLength;
catch
featureVector(1,9) = 0;
end
s = regionprops(curObjSegm,'MinorAxisLength');
try
featureVector(1,10) = s.MinorAxisLength;
catch
featureVector(1,10) = 0;
end
s = regionprops(curObjSegm,'Orientation');
try
featureVector(1,11) = s.Orientation;
catch
featureVector(1,11) = 0;
end
% add featureVector to dataSet
if max(featureVector)
featureVector = abs(featureVector);
featureVector = featureVector/max(featureVector); %normalize
end
dataSet(m+1, colRange) = featureVector;
colRange = colRange + numOfFeatures;
end
dataSetClasses{end+1,:} = dataClasses{classIdx};
end
end
end
end
function imgSegments = segment(self, img, blockSize)
[imgX,imgY] = size(img);
if nargin < 3
blockSize = self.defBlockSize;
end
blockX = blockSize(1);
blockY = blockSize(2);
imgSegments = cell(0, 1);
if blockX <= imgX && blockY <= imgY
x1 = 1; x2 = blockX;
while x2 <= imgX
y1 = 1; y2 = blockY;
while y2 <= imgY
imgSegments{end+1} = img(x1:x2, y1:y2);
y1 = y2+1;
y2 = y2+blockY;
if y1 <= imgY && y2 > imgY
y1 = y1-(y2-imgY);
y2 = imgY;
end
end
x1 = x2+1;
x2 = x2+blockX;
if x1 <= imgX && x2 > imgX
x1 = x1-(x2-imgX);
x2 = imgX;
end
end
else
imgSegments{1} = img;
end
end
function [dataSet, dataSetClasses, rectPositions] = TrainHOG(self, dataClasses, imagePaths2D, noiseThreshold, CellSize)
dataSetClasses = cell(0,1);
rectPositions = cell(0,1);
dataSet_Initialized = 0;
for classIdx = 1 : numel(dataClasses)
classImgsPaths = imagePaths2D{classIdx};
for classImgPathIdx = 1 : numel(classImgsPaths)
curImgPath = classImgsPaths{classImgPathIdx};
if nargin < 4
enhancedBinImg = self.imenhance(curImgPath);
else
enhancedBinImg = self.imenhance(curImgPath, noiseThreshold);
end
[imgObjs, imgObjsPositions] = self.extractObjects(enhancedBinImg);
rectPositions = vertcat(rectPositions, imgObjsPositions);
for objIdx = 1 : numel(imgObjs)
curObj = imgObjs{objIdx};
if nargin < 5
CellSize = self.defBlockSize;
end
hogFeatures = extractHOGFeatures(curObj,'CellSize', CellSize);
if ~dataSet_Initialized %initialize for first time only
dataSet = zeros(0, numel(hogFeatures));
dataSet_Initialized = 1;
end
dataSet(end+1,:) = hogFeatures;
dataSetClasses{end+1,:} = dataClasses{classIdx};
end
end
end
end
function [dataSet, dataSetClasses, rectPositions] = TrainAsync(self, dataClasses, imagePaths2D, noiseThreshold, blockSize, isHOG)
switch nargin
case 5
isHOG = 1;
case 4
isHOG = 1;
blockSize = self.defBlockSize;
case 3
isHOG = 1;
blockSize = self.defBlockSize;
noiseThreshold = self.defNoiseThreshold;
end
if isHOG
parfor classIdx = 1:numel(dataClasses)
[dataSetCell{classIdx,1}, dataSetClassesCell{classIdx,1}, rectPositionsCell{classIdx,1}] = self.TrainHOG({dataClasses{classIdx}}, {imagePaths2D{classIdx}}, noiseThreshold, blockSize);
end
else
parfor classIdx = 1:numel(dataClasses)
[dataSetCell{classIdx,1}, dataSetClassesCell{classIdx,1}, rectPositionsCell{classIdx,1}] = self.Train({dataClasses{classIdx}}, {imagePaths2D{classIdx}}, noiseThreshold, blockSize);
end
end
%Copy cell arrays to standard arrays
dataSet = zeros(0, numel(dataSetCell{1,1}(1,:)));
dataSetClasses = cell(0, 1);
rectPositions = cell(0, 1);
parfor classIdx = 1:numel(dataClasses)
dataSet = vertcat(dataSet, dataSetCell{classIdx,1});
dataSetClasses = vertcat(dataSetClasses, dataSetClassesCell{classIdx,1});
rectPositions = vertcat(rectPositions, rectPositionsCell{classIdx,1});
end
end
end
methods(Access = private)
function [MedoidVal, MedoidIdx] = extractMedoidRow(~, imgObject)
[m,n] = size(imgObject);
MedoidVal = zeros(1,n);
lastRowDistance = 0;
for r = 1:m
rowDistance = 0;
for c = 1:n
curEl = imgObject(r,c);
for internalRow = 1:m
rowDistance = rowDistance + abs(curEl - imgObject(internalRow, c));
end
end
if r == 1
MedoidVal = imgObject(1,:); % extract the first row
MedoidIdx = 1;
lastRowDistance = rowDistance; % save the last row distance
else
if rowDistance < lastRowDistance
MedoidVal = imgObject(r,:); % extract the r's row
MedoidIdx = r;
lastRowDistance = rowDistance;
end
end
end
end
end
end