function activeContoursNoEdges(ima, con) warning off; % Read input image. u0=imread(ima); % Convert image to grayscale if necessary. if size(size(u0),2) == 3 u0=rgb2gray(u0); end u0=double(u0)/255; u0=imresize(u0,0.5); % % For the weak gradient image. % % Parameters of the method. % mu=0; % lambda1=1; % lambda2=1; % v=0; % ite=1000; % epsilon=2; % dt=5; % Parameters of the method. mu=0.01; lambda1=1; lambda2=1; v=0; ite=1000; epsilon=2; dt=1; % Figure that will hold the contour. figContour=figure; set(figContour,'Position',[200 350 150 150],'Color',[1 1 1],'Visible','off'); % Figure that will hold the corresponding segmentation. figSeg=figure; set(figSeg,'Position',[650 350 150 150],'Color',[1 1 1],'Visible','off'); if nargin > 1 && con==1 % Allow marking the original contour. figure; set(figContour,'Position',[200 150 150 150],'Color',[1 1 1]); imshow(u0,'InitialMagnification',300); [x,y]=ginput(2); x=min(max(x,1),size(u0,2)); y=min(max(y,1),size(u0,1)); phi=-ones(size(u0)); phi(y(1,1) : y(2,1), x(1,1) : x(2,1))=1; close; else % Setting the initial contour as a small square in the center. phi=-ones(size(u0)); phi(size(u0,1)/4+4 : end-size(u0,1)/4-3, size(u0,2)/4+4 : end-size(u0,2)/4-3)=1; end % Derivative operators gx,gy. gx=fspecial('prewitt')/6; gy=gx'; for t=1:ite % Computing H2eps(phi). H2eps=0.5*(1+(2/pi)*atan(phi/epsilon)); % Computing delta phi. deltaPhiX=imfilter(H2eps,gx,'replicate'); deltaPhiY=imfilter(H2eps,gy,'replicate'); deltaPhi=sqrt((deltaPhiX.^2 + deltaPhiY.^2)); % Computing c1 and c2 c1=mean( u0(phi>=0) ); c2=mean( u0(phi<0) ); % Computing |del phi|. [delPhiX,delPhiY]=gradient(phi); normDelPhi=sqrt(delPhiX.^2+delPhiY.^2)+eps; % Compute the gradient of phi and then the divergent. [gradPhiX,gradPhiY]=gradient(phi); divX=imfilter(gradPhiX./normDelPhi,gx,'replicate'); divY=imfilter(gradPhiY./normDelPhi,gy,'replicate'); div=divX+divY; % Compute the time increment. dphiT=dt.*deltaPhi.*(mu*div-v-lambda1*(u0-c1).^2+lambda2*(u0-c2).^2); % Integrate phi. phi=phi+dphiT; % Display phi's derivative. displayContour(phi, deltaPhi, u0, figContour, figSeg, t); end %figure(f2); %imshow(phi,'InitialMagnification',300); %imshow(); title('Active contours without edges, Chan and Vese') end function displayContour(phi, deltaPhi, u0, figContour, figSeg, t) % Plot the image iteration by iteration. rgbDeltaPhi=zeros([size(deltaPhi),3]); rgbDeltaPhi(:,:,1)=(deltaPhi>0.075); rgbU0=zeros([size(u0),3]); rgbU0(:,:,1)=u0; rgbU0(:,:,2)=u0; rgbU0(:,:,3)=u0; figure(figContour); imshow(rgbDeltaPhi+rgbU0,'InitialMagnification',300); text(5,5,['Ite: ',num2str(t)],'Color', 'b','FontWeight','bold','FontSize',12); figure(figSeg); imshow(phi,'InitialMagnification',300) text(5,5,['Ite: ',num2str(t)],'Color', 'b','FontWeight','bold','FontSize',12); end