annotate _FullBNT/KPMtools/rectintSparse.m @ 9:4ea6619cb3f5 tip

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