-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdetect_class.m
257 lines (204 loc) · 7.18 KB
/
detect_class.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
% run('../vlfeat-0.9.20/toolbox/vl_setup')
imageDir = './';
imageList = dir(sprintf('%s/*.jpg',imageDir));
nImages = length(imageList);
nImages = 1;
load('mysvm.mat');
bboxes = zeros(0,4);
confidences = zeros(0,1);
image_names = cell(0,1);
cellSize = 6;
dim = 36;
bboxes_final = [];
confidences_final = [];
image_names_final = [];
r_0 = 0;
scales = [0.5 1 1.5];
scales = fliplr(scales)';
num_scales = numel(scales);
for i=1:nImages
number_of_boxes = [];
temp_boxes = [];
temp_confs = [];
temp_names = [];
feat_dims = zeros(num_scales,2);
% load and show the image
im = im2single(rgb2gray(imread('class.jpg')));
imshow(im);
pause;
hold on;
[im_h, im_w, im_c] = size(im);
multiscale_confs = -Inf .* ones(((ceil(im_h/cellSize))-5)*((ceil(im_w/cellSize))-5),num_scales);
[multiscale_confs,feat_dims] = msConfs(im,scales,cellSize,w,b);
% get the most confident predictions for this scale
[~,inds] = sort(multiscale_confs(:,:),'descend');
num_inds = size(multiscale_confs,1);
if num_inds > 20,
inds = inds(1:20,:);
num_inds = 20;
end
number_of_boxes = [number_of_boxes; num_inds];
%For each scale, take top 20 boxes, scale them appropriately, add them
%to the bounding box list
i_scales = [];
for j=1:num_scales
scale = scales(j);
for n=1:num_inds
[row,col] = ind2sub([feat_dims(j,1) feat_dims(j,2)],inds(n,j));
bbox = [ (col*cellSize)./scale...
(row*cellSize)./scale ...
((col+cellSize-1)*cellSize)./scale ...
((row+cellSize-1)*cellSize)./scale];
confs = reshape(multiscale_confs(1:feat_dims(j,1)*feat_dims(j,2),j),feat_dims(j,:));
conf = confs(row,col);
image_name = {imageList(i).name};
plot_rectangle = [bbox(1), bbox(2); ...
bbox(1), bbox(4); ...
bbox(3), bbox(4); ...
bbox(3), bbox(2); ...
bbox(1), bbox(2)];
temp_boxes = [temp_boxes; bbox];
temp_confs = [temp_confs; conf];
temp_names = [temp_names; image_name];
bboxes = [bboxes; bbox];
confidences = [confidences; conf];
image_names = [image_names; image_name];
i_scales = [i_scales; scale];
end
end
(bboxes(:,4)-bboxes(:,2)).*(bboxes(:,3)-bboxes(:,1));
rb = temp_boxes;
rc = temp_confs;
rs = i_scales;
r_0 = r_0+1;
bboxes_temp = [];
confidences_temp = [];
image_names_temp = [];
ks = [];
[X,Y,Z] = nonMaxSupp(rb,rc,rs);
for index=1:size(X,1),
plot_rectangle = [X(index,1), X(index,2); ...
X(index,1), X(index,4); ...
X(index,3), X(index,4); ...
X(index,3), X(index,2); ...
X(index,1), X(index,2)];
plot(plot_rectangle(:,1), plot_rectangle(:,2), 'g-');
end
pause;
clf;
bboxes_final = [bboxes_final ; bboxes_temp];
confidences_final = [confidences_final ; confidences_temp];
image_names_final = [image_names_final ; image_names_temp];
fprintf('got preds for image %d/%d\n', i,nImages);
end
function X = arrayHelper(boxes,confidences,scales),
if isempty(boxes),
X = [];
return;
end
%Take first box
box_i = boxes(1,:);
boxes(1,:) = [];
confidences_temp = confidences(1);
confidences(1) = [];
scales_temp = scales(1);
scales(1) = [];
indices = 1;
bboxes_temp = box_i;
bi=[max(box_i(1),boxes(:,1)) max(box_i(2),boxes(:,2)) min(box_i(3),boxes(:,3)) min(box_i(4),boxes(:,4))];
iw = bi(:,3) - bi(:,1)+1;
ih = bi(:,4) - bi(:,2)+1;
box_i_area = (box_i(3) - box_i(1))*(box_i(4)-box_i(2));
i_area = (iw .* ih);
boxes_area = (boxes(:,3)-boxes(:,1)).*(boxes(:,4)-boxes(:,2));
u_area = box_i_area + boxes_area - i_area;
b_inds = find((i_area./u_area) > 0.01);
bboxes_temp = [bboxes_temp;boxes(b_inds,:)];
boxes(b_inds,:) = [];
confidences_temp = [confidences_temp;confidences(b_inds)];
confidences(b_inds) = [];
scales_temp = [scales_temp;scales(b_inds)];
scales(b_inds) = [];
largest_index = find(confidences_temp == max(confidences_temp));
X = [bboxes_temp(largest_index,:) ; arrayHelper(boxes,confidences,scales)];
end
function [X,Y,Z] = nonMaxSupp(boxes,confidences,scales)
if isempty(boxes),
X = [];
Y=[];
Z = [];
return;
end
nBoxes = size(boxes,1);
finalBoxes = [];
finalConfs = [];
finalScales = [];
tempBoxes = [];
tempConfs = [];
tempScales = [];
for i=1:nBoxes,
if isempty(boxes),
break;
end
box_a = boxes(1,:);
boxes(1,:) = [];
tempBoxes = box_a;
ba_area = (box_a(3)-box_a(1)+1)*(box_a(4)-box_a(2)+1);
conf_a = confidences(1);
confidences(1) = [];
tempConfs = conf_a;
scale_a = scales(1);
scales(1) = [];
tempScales = scale_a;
%Check if first box intersects with any others
bi = [max(box_a(1),boxes(:,1)) max(box_a(2),boxes(:,2)) min(box_a(3),boxes(:,3)) min(box_a(4),boxes(:,4))];
iw = bi(:,3) - bi(:,1) + 1;
ih = bi(:,4) - bi(:,2) + 1;
i_area = iw .* ih;
u_area = ba_area + ((boxes(:,3)-boxes(:,1)+1).*(boxes(:,4)-boxes(:,2) + 1)) - i_area;
%Find the boxes that intersect, remove them from the box array, put
%them into an array with the original box for non_max suppression.
i_indices = find((i_area./u_area)>0.05);
i_boxes = [box_a; boxes(i_indices,:)];
boxes(i_indices,:) = [];
i_confs = [conf_a; confidences(i_indices)];
confidences(i_indices) = [];
i_scales = [scale_a; scales(i_indices)];
scales(i_indices) = [];
max_ind = find(i_confs == max(i_confs(:)));
addedBox = i_boxes(max_ind,:);
addedConf = i_confs(max_ind);
addedScale = i_scales(max_ind);
finalBoxes = [finalBoxes; addedBox];
finalConfs = [finalConfs; addedConf];
finalScales = [finalScales; addedScale];
end
X = finalBoxes;
Y = finalConfs;
Z = finalScales;
end
function [MSC,D] = msConfs(im,scales,cellSize,w,b),
nScales = numel(scales);
[im_h im_w im_c] = size(im);
D = zeros(nScales,2);
MSC = -Inf .* ones(ceil(im_h/cellSize)*ceil(im_w/cellSize),nScales);
for j=1:nScales,
scale = scales(j);
im2 = imresize(im,[im_h * scale im_w * scale]);
im2 = (im2 - mean(im2(:)))/std(im2(:));
feats = vl_hog(im2,cellSize);
[rows,cols,~] = size(feats);
confs = zeros(rows,cols);
D(j,:) = [rows cols];
for r=1:rows-5
for c=1:cols-5
% create feature vector for the current window and classify it using the SVM model,
im_wind = feats(r:r+5,c:c+5,:);
im_wind = im_wind(:);
score = w'*im_wind + b;
confs(r,c) = score;
end
end
MSC(1:size(confs,1)*size(confs,2),j)=confs(:);
end
end