clear all;
close all;
Img = imread('50.bmp'); % The same cell image in the paper is used here
Img=double(Img(:,:,1));
sigma=1.5; % scale parameter in Gaussian kernel for smoothing.
G=fspecial('gaussian',15,sigma)
Img_smooth=conv2(Img,G,'same');
[Ix,Iy]=gradient(Img_smooth);
f=Ix.^2+Iy.^2;
g=1./(1+f); % 边缘指标函数edge indicator function.
epsilon=1.5; % the papramater in the definition of smoothed Dirac functio
timestep=5; % time step
mu=0.2/timestep; % coefficient of the internal (penalizing) energy term P(\phi)
% Note: the product timestep*mu must be less than 0.25 for stability!
lambda=5; % coefficient of the weighted length term Lg(\phi)
alf=1.5; % coefficient of the weighted area term Ag(\phi);
% Note: Choose a positive(negative) alf if the initial contour is outside(inside) the object.
% define initial level set function (LSF) as -c0, 0, c0 at points outside, on the boundary, and inside of a region R, respectively.
[nrow, ncol]=size(Img);
c0=4;
initialLSF=c0*ones(nrow,ncol)
w=10;
initialLSF(w+1:end-w, w+1:end-w)=0; % zero level set is on the boundary of R.
% Note: this can be commented out. The intial LSF does NOT necessarily need a zero level set.
initialLSF(w+2:end-w-1, w+2: end-w-1)=-c0; % negative constant -c0 inside of R, postive constant c0 outside of R
u=initialLSF;
figure;imagesc(Img);colormap(gray);hold on;
[c,h] = contour(u,[0 0],'r');
title('Initial contour');
% start level set evolution
for n=1:500
u=EVOLUTION(u, g ,lambda, mu, alf, epsilon, timestep, 1);
if mod(n,20)==0
pause(0.001);
imagesc(Img);colormap(gray);hold on;
[c,h] = contour(u,[0 0],'r');
iterNum=[num2str(n), ' iterations'];
title(iterNum);
hold off;
end
end
imagesc(Img);colormap(gray);hold on;
[c,h] = contour(u,[0 0],'r');
totalIterNum=[num2str(n), ' iterations'];
title(['Final contour, ', totalIterNum]);
如上面是一段基于水平及的图像分割的代码,具体的水平集分割那块算法写在EVOLUTION函数中了,没有贴上来。
小弟刚接这块,代码大致能看懂吧,但是数学方面很捉急。想请教各位大哥大姐几个问题:
1.Ix.^2+Iy.^2 这个是说的x与y轴的梯度矩阵平方和吧,这样求出来的值他的数学意义是什么呢?后面那句话的g又是求的什么呢?
2.想问mu lambda alf这三个系数定义的是什么呢?英文字面意思能懂,但是不知道具体说的是什么。phi又是什么呢
3.代码整体出来的效果是:读取图像,然后在图像上面有一个红框,应该是这句[c,h] = contour(u,[0 0],'r');画的等高线。 它是根据u这个矩阵画的,u矩阵从外往里是4作为外围,然后一个1的框,里面包着-4,然后我想请教,C0=4和w=10数字是怎么来的呢,是纯粹作出等高线还是通过运算来的?
我想就是这几个问题了吧,希望可以得到解决,在此谢过。