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