wolffd@0: % Consider matching sources to detections wolffd@0: wolffd@0: % s1 d2 wolffd@0: % s2 d3 wolffd@0: % d1 wolffd@0: wolffd@0: %a = bipartiteMatchingHungarian([52;0.01]) wolffd@0: wolffd@0: % sources(:,i) = [x y] coords wolffd@0: sources = [0.1 0.7; 0.6 0.4]'; wolffd@0: detections = [0.2 0.2; 0.2 0.8; 0.7 0.1]'; wolffd@0: dst = sqdist(sources, detections); wolffd@0: wolffd@0: % a = [2 3] which means s1-d2, s2-d3 wolffd@0: a = bipartiteMatchingHungarian(dst); wolffd@0: a2 = bipartiteMatchingIntProg(dst); wolffd@0: assert(isequal(a(:),a2(:))) wolffd@0: wolffd@0: wolffd@0: figure(1); clf wolffd@0: bipartiteMatchingDemoPlot(sources, detections, a) wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0: %%%% Flip roles of sources and detections wolffd@0: wolffd@0: %dst = dst'; wolffd@0: dst = sqdist(detections, sources); wolffd@0: % a = [0 1 2] which means d1-0, d2-s1, d3-s2 wolffd@0: a = bipartiteMatchingHungarian(dst); wolffd@0: wolffd@0: a2 = bipartiteMatchingIntProg(dst); wolffd@0: assert(isequal(a(:),a2(:))) wolffd@0: wolffd@0: figure(2); clf wolffd@0: bipartiteMatchingDemoPlot(detections, sources, a) % swapped args wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0: %%%%%%%%%% Move s1 nearer to d1 wolffd@0: % d2 wolffd@0: % s2 d3 wolffd@0: % s1 d1 wolffd@0: wolffd@0: sources = [0.1 0.3; 0.6 0.4]'; wolffd@0: detections = [0.2 0.2; 0.2 0.8; 0.7 0.1]'; wolffd@0: dst = sqdist(sources, detections); wolffd@0: wolffd@0: % a = [2 3] which means s1-d2, s2-d3 wolffd@0: a = bipartiteMatchingHungarian(dst); wolffd@0: [a2, ass] = bipartiteMatchingIntProg(dst); wolffd@0: assert(isequal(a(:),a2(:))) wolffd@0: wolffd@0: wolffd@0: figure(3); clf wolffd@0: bipartiteMatchingDemoPlot(sources, detections, a) wolffd@0: wolffd@0: wolffd@0: wolffd@0: %%%%%%%%%% wolffd@0: wolffd@0: % Use random points wolffd@0: wolffd@0: % Generate 2D data from a mixture of 2 Gaussians (from netlab demgmm1) wolffd@0: randn('state', 0); rand('state', 0); wolffd@0: gmix = gmm(2, 2, 'spherical'); wolffd@0: ndat1 = 10; ndat2 = 10; ndata = ndat1+ndat2; wolffd@0: %gmix.centres = [0.3 0.3; 0.7 0.7]; wolffd@0: %gmix.covars = [0.01 0.01]; wolffd@0: gmix.centres = [0.5 0.5; 0.5 0.5]; wolffd@0: gmix.covars = [0.1 0.01]; wolffd@0: [x, label] = gmmsamp(gmix, ndata); wolffd@0: wolffd@0: ndx = find(label==1); wolffd@0: sources = x(ndx,:)'; wolffd@0: ndx = find(label==2); wolffd@0: detections = x(ndx,:)'; wolffd@0: dst = sqdist(sources, detections); wolffd@0: wolffd@0: [a, ass] = bipartiteMatchingIntProg(dst); wolffd@0: [a2] = bipartiteMatchingHungarian(dst); wolffd@0: assert(isequal(a(:), a2(:))) wolffd@0: wolffd@0: figure(4); clf wolffd@0: bipartiteMatchingDemoPlot(sources, detections, a) wolffd@0: wolffd@0: % only match 80% of points wolffd@0: p1 = size(sources, 2); wolffd@0: p2 = size(detections, 2); wolffd@0: nmatch = ceil(0.8*min(p1,p2)); wolffd@0: a2 = bipartiteMatchingIntProg(dst, nmatch); wolffd@0: figure(5); clf wolffd@0: bipartiteMatchingDemoPlot(sources, detections, a2) wolffd@0: wolffd@0: wolffd@0: %%% swap roles wolffd@0: wolffd@0: ndx = find(label==2); wolffd@0: sources = x(ndx,:)'; wolffd@0: ndx = find(label==1); wolffd@0: detections = x(ndx,:)'; wolffd@0: dst = sqdist(sources, detections); wolffd@0: wolffd@0: % only match 80% of points wolffd@0: p1 = size(sources, 2); wolffd@0: p2 = size(detections, 2); wolffd@0: nmatch = ceil(0.8*min(p1,p2)); wolffd@0: a2 = bipartiteMatchingIntProg(dst, nmatch); wolffd@0: figure(6); clf wolffd@0: bipartiteMatchingDemoPlot(sources, detections, a2)