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