Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/KPMtools/rectintSparse.m @ 0:e9a9cd732c1e tip
first hg version after svn
author | wolffd |
---|---|
date | Tue, 10 Feb 2015 15:05:51 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e9a9cd732c1e |
---|---|
1 function [overlap, normoverlap] = rectintSparse(A,B) | |
2 % | |
3 % A(i,:) = [x y w h] | |
4 % B(j,:) = [x y w h] | |
5 % overlap(i,j) = area of intersection | |
6 % normoverla(i,j) | |
7 % | |
8 % Same as built-in rectint, but uses less memory. | |
9 % Use rectintSparseC for a faster version. | |
10 % | |
11 | |
12 leftA = A(:,1); | |
13 bottomA = A(:,2); | |
14 rightA = leftA + A(:,3); | |
15 topA = bottomA + A(:,4); | |
16 | |
17 leftB = B(:,1)'; | |
18 bottomB = B(:,2)'; | |
19 rightB = leftB + B(:,3)'; | |
20 topB = bottomB + B(:,4)'; | |
21 | |
22 numRectA = size(A,1); | |
23 numRectB = size(B,1); | |
24 | |
25 %out = rectintSparseLoopC(leftA, rightA, topA, bottomA, leftB, rightB, topB, bottomB); | |
26 | |
27 nnz = ceil(0.2*numRectA*numRectB); % guess of number of non-zeroes | |
28 overlap = sparse([], [], [], numRectA, numRectB, nnz); | |
29 normoverlap = sparse([], [], [], numRectA, numRectB, nnz); | |
30 for j=1:numRectB | |
31 for i=1:numRectA | |
32 tmp = (max(0, min(rightA(i), rightB(j)) - max(leftA(i), leftB(j)) ) ) .* ... | |
33 (max(0, min(topA(i), topB(j)) - max(bottomA(i), bottomB(j)) ) ); | |
34 if tmp>0 | |
35 overlap(i,j) = tmp; | |
36 areaA = (rightA(i)-leftA(i))*(topA(i)-bottomA(i)); | |
37 areaB = (rightB(j)-leftB(j))*(topB(j)-bottomB(j)); | |
38 normoverlap(i,j) = min(tmp/areaA, tmp/areaB); | |
39 end | |
40 %fprintf('j=%d, i=%d, overlap=%5.3f, norm=%5.3f\n',... | |
41 % j, i, overlap(i,j), normoverlap(i,j)); | |
42 end | |
43 end | |
44 | |
45 | |
46 if 0 | |
47 N = size(bboxDense01,2); % 1000; | |
48 rect = bboxToRect(bboxDense01)'; | |
49 A = rect(1:2,:); | |
50 B = rect(1:N,:); | |
51 | |
52 tic; out1 = rectint(A, B); toc | |
53 tic; out2 = rectintSparse(A, B); toc | |
54 tic; out3 = rectintSparseC(A, B); toc | |
55 tic; out4 = rectintC(A, B); toc | |
56 assert(approxeq(out1, out2)) | |
57 assert(approxeq(out1, full(out3))) | |
58 assert(approxeq(out1, out4)) | |
59 end |