
%%-------------------- USES the SHI-MALIK APPROACH -- 

if (1) 
%%
%%http://matlabtricks.com/post-35/a-simple-image-segmetnation-example-in-matlab
image = imread('jump.jpg');     % read image

%% dowsample it

 X1 = scrop(image,30,120,50,0);
 X = dsample(X1,3,3);
 [m,n,p] = size(X);
 t = reshape(mean(mean(X)),3,1);
 sigmaI = norm(t)/4;
 sigmaX = sqrt(size(X,1)^2 + size(X,2)^2)/4.0;
 sigmaX = sigmaX/max(m,n);   %% because it is scaled in
                             %% makeRBFgraph 
else
    N = 45
    X = mir(N);
    sigmaX = 0.75*sqrt(N);
    %    sigmaX = 5.0;
    sigmaI = 0.1; 
end
[mm, nn, col] = size(X);
figure(1)
imagesc(X)
 X = double(X);

 [W, xi, yj] = makeRBFGraph(X,sigmaX, sigmaI,3);
 disp('makeRBFGraph out') 
 n = size(W,1);

 %%-------------------- scale 
     D = spdiags(W,0);
     D = 1.0 ./  sqrt(D);
     D1 = spdiags(D,0,n,n);
     W = D1*(W*D1);
     Nc = 5;
     neig = 3;
     % [V, lam] = Lan(W, neig+1, msteps, 'ascend');
     W = 0.5*(W+W');
     [V, D] = eigs(W, neig+1,'SA');
     lam = diag(D);
     disp('eigs out ..')
 
     [ctr, indx] = myKmeans(V(:,2:neig+1), Nc);
     disp('Kmeans out..')
 
     lab2 = reshape(indx,mm,nn);
     lab4 =fix(((lab2-1)/Nc)*255) ;

     figure(2)
     imagesc(lab4)
     
 %%-------------------- convert indices
 
 %%%return
