www.pudn.com > MATLAB.zip > PLSE_demo1.m, change:2006-08-16,size:3163b

%  **********   Do not distribute this file  ***********
% This Matlab program implements the level set method in Li et al's paper
%    "Level Set Evolution Without Re-initialization: A New Variational Formulation"
%    in Proceedings of CVPR'05, vol. 1, pp. 430-436.
% Copyright (c) 2004--2007 by Chunming Li
% Author: Chunming Li
% Revision by Chunming Li 4/25/2006
% e-mail: li_chunming@hotmail.com
% http://vuiis.vanderbilt.edu/~licm/

% clear all;
% close all;
% % Img = imread('twocells.bmp');  % The same cell image in the paper is used here
% Img=double(Img(:,:,1));
% Img = Img + 127;
% sigma=1;    % scale parameter in Gaussian kernel.
% G=fspecial('gaussian',15,sigma);
% Img_smooth=conv2(Img,G,'same');  % smooth image by Gaussiin convolution
% imshow(uint8(Img_smooth));
% figure;
% f=Ix.^2+Iy.^2;

fp = fopen('nominal1.txt', 'r');
m = fscanf(fp,'%d,%d');
fscanf(fp,';');
n = fscanf(fp,'%f,');
rm1 = reshape(n,m(2),m(1));
rm = rm1;
rm1 = rm';
rm1 = rm1(:,m(2):-1:1);
Img = rm1';

fclose(fp);
[X Y] = meshgrid(0:size(Img,1)-1,0:size(Img,2)-1);
[K,H,P1,P2] = surfature(X,Y,Img);
Img = H;

% Ix = diff(Img_smooth,2,1);
% Iy = diff(Img_smooth,2,2);
% Ix=Ix(:,2:end-1);
% Iy=Iy(2:end-1,:);
% f=(Ix.^2+Iy.^2)*8;
%
% Img_smooth=Img_smooth(2:end-1,2:end-1);
% Img=Img(2:end-1,2:end-1);
% g = max(max(f))-f;

% Ix = diff(Img_smooth,3,1);
% Iy = diff(Img_smooth,3,2);
% Ix=Ix(:,2:end-2);
% Iy=Iy(2:end-2,:);
% f=(Ix.^2+Iy.^2)*8;
% Img=Img(2:end-2,2:end-2);

f=(Ix.^2+Iy.^2);
ff = mat2gray(f);
imshow(ff);
g=1./(1+f);  % edge indicator function.
% g=1./(1+H);
gg = mat2gray(g);
imshow(gg);

lambda=150; % coefficient of the weighted length term L(\phi)
mu=0.04;  % coefficient of the internal (penalizing) energy term P(\phi)
alf=10;  % coefficient of the weighted area term A(\phi)
epsilon=1.5; % the papramater in the definition of smooth Dirac function
timestep=5;  % time step

% define initial level set function (LSF) as -c, 0, c at points outside, on
% the boundary, and inside of a region R, respectively.
[nrow, ncol]=size(Img);
c=10;
initialLSF=c*ones(nrow,ncol);
d=8;
initialLSF(d+1:end-d, d+1:end-d)=0;  % zero level set on the boundary of R
initialLSF(d+2:end-d-1, d+2: end-d-1)=-c; % negative constant inside of R
u=initialLSF;
figure;imagesc(Img);colormap(gray);hold on;
[c,h] = contour(u,[0 0],'r','LineWidth',2);

% pause(3);

title('Initial contour');
% start level set evolution
for n=1:880
u=EVOLUTION_PLSE(u, g ,lambda, mu, alf, epsilon, timestep, 1);
pause(0.02);
if mod(n,20)==0
imagesc(Img);colormap(gray);hold on;
axis equal; axis off;
[c,h] = contour(u,[0 0],'g','LineWidth',3);
iterNum=[num2str(n), ' iterations'];
title(iterNum);
hold off;
end
end
imagesc(Img);colormap(gray);hold on;
[c,h] = contour(u,[0 0],'g','LineWidth',4);
totalIterNum=[num2str(n), ' iterations'];
title(['Final contour, ', totalIterNum]);