annotate _FullBNT/KPMtools/bipartiteMatchingIntProg.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 [a,ass] = bipartiteMatchingIntProg(dst, nmatches)
matthiasm@8 2 % BIPARTITEMATCHINGINTPROG Use binary integer programming (linear objective) to solve for optimal linear assignment
matthiasm@8 3 % function a = bipartiteMatchingIntProg(dst)
matthiasm@8 4 % a(i) = best matching column for row i
matthiasm@8 5 %
matthiasm@8 6 % This gives the same result as bipartiteMatchingHungarian.
matthiasm@8 7 %
matthiasm@8 8 % function a = bibpartiteMatchingIntProg(dst, nmatches)
matthiasm@8 9 % only matches the specified number (must be <= min(size(dst))).
matthiasm@8 10 % This can be used to allow outliers in both source and target.
matthiasm@8 11 %
matthiasm@8 12 % For details, see Marciel & Costeira, "A global solution to sparse correspondence
matthiasm@8 13 % problems", PAMI 25(2), 2003
matthiasm@8 14
matthiasm@8 15 if nargin < 2, nmatches = []; end
matthiasm@8 16
matthiasm@8 17 [p1 p2] = size(dst);
matthiasm@8 18 p1orig = p1; p2orig = p2;
matthiasm@8 19 dstorig = dst;
matthiasm@8 20
matthiasm@8 21 if isempty(nmatches) % no outliers allowed (modulo size difference)
matthiasm@8 22 % ensure matrix is square
matthiasm@8 23 m = max(dst(:));
matthiasm@8 24 if p1<p2
matthiasm@8 25 dst = [dst; m*ones(p2-p1, p2)];
matthiasm@8 26 elseif p1>p2
matthiasm@8 27 dst = [dst m*ones(p1, p1-p2)];
matthiasm@8 28 end
matthiasm@8 29 end
matthiasm@8 30 [p1 p2] = size(dst);
matthiasm@8 31
matthiasm@8 32
matthiasm@8 33 c = dst(:); % vectorize cost matrix
matthiasm@8 34
matthiasm@8 35 % row-sum: ensure each column sums to 1
matthiasm@8 36 A2 = kron(eye(p2), ones(1,p1));
matthiasm@8 37 b2 = ones(p2,1);
matthiasm@8 38
matthiasm@8 39 % col-sum: ensure each row sums to 1
matthiasm@8 40 A3 = kron(ones(1,p2), eye(p1));
matthiasm@8 41 b3 = ones(p1,1);
matthiasm@8 42
matthiasm@8 43 if isempty(nmatches)
matthiasm@8 44 % enforce doubly stochastic
matthiasm@8 45 A = [A2; A3];
matthiasm@8 46 b = [b2; b3];
matthiasm@8 47 Aineq = zeros(1, p1*p2);
matthiasm@8 48 bineq = 0;
matthiasm@8 49 else
matthiasm@8 50 nmatches = min([nmatches, p1, p2]);
matthiasm@8 51 Aineq = [A2; A3];
matthiasm@8 52 bineq = [b2; b3]; % row and col sums <= 1
matthiasm@8 53 A = ones(1,p1*p2);
matthiasm@8 54 b = nmatches; % total num matches = b (otherwise get degenerate soln)
matthiasm@8 55 end
matthiasm@8 56
matthiasm@8 57
matthiasm@8 58 ass = bintprog(c, Aineq, bineq, A, b);
matthiasm@8 59 ass = reshape(ass, p1, p2);
matthiasm@8 60
matthiasm@8 61 a = zeros(1, p1orig);
matthiasm@8 62 for i=1:p1orig
matthiasm@8 63 ndx = find(ass(i,:)==1);
matthiasm@8 64 if ~isempty(ndx) & (ndx <= p2orig)
matthiasm@8 65 a(i) = ndx;
matthiasm@8 66 end
matthiasm@8 67 end
matthiasm@8 68
matthiasm@8 69