-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.m
80 lines (62 loc) · 2.34 KB
/
detect.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
function [x,y,score] = detection(I,template,ndet)
%
% return top ndet detections found by applying template to the given image.
% x,y should contain the coordinates of the detections in the image
% score should contain the scores of the detections
%
% compute the feature map for the image
f = hog(I);
nori = size(f,3);
% cross-correlate template with feature map to get a total response
R = zeros(size(f,1),size(f,2));
for i = 1:nori
R = R + imfilter(f(:,:,i), template(:,:,i), 'replicate');
end
% now return locations of the top ndet detections
% sort responses from high to low
[val,ind] = sort(R(:),'descend');
% work down the list of responses, removing overlapping detections as we go
i = 1;
detcount = 0;
x = zeros(ndet,1);
y = zeros(ndet,1);
score = zeros(ndet,1);
while ((detcount < ndet) && (i <= length(ind)))
% convert ind(i) back to (i,j) values to get coordinates of the block
% [yblock,xblock] = ind2sub([size(f,1), size(f,2)],ind(i));
[yblock,xblock] = ind2sub([size(f,1), size(f,2)],transpose(ind(i)));
assert(val(i)==R(yblock,xblock)); %make sure we did the indexing correctly
% now convert yblock,xblock to pixel coordinates
ypixel = yblock*8;
xpixel = xblock*8;
% check if this detection overlaps any detections which we've already added to the list
% you should do this by comparing the x,y coordinates of the new candidate detection to all
% the detections previously added to the list and check if the distance between the
% detections is less than 70% of the template width/height
overlap = ismember(xpixel, x) && ismember(ypixel, y);
if i ~= 1
for k = 1:size(x)
if (abs(xpixel-x(k)) < 5.6*size(template,2)) || (abs(ypixel-y(k)) < 5.6*size(template,1))
overlap = true; % non-maxima suppresion
break
end
end
end
% if not, then add this detection location and score to the list we return
if (~overlap)
detcount = detcount+1;
x(detcount) = xpixel;
y(detcount) = ypixel;
score(detcount) = R(yblock, xblock);
end
i = i + 1;
end
% the while loop may terminate before we find the desired number
% of detections... in that case you should shrink the vectors
% x,y,score down to the correct size
x = x(1:detcount);
y = y(1:detcount);
score = score(1:detcount);
assert(length(x)==detcount);
assert(length(y)==detcount);
assert(length(score)==detcount);