Daniel@0: function [overlap, normoverlap] = rectintSparse(A,B) Daniel@0: % Daniel@0: % A(i,:) = [x y w h] Daniel@0: % B(j,:) = [x y w h] Daniel@0: % overlap(i,j) = area of intersection Daniel@0: % normoverla(i,j) Daniel@0: % Daniel@0: % Same as built-in rectint, but uses less memory. Daniel@0: % Use rectintSparseC for a faster version. Daniel@0: % Daniel@0: Daniel@0: leftA = A(:,1); Daniel@0: bottomA = A(:,2); Daniel@0: rightA = leftA + A(:,3); Daniel@0: topA = bottomA + A(:,4); Daniel@0: Daniel@0: leftB = B(:,1)'; Daniel@0: bottomB = B(:,2)'; Daniel@0: rightB = leftB + B(:,3)'; Daniel@0: topB = bottomB + B(:,4)'; Daniel@0: Daniel@0: numRectA = size(A,1); Daniel@0: numRectB = size(B,1); Daniel@0: Daniel@0: %out = rectintSparseLoopC(leftA, rightA, topA, bottomA, leftB, rightB, topB, bottomB); Daniel@0: Daniel@0: nnz = ceil(0.2*numRectA*numRectB); % guess of number of non-zeroes Daniel@0: overlap = sparse([], [], [], numRectA, numRectB, nnz); Daniel@0: normoverlap = sparse([], [], [], numRectA, numRectB, nnz); Daniel@0: for j=1:numRectB Daniel@0: for i=1:numRectA Daniel@0: tmp = (max(0, min(rightA(i), rightB(j)) - max(leftA(i), leftB(j)) ) ) .* ... Daniel@0: (max(0, min(topA(i), topB(j)) - max(bottomA(i), bottomB(j)) ) ); Daniel@0: if tmp>0 Daniel@0: overlap(i,j) = tmp; Daniel@0: areaA = (rightA(i)-leftA(i))*(topA(i)-bottomA(i)); Daniel@0: areaB = (rightB(j)-leftB(j))*(topB(j)-bottomB(j)); Daniel@0: normoverlap(i,j) = min(tmp/areaA, tmp/areaB); Daniel@0: end Daniel@0: %fprintf('j=%d, i=%d, overlap=%5.3f, norm=%5.3f\n',... Daniel@0: % j, i, overlap(i,j), normoverlap(i,j)); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: Daniel@0: if 0 Daniel@0: N = size(bboxDense01,2); % 1000; Daniel@0: rect = bboxToRect(bboxDense01)'; Daniel@0: A = rect(1:2,:); Daniel@0: B = rect(1:N,:); Daniel@0: Daniel@0: tic; out1 = rectint(A, B); toc Daniel@0: tic; out2 = rectintSparse(A, B); toc Daniel@0: tic; out3 = rectintSparseC(A, B); toc Daniel@0: tic; out4 = rectintC(A, B); toc Daniel@0: assert(approxeq(out1, out2)) Daniel@0: assert(approxeq(out1, full(out3))) Daniel@0: assert(approxeq(out1, out4)) Daniel@0: end