forked from vedaldi/practical-object-category-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boxinclusion.m
55 lines (42 loc) · 1.37 KB
/
boxinclusion.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
function dist = calcBoxInclusion(A, B, varargin)
% GETBOXOVERLAP
% A and B have a box for each column, in the format [xmin ymin xmax
% ymax]. The resulting matrix dist has A's boxes along the rows
% and B's boxes along the columns and contains the percentage of
% the area of each box B contained in the box A.
%
% Author:: Andrea Vedaldi
% AUTORIGHTS
% Copyright (C) 2008-09 Andrea Vedaldi
%
% This file is part of VGG MKL classification and detection code,
% available in the terms of the GNU General Public License version 2.
opts.pascalFormat = false ;
opts = vl_argparse(opts, varargin) ;
m = size(A,2) ;
n = size(B,2) ;
O = [] ;
if m==0 || n==0, dist = zeros(m,n) ; return ; end
om = ones(1,m) ;
on = ones(1,n) ;
if opts.pascalFormat
A(3:4,:) = A(3:4,:) + 1 ;
B(3:4,:) = B(3:4,:) + 1 ;
end
% find length Ox of the overlap range [x1, x2] along x
% x1 cannot be smaller than A.xmin B.xmin
% x2 cannot be larger than A.xmax B.xmax
% Ox is x2 - x1 or 0
x1 = max(A(1*on,:)', B(1*om,:)) ;
x2 = min(A(3*on,:)', B(3*om,:)) ;
Ox = max(x2 - x1, 0) ;
y1 = max(A(2*on,:)', B(2*om,:)) ;
y2 = min(A(4*on,:)', B(4*om,:)) ;
Oy = max(y2 - y1, 0) ;
% are of the intersection
areaInt = Ox .* Oy ;
% area of the union is sum of areas - inersection
areaA = prod(A(3:4,:) - A(1:2,:)) ;
areaB = prod(B(3:4,:) - B(1:2,:)) ;
% final distance matrix
dist = areaInt ./ (areaB(om,:) + eps) ;