Normalized Cuts and Image Segmentation

Normalized Cuts and Image Segmentation_第1张图片
    上图中每两个节点之间都有个权值,权值的值在代码中有给出。
%2014-9-23
%writed by Tianliang
%Ncut test
M = [6.1 3.5 5 6.3 3.7 3.6 2.6];
D = diag(M);
disp(D);
W = [0 1 1.5 3 0.5 0.1 0;
    1 0 1.5 1 0 0 0;
    1.5 1.5 0 2 0 0 0;
    3 1 2 0 0.2 0 0.1;
    0.5 0 0 0.2 0 2 1;
    0.1 0 0 0 2 0 1.5;
    0 0 0 0.1 1 1.5 0];
disp(W);
a=round(2*rand(10000,7)-1);
for i = 1:10000
    for j = 1:7
        if a(i,j)>0 
            a(i,j)=1;
        else
            a(i,j)=-1;
        end
    end
end
disp(D-W);
%x = [1 1 1 1 -1 -1 -1]';
%disp('1+x'),disp(1+x);
term = zeros(1,10000);
for i = 1:10000
    x=a(i,:)';
    D_sum = 0;
    for j = 1:7
        if x(j,1)==1
            D_sum = D_sum + D(j,j);
        end
    end
    k = D_sum/sum(sum(D));
    %disp('k = '),disp(k);
    term1 =((1+x)'*(D-W)*(1+x))/(k*ones(7,1)'*D*ones(7,1));
    term2 =((1-x)'*(D-W)*(1-x))/((1-k)*ones(7,1)'*D*ones(7,1));
    term(1,i) = term1 + term2;
    %disp('term = '),disp(term(i));
end
[term_min,term_coord]=min(term);
disp('rouping at minimum cut:'),disp(a(term_coord,:));
[V,D]=eig(D^(-(1/2))*(D-W)*(D^(-1/2)));
nbSegments = 2;
V=V(:,1:nbSegments);
[EigenvectorsDiscrete,EigenVectors]=discretisation(V);
EigenvectorsDiscrete=full(EigenvectorsDiscrete);
disp('EigenvectorsDiscrete = '),disp(EigenvectorsDiscrete');




  A B sum
A assoc(A,A) cut(A,B) assoc(A,V)
B cut(B,A) assoc(B,B) assoc(B,V)
sum assoc(A,V) assoc(B,V)  

论文中的matlab代码:http://download.csdn.net/detail/tianliangjay/7976791


Normalized Cuts and Image Segmentation_第2张图片

你可能感兴趣的:(NCut)