www.pudn.com > 1_FCM_S.zip > simple.m, change:2015-10-18,size:1693b


function labels = simple(I,k,sigmac,sigmap,nsamp,n,t,niter)
% function labels = ncut_segment(I,sigmap,sigmac)
%
% This function returns a partition image for the given image I
% 此函数返回给定图像的分区图像
% It takes two parameters, 
%
%      k : Number of groups to extract  组数
% sigmac : color change sensitivity 颜色变化的灵敏度
% sigmap : proximity sensitivity 邻近灵敏度
%  nsamp : number of samples to be used 样本数目
%      n : number of iterations of kmeans km的迭代次数
%      t : k-means termination threshold km的终止阈值
%  niter : number of randomized restarts for k-means 随机重启kmean的次数

% how do you decide on nsamp 怎样定义样本点

[N1,N2,N3]=size(I);

if max(I(:)) > 1.0;
  cdata=reshape(double(I)/255,[N1*N2 N3]);
else
  cdata=reshape(double(I),[N1*N2 N3]);
end

[X,Y]=meshgrid(1:N2,1:N1);
pdata=[X(:) Y(:)];
N=N1*N2;
%nsamp= max(round(k*0.01*N),50);

ind_all=randperm(N);
ind_samp=ind_all(1:nsamp);
ind_rest=ind_all(nsamp+1:end);


d2Ac=pairdist(cdata(ind_samp,:),cdata(ind_samp,:),'L2');
d2Ap=pairdist(pdata(ind_samp,:),pdata(ind_samp,:),'L2');
d2Bc=pairdist(cdata(ind_samp,:),cdata(ind_rest,:),'L2');
d2Bp=pairdist(pdata(ind_samp,:),pdata(ind_rest,:),'L2');

% A=exp(-d2Ac.^2/(2*sigmac^2) -(d2Ap.^2/(2*sigmap^2))); % choose your sigma 
% B=exp(-d2Bc.^2/(2*sigmac^2) -d2Bp.^2/(2*sigmap^2));
A=exp(-d2Ac.^2/(2*sigmac^2)); % choose your sigma  选取邻近灵敏度
B=exp(-d2Bc.^2/(2*sigmac^2));
% the joys of nystrom
[V,ss]=nystrom_ncut(A,B);

% depermute the nsytrom result
V(ind_all,:)=V;
V1=V(:,1:k); % choose the k eigenvectors
Vnormalized=V1./repmat(sqrt(sum(V1.^2,2)),1,k);
[centers,labels,error]=kmwrapper(Vnormalized',k,n,t,niter);
labels=reshape(labels,[N1 N2]);