|
基于随机游走算法的图像分割:
代码经过,提炼抽取,请参考百度网盘:链接:https://pan.baidu.com/s/1Se-Rd7ccDk4gzH4PkO4OLw 提取码:t09e- function [mask,probabilities] = random_walker(img,seeds,labels,beta)
- %
- %Available at: http://www.cns.bu.edu/~lgrady/grady2006random.pdf
- %
- %Note: Requires installation of the Graph Analysis Toolbox available at:
- %http://eslab.bu.edu/software/graphanalysis/
- %Find image size
- img=im2double(img);
- [X Y Z]=size(img);
- %Build graph
- [points edges]=lattice(X,Y);
- % save data.mat points edges
- %Generate weights and Laplacian matrix
- if(Z > 1) %Color images
- tmp=img(:,:,1);
- imgVals=tmp(:);
- tmp=img(:,:,2);
- imgVals(:,2)=tmp(:);
- tmp=img(:,:,3);
- imgVals(:,3)=tmp(:);
- else
- imgVals=img(:);
- end
- weights=makeweights(edges,imgVals,beta);
- L=laplacian(edges,weights);
- %L=laplacian(edges,weights,length(points),1);
- %Determine which label values have been used
- label_adjust=min(labels); labels=labels-label_adjust+1; %Adjust labels to be > 0
- labels_record(labels)=1;
- labels_present=find(labels_record);
- number_labels=length(labels_present);
- %Set up Dirichlet problem
- boundary=zeros(length(seeds),number_labels);
- for k=1:number_labels
- boundary(:,k)=(labels(:)==labels_present(k));
- end
- %Solve for random walker probabilities by solving combinatorial Dirichlet
- %problem
- probabilities=dirichletboundary(L,seeds(:),boundary);
- %Generate mask
- [dummy mask]=max(probabilities,[],2);
- mask=labels_present(mask)+label_adjust-1; %Assign original labels to mask
- mask=reshape(mask,[X Y]);
- probabilities=reshape(probabilities,[X Y number_labels]);
复制代码
|
|