annotate toolboxes/FullBNT-1.0.7/KPMtools/rectintSparse.m @ 0:cc4b1211e677 tip

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