You are on page 1of 23

Active Contours Without Edges

and Image Segmentation


Ryo Takei, rrtakei@sfu.ca

Final Project, APMA 922

1 Introduction
The main goal of this report, in terms of application, is to solve the image segmentation
problem: given an image, we seek to detect the shape of the objects in it. This problem,
like many other image processing problems, has vast applications in the industry, especially
in the area of pattern recognition and motion capturing.
The solution of the image segmentation problem is best described by a (generally closed)
curve in the 2-D domain representing the boundary of the object(s). This naturally lead
one to consider the problem to be solve by curve evolution or active contours: starting
with an initial curve and evolving it to the ’correct’ steady state, ie. the object boundaries.
This has led to various ideas, such as marker-points, volume of fluid method, cell method
and, the level set method. The latter method will be described in the next section.
This report will consider one (recent) method of solving the image segmentation problem,
namely, the Active Contour Without Edges method. This method utilizes the level set
technique of curve treatment and more importantly, overcomes several difficulties arising
in previous methods of image segmentation. Most part of this report follows that of Chan
and Vese.

2 Background
2.1 Definitions and Problem Statement
An image u can be defined as a bounded real positive function on some (rectangular)
domain Ω ∈ <2 . Let its boundary be denoted ∂Ω. Typically, in image processing,
u ∈ {0, 1, . . . , 255}, and referred to as a greyscale image. We denote C : [0, 1] → Ω a
curve parametrized by arclength. We assume C is piecewise C 1 ([0, 1]).
Although the problem of image segmentation has various forms, we will consider the sim-
plest case where we “segment” the image into two region. The definition of the problem is
as follows:

1
Definition 1 (The image segmentation problem) Let u0 be an image in Ω. Suppose
Ω can be partitioned into Ω1 and Ω2 , where for some image property P , u0 |Ω1 differ from
u0 |Ω2 . Find a curve C ∈ Ω such that it partitions Ω = Ω1 ∪ Ω2 .
Here, P is typically the image intensity (the function value of u0 ), but can also be texture,
pattern curvatures, etc..
A conventional approach in solving image segmentation is to start with some initial guess
C(0) = C0 and evolve C(t) in a time dependent PDE
Ct = F(C(t))
such that C¯ = limt→∞ C(t) solves the problem. In other words, ideally, the model should
reach a steady state when C(t) is at the correct solution. See Figure 1.

Figure 1: Evolving C (in green) to the boundary of Ω1 and Ω2 .

It should be noted here that given an arbitrary image, there is not definite unique segmen-
tation solution to it. The “correct” segmentation solution will be dependent on the model,
and is somewhat subjective. The notion of “better” models, however, will be developed
later.

2.2 The Level-Set Method and Active Contour Methods


When evolving a curve C in <2 , the standard approach is to use the level-set method
(or, level-sets). The idea of level-sets was first discovered by Osher and Sethian in [2]; it
exploits the remarkable fact that treating a curve C(s) as a zero level curve of a surface
φ(x, y) simplifies various tasks when evolving C, such as changes in its topological structure
(See Figure 2). This is suited well for image processing PDE problems, since the image
itself can be considered as a surface u(x)1 with intensity values at position x.

1
Generally, the most applicable function space for images is the space of bounded variation, BV (Ω), a
banach space with norm ||u||BV (Ω) = ||u||L1 (Ω) + |Du|(Ω), where D is the generalized derivative.

2
Figure 2: A schematic diagram of how the level-set function and the curve C evolve. Note
how the topological changes are treated with ease.

The following is a basic result in level-sets:


Proposition 1 (Curve evolution for level-sets) Let C(t0 ) the zero level curve of φ(x, t0 )
at some time t = t0 . If x0 ∈ C(t0 ) and v(x0 , t0 ) denotes the outer normal velocity of C(t0 ),
then it solves the PDE,
φt = v(x, t)|∇φ|
The proof of this may be found in, for example, [3].
One widely used type of curve evolution is motion by mean curvature, when v(x) = κ(x),
the curvature of C at x:
∇φ
φt = κ(x)|∇φ| = |∇φ|∇ · ( )
|∇φ|
Prior to the main model described in this report, image segmentation problems were treated
in tandem with edge-detecting problems. Denote Ωb for the correct boundary between Ω1
and Ω2 and define an edge function g as, g(0) = 1, decreasing and lims→∞ g(s) = 0. An
example of such function is,
1
g(|∇u0 |) =
1 + |∇Jρ ∗ u0 |2
where Jρ ∗ u0 2 is some regularization of image u0 to a smoother function. See [4] for further
2
Commonly Jρ = Gρ , the gaussian curve with standard deviation ρ.

3
examples.
For images with a distinct boundary Ωb , ie.

|∇u(x0 )|  |∇u(x)| for x0 ∈ Ωb and x ∈ Ω − Ωb

it is plausible to evolve C until it “hits” points of large derivative. One such model is the
following geometric active contour PDE:
 
∇φ
φt = g(|∇u0 |)|∇φ| ∇ · ( )+ν , (1)
|∇φ|

where u0 is the original image, and g is an edge-function. Note its similarity to the motion
by mean curvature equation; the ν term, when large enough, enforces C to evolve inwards
even when κ = 0. The evolution should stop when C(t) = {x : g(|∇u0 (x)|) < ε} for some
ε small.

2.3 The Mumford-Shah Functional


Consider the functional of two functions, u (an image) and C (a curve):
Z Z
F MS 2
[u, C] = µ · length(C) + λ |u0 − u| dx + |∇u|2 dx. (2)
Ω Ω−C

where u0 is a (noised) image and µ > 0, λ > 0 parameters. F MS is known as the Mumford-
Shah functional. The result of the minimization,

inf u,C F MS [u, C] (3)

is an image u smooth in a partition of regions Ri with sharp boundaries C. Hence, it solves


the image denoising problem and the segmentation problem simultaneously (See Figure 3).
Now, if we consider the same minimization (3) with u restricted to the piecewise constant
function space we should obtain an image u with constant values ci in each Ri ; the ci ’s are
in fact the averages of u0 each region Ri . This reduced minimization problem is known as
the minimal partition problem.
It will be useful to understand how each term of (2) contribute to the minimization (3).
The first term forces C to be relatively smooth on the boundaries, while the third term
forces u to be smooth in areas not on C. The second term, the only term containing the
original image u0 , keep the fidelity of u with u0 .
In fact, the model introduced in this paper is a particular case of the minimal partition
problem, and hence well-related to the Mumford-Shah functional.

4
Figure 3: Left: original image (with initial curve C0 ). Middle: Minimizing curve C in the
Mumford-Shah functional. RIght: Minimizing function u.

3 The Active Contour Without Edges Model


3.1 Idea of the Model
The active contour without edges model exploits the following simple observation. Given
an image u0 and a closed curve C, let c1 , c2 be the average value of u0 in the “inside” of C
and “outside” of C, respectively. Define the functionals Fi to be,
Z Z
2
F1 (C) = |u0 − c1 | dx, and F2 (C) = |u0 − c2 |2 dx.
inside C outside C
¯ each of
With the assumption that u0 consists of two regions Ω1 and Ω2 with boundary C,
nearly constant intensities, it is clear that, uniquely,
¯ + F2 (C)
inf {F1 (C) + F2 (C)} = F1 (C) ¯ =0
C

See Figure 4.
With additional minimizing terms we have the the model:

Definition 2 (Active contour without edges) Let, p ≥ 2, and

F (C) = µ · (length(C))p + ν · area(inside(C)) + λ1 F1 (C) + λ2 F2 (C) (4)

If C¯ solves
¯ = inf F (C),
F (C)
C

then C¯ is a solution to the segmentation problem.

The F in the definition above, the first term accounts for smoothing C and the second
term forces C to move inward if ν is large. Note that this model is a particular case of

5
Figure 4: Values of F1 and F2 for values curves C.

the minimal partition problem where we constrain the minimizing function u to be the
piecewise constant function,

average(u0 ) inside C
u=
average(u0 ) outside C,

if ν = 0. The proof of the existence of the minimizing C is presented in Chan and Vese [1].

3.2 Level-set Formulation


Introduce a level-set function φ such that C = {x ∈ Ω : φ(x) = 0}. Define the inside and
outside of C by

inside(C) = {x ∈ Ω : φ(x) > 0}, and outside(C) = {x ∈ Ω : φ(x) < 0}.

Recall the definition of the heaviside function H(s) to be,



1, if s ≥ 0
H(s) =
0, if s < 0

and δ(s), the Dirac delta function, to be the distributional derivative of H(s)3 .
Noting that,
Z  Z 
length(C) = |∇H(φ(x))| = δ(φ)|∇φ| ,
Ω Ω
Z
area(inside(C)) = H(φ(x))dx,

3
With this in mind, we will simply write ∇H(x) = δ(x)

6
we can rewrite (4) as,
Z Z Z Z
F (φ) = µ( |∇H(φ)|)p +ν H(φ)dx+λ1 |u0 −c1 |2 H(φ)dx+λ2 |u0 −c2 |2 (1−H(φ))dx,
Ω Ω Ω Ω
R R R R (5)
where c1 = Ω u0 H(φ)dx/ Ω H(φ)dx and c2 = Ω u0 (1 − H(φ))dx/ Ω (1 − H(φ))dx.

3.3 Functional Minimization


As stated in the model definition, we seek to minimize (5). From variational calculus, the
minimization of a functional asks for the Euler-Lagrange equation to be solved. Note that,
in processing with solving the Euler-Lagrange equation, the heaviside and delta functions
must be regularized to Hε and δε so that ∇Hε (x) = δε (x) in the strict sense of the
derivative. In this sense, the regularized version of the functional (5) becomes,
Z Z Z Z
p 2
Fε (φ) = µ( δε |∇φ|) +ν Hε (φ)dx+λ1 |u0 −c1 | Hε (φ)dx+λ2 |u0 −c2 |2 (1−Hε (φ))dx.
Ω Ω Ω Ω

Substituting into the Euler-Lagrange equation, and applying Green’s identity and Green’s
theorem (see [1] for details) we arrive with the differential equation:

∇φ
Z
δε (φ)[µp( δε (φ)|∇φ|)p−1 ∇ · ( ) − ν − λ1 (u0 − c1 )2 + λ2 (u0 − c2 )2 ] = 0, (6)
Ω |∇φ|
∂φ
on Ω with boundary conditions ∂n = 0 on ∂Ω, where c1 and c2 were defined earlier.
The numerical scheme evolves C, the zero level-set of φ, by letting φ be a function of also
time, t, and the right hand side of (6) be ∂φ∂t . If a steady state of this PDE is reached,
φ must satisfy (6), and hence minimize the functional (5). The zero level-set of φ at the
steady state is the curve C which solves the segmentation problem.

4 Numerics
4.1 Implicit and Explicit Schemes
Assume the image, u0 , to be segmented be square. Let Ω be the unit square [0, 1] × [0, 1].
Discretize into N × N interior mesh points, with mesh spacing h = 1/(N + 1). Let
(xi , yj ) = (ih, jh) and φni,j be the approximation to φ(n∆t, xi , yj ), where ∆t is the time
step.
As developed in the previous section, the goal is to evolve the following time dependent
PDE to steady state:

7
" Z p−1 #
∂φ ∇φ
= δε (φ) µp δε (φ)|∇φ| ∇·( ) − ν − λ1 (u0 − c1 )2 + λ2 (u0 − c2 )2 in Ω
∂t Ω |∇φ|
R R
u 0 H ε (φ)dx u0 (1 − Hε (φ))dx
c1 (φ) = Ω R , c2 (φ) = ΩR (7)
Ω Hε (φ)dx Ω (1 − Hε (φ))dx
φ(0, x) = φ0 (x) in Ω, at t = 0
∂φ
= 0 in ∂Ω
∂n
The main difficulty in the implementation is the first term of the PDE, the modified motion
by mean curvature term.
First define
∆x+ φi,j = φi+1,j − φi,j , ∆x− φi,j = φi,j − φi−1,j
and similarly for ∆y+ and ∆y− . The forward Euler implicit scheme presented in [1] is the
following:
  
φn+1
i,j − φn
i,j µ ∆ x φn+1
+ i,j
= δh (φni,j )  2 (pL(φn )p−1 )∆x−  q 
∆t h (∆+ φi,j ) /h + (φni,j+1 − φni,j−1 )2 /(2h)2
x n 2 2
 
y n+1
µ ∆ φ
+ i,j
+ 2 (pL(φn ))p−1 ∆y−  q 
h y n 2 2 n n 2
(∆+ φi,j ) /h + (φi+1,j − φi−1,j ) /(2h) 2
#
−ν − λ1 (u0,i,j − c1 (φn ))2 + λ2 (u0,i,j − c2 (φn ))2

The advantage of the implicit method is the unconditional stability with respect to the
time step size ∆t. However, it requires to solve a system of linear equations at every time
step tn . This system is solved via the Jacobi iterative method.
The explicit method can be also be implemented by simply replacing φn+1 i,j on the right
hand side of the implicit method by φni,j . The unconditional stability no longer holds, but
there are no linear systems to be solved, which decreases the computation time at each
time step.
However, in practice, the explicit method is less susceptible to flattening or to have large
gradients present at φn+1 from a reinitialized φn (see next section). This makes the explicit
method more suitable for certain applications.

4.2 Reinitialization
At each time step, when φn+1 n n
i,j is computed from φi,j , due to the δh (φ ) term in the scheme
causes sharp gradients in φ. The presence of such “shocks” may cause difficulty in the

8
computation as described in [5]. (This, in general, happens for the implicit method, while
rare for explicit methods.) This is resolved by flattening, or reinitializing φ into a dis-
tance function (See Figure 5). A distance function ψ of φ can be defined as follows: 1.
ψ(x) = 0 ⇔ φ(x) = 0, ie. both zero level-sets are equal, 2. ψ(x) = sgn(φ(x))dist(x, C),
where dist(x, C) denotes the distance between x and its closest point on the zero level-set
of φ.
Finding the distance function ψ from φn can be done by evolving the following time de-
pendent PDE to steady state [5]:

ψt = sign(φn )(1 − |∇ψ|)


ψ(0, x) = φn (x)

The numerical scheme in solving this is as follows. Let,


k+1 k
ψi,j = ψi,j − ∆t(sign(φn ))G(ψi,j
k
)

where G(·) is defined as,


 p
 pmax((a+ )2 , (b− )2 ) + max((c+ )2 , (d− )2 )/h − 1, if φn (xij ) > 0
G(ψi,j ) = max((a− )2 , (b+ )2 ) + max((c− )2 , (d+ )2 )/h − 1, if φn (xij ) < 0

0, otherwise

and a+ = max(∆x− ψi,j /h, 0), b− = max(∆x+ ψi,j , 0)/h, c+ = max(∆y− ψi,j /h, 0), d+ = max(∆y+ ψi,j , 0), a− =
min(∆x− ψi,j /h, 0), and so on.

Figure 5: Left: Image with C. Middle: The corresponding level-set function. Note that the
height is of order 10−4 . Right: The distance function, with the zero level-set. Note that
the zero level-set does not change. The height is approximately 0.3.

In general, the bulk of the computation time is spend during this reinitialization procedure.

9
4.3 Regularization of H(s) and δ(s)
The regularizations for H and δ, namely Hε and δε , respectively, used for computation is
the following:
 
1 2 s
Hε (s) = 1 + arctan( )
2 π ε
1 ε
δε (s) =
π ε2 + s2
Note that δε = Hε0 . This formulation is better than the [−ε, ε]-compactly supported ver-
sions of the regularization, since it reduces the chance of division by zero for the implicit
scheme. See Figure 6.

Figure 6: Left: Hε (s). Right: δε (s).

4.4 Effect of Parameters


There are four parameters involved in the minimization functional: µ, ν, λ1 and λ2 . The
effect of each parameter with respect to the steady state is as follows:

• µ controls the importance of the length of C in the minimization. This means that
if µ  1, then a few large closed curves will retain in the steady state, compared to
many small ones. This may be useful in ignoring noise, or grouping objects of similar
characteristics (“chromatic resemblance”[1]).

• ν controls the the importance of area inside C. If ν  1, like the geometric active
contour model (1), it forces C to move strictly inward. Also, the speed at which C
evolves inward increases.

• The relative balance between λ1 and


R λ2 determines which side, inside or outside, has
higher importance in minimizing Ωi |u0 − ci |dx. This is useful in segmenting blurred

10
images: for example, if one wishes to completely enclose the blurred object, λ2 > λ1
will ensure this.

All of these properties will be verified in the numerical results.

4.5 Note on Convergence


Both the implicit and the explicit schemes are clearly of order O(∆t, ∆x) (The time dis-
cretization is forward Euler which is order one, and the motion by mean curvature terms
involve ∆x− , etc., which are of order one). This is not of high order; note that in the fluids
community, for instance, second order in space is a minimal requirement, since anything
less would cause too much diffusion. In the Image processing sense, however, the order of
convergence is rarely discussed, since there are often no physical phenomenon that one is
modeling. Do to the nature of its applications, what’s more important is, in general, the
computation time, and how well the solutions behave compared to what was expected (for
this model, does the steady state curve C trace out the boundary of Ω1 and Ω2 correctly?).

5 Results
All images were 100 × 100 on the unit square Ω. For all cases p = 1 and 300 steps is used
for reinitialization. The order of computation time is about 30 sec.
For each sample image, the series of evolutions of C and the piecewise constant approxi-
mations of u are shown in parallel. Note that, as the C evolves, c1 (average u inside C)
becomes darker and c2 (average u outside C) becomes lighter.

5.1 A Simple Example


Parameters: µ = 1, ν = 0, λ1 = λ2 = 1. See Figure 7.

Figure 7: Number of iterations = 41

11
5.2 Change in Topology
Parameters: µ = 1, ν = 0, λ1 = λ2 = 1. See Figure 8.

Figure 8: Number of iterations = 45. Note how the level-set approach allows for changes
in topologies.

5.3 Blurred and Noised Images


Parameters: µ = 1, ν = 0, λ1 = 1, λ2 = 2. See Figure 9.

Figure 9: Number of iterations = 45. Note how the noise is being ignored by the contour.

5.4 Chromatic Resemblance


Parameters: µ = 108 , ν = 0, λ1 = λ2 = 1. See Figure 10.

5.5 Restriction on Curve Evolution Direction


Parameters: µ = 1, ν = 0, λ1 = λ2 = 1. See Figure 11. µ = 1, ν = 106 , λ1 = λ2 = 1. See
Figure 12.

12
Figure 10: Number of iterations = 40. Note how objects of similar properties are being
grouped as one object (chromatic resemblance).

Figure 11: Number of iterations = 40. Note how a part of C extends outwards to enclose
the lone black circle in the top right.

13
Figure 12: Number of iterations = 40. Note how the curve is forced inward and the top
right circle being ignored.

Appendix - Matlab codes


runAC.m - Main driver

% runAC.m - run the active conotur w/o edges algorithm


clear all, clc;

% get Image
u0 = imread(’test3.bmp’);
u0 = u0 < 1;
u0 = u0 * 10;

% parameters
Len = 1;
[N,N] = size(u0);

figure(5),clf;
image(u0(N:(-1):1,:));
axis equal;

h = Len / (N-1);
ddt = 0.001;
x = 0:h:Len;
[X,Y] = meshgrid(x);

dt = 10^(-6); %no reason

14
Nt = 20;

mu = 1;
nu = 0;
L1 = 1;
L2 = 1;
p = 2;

TOL = dt * h^2;
NumIter = 100;

% initial level set function


phi = -(sqrt((X - 0.5).^2 + (Y - 0.5).^2)-0.4);
phin = phi;

figure(1),clf;
uplot = (u0 < 1) * 20;
pl3 = pcolor(X,Y,uplot); colormap(GRAY); shading interp; hold on;
pl2 = contour(X,Y,phin,[0 0],’r’);
setcurve(’color’,’green’,’linewidth’,4);
axis equal;
drawnow

length = L(phin,h)

flag = 1;

for k = 1:Nt
size(phi)
% reinitialize
if (flag == 1)
flag = 0;
else
phin = reinit(phi,ddt,NumIter,h);
end

% evolve
[phinn,num] = Iteration(phin,p,mu,h,dt,L1,L2,u0,TOL);

figure(1),clf;
pl3 = pcolor(X,Y,uplot); colormap(GRAY); shading interp; hold on;

15
pl2 = contour(X,Y,phinn,[0 0],’r’);
setcurve(’color’,’green’,’linewidth’,4);
axis equal;
drawnow
% iterate
phi = phinn;

end
Iteration.m - Implicit Method
% Iteration.m - Do at each time-step
function [f,num] = Iteration(phi,p,mu,h,dt,L1,L2,u0,TOL);

psi = phi;
psin = psi;

[N,N] = size(phi);
i = 2:(N-1);
j = 1:N;

sum_boolean = abs(phi) < h;


M = sum(sum(sum_boolean));
Mi = 1/M;

for t = 1:5
psin(i,i) = stepIter(psi,phi,p,mu,h,dt,L1,L2,u0);
psin(1,j) = psin(2,j); psin(N,j) = psin(N-1,j);
psin(j,1) = psin(j,2); psin(j,N) = psin(j,N-1);

Q = sum(sum(sum_boolean .* abs(psin - phi))) * Mi;

disp([’TOL = ’, num2str(TOL)]);
disp(num2str(Q));

if(Q < TOL)


break;
end

figure(99),clf;
PP = surf(psin);
title(num2str(t));

16
drawnow;
%pause

psi = psin;

end

f = psin;
num = t;
Iteration2.m - Explicit Method
% Iteration2.m - Explicit method
function [f,num] = Iteration2(phi,p,mu,h,dt,L1,L2,u0,TOL);

[N,N] = size(phi);
i = 2:(N-1);
j = 1:N;
M = zeros(N,N);

p00 = phi(i,i);
p01 = phi(i+1,i);
p10 = phi(i,i+1);
p0m = phi(i-1,i);
pm0 = phi(i+1,i);

del = delta(phi,h);

T1 = mu / h^2 * p * L(phi,h) .^ p;

%size(u0)
%size(phi)

cc1 = c1(phi,u0,h);
cc2 = c2(phi,u0,h);
T2 = L1 .* (u0(i,i) - cc1).^2;
T3 = L2 .* (u0(i,i) - cc2).^2;

Ta = (p10 - p00) ./ Dx0(phi) - (p00 - pm0) ./ Dx1(phi);

17
Tb = (p01 - p00) ./ Dy0(phi) - (p00 - p0m) ./ Dy1(phi);

M(i,i) = phi(i,i) + dt*(T1.*(Ta+Tb) - mu - T2 + T3);


M(1,j) = phi(2,j); M(N,j) = phi(N-1,j);
M(j,1) = phi(j,2); M(j,N) = phi(j,N-1);

f = M;

figure(99),clf;
PP = surf(f);
title(’explicit method - 1 step’);
drawnow;

num = -1;
stepIter.m - Jacobi Iteration for Implicit Method
% stepIter.m - each time step for phi
function f = stepIter(psi,phi,p,mu,h,dt,L1,L2,u0);

[N,N] = size(phi); % = size(psi);

i = 2:(N-1);

C_1 = 1 ./ Dx0(phi);
C_2 = 1 ./ Dx1(phi);
C_3 = 1 ./ Dy0(phi);
C_4 = 1 ./ Dy1(phi);
C = C_1 + C_2 + C_3 + C_4;
LL = L(phi,h);
d = delta(phi, h); del = d(i,i);
cc1 = c1(phi,u0,h);
cc2 = c2(phi,u0,h);

A = 1 + p*mu/h^2 * (LL^(p-1)) * del .* C;


B = mu*p/h^2 * (LL^(p-1)) * (C_1.*psi(i,i+1) + C_2.*psi(i,i-1)...
+ C_3.*psi(i+1,i) + C_4.*psi(i-1,i));
D = -L1 * (u0(i,i) - cc1).^2 + L2 * (u0(i,i) - cc2).^2;

18
f = phi(i,i) + dt * del .* (B - mu + D);
f = f ./ A;

Dx0.m, (Dx1.m, Dy0.m, Dy1.m) - Modules for FD scheme

% Dx1.m - descrete map


function d = Dx1(phi);

L = 1;
[N,N] = size(phi);
h = L / (N-1);

[N,N] = size(phi);
d = zeros(N-2,N-2);

i = 2:(N-1);
d(i-1,i-1) = sqrt( (phi(i,i) - phi(i,i-1)).^2 / (h^2)...
+ (phi(i+1,i-1) - phi(i-1,i-1)).^2 / (4*h^2) );

reinit.m - Reinitialization

% reinit.m - reninitialization of phi


function f = reinit(phi,dt,NumIter,h);

psi = phi; psin = phi;


[N,N] = size(phi);
i = 2:(N-1);
j = 1:N;

S = sign(phi);

for k = 1:NumIter
% evolve
psin(i,i) = psi(i,i) - dt * S(i,i) .* G(psi,phi,h);
% Neumann bdry cond
psin(j,1) = psin(j,2)-h;
psin(j,N) = psin(j,N-1)-h;
psin(1,j) = psin(2,j)-h;
psin(N,j) = psin(N-1,j)-h;

figure(98),clf;

19
PP = surf(psin);
title(’reinitialized’);
drawnow;
%pause

% update
psi = psin;
end

f = psin;

G.m - Module of reinitialization

% G.m - flux function for reinitialization


function g = G(psi,phi,h);

[N,N] = size(phi); % = size(psi);


i = 2:(N-1);

a = ( psi(i,i) - psi(i,i-1) ) / h;
b = ( psi(i,i+1) - psi(i,i) ) / h;
c = ( psi(i,i) - psi(i-1,i) ) / h;
d = ( psi(i+1,i) - psi(i,i) ) / h;
ap = max(a,0); am = min(a,0);
bp = max(b,0); bm = min(b,0);
cp = max(c,0); cm = min(c,0);
dp = max(d,0); dm = min(d,0);

Pos = phi(i,i) > 0;


Neg = phi(i,i) < 0;

Tp = sqrt(max(ap.^2,bm.^2) + max(cp.^2,dm.^2)) - 1;
Tn = sqrt(max(am.^2,bp.^2) + max(cm.^2,dp.^2)) - 1;

% returnes (N-1)x(N-1) matrix


g = Tp .* Pos + Tn .* Neg;

L.m - Length of C

% L.m - compute length of curve C


function l = L(phi,eps);
% phi - level set function

20
% eps - epsilon for delta function

[N,N] = size(phi);
Len = 1;
dx = Len / (N-1);

d = delta(phi,eps);

a = zeros(N,N);
b = a; c = a; d = a;

i = 1:(N-1);
a(:,i+1) = (phi(:,i+1) - phi(:,i)) / dx;
b(:,i ) = a(:,i+1);
c(i+1,:) = (phi(i+1,:) - phi(i,:)) / dx;
d(i ,:) = c(i+1,:);

ap = max(a,0); am = min(a,0);
bp = max(b,0); bm = min(b,0);
cp = max(c,0); cm = min(c,0);
dp = max(d,0); dm = min(d,0);

T1 = max(am.^2, bp.^2); T2 = max(cm.^2,dp.^2);


gradphi = sqrt(T1 + T2);

l = sum(sum(d .* gradphi)) * dx^2;

hs.m - Heaviside function

% hs.m - Heaviside function from chen & vese


function h = hs(phi,eps);

h = 0.5 * (1 + 2 / pi * atan(phi / eps));

delta.m - Dirac δ function

% delta.m - delta function from chan & vese


function d = delta(phi, eps);

d = eps ./ ((eps^2 + phi.^2) * pi);

c1.m, (c2.m) - c1 , c2 .

21
% c1.m - compute average intensity level "inside" C
%function c = c1(phi,u0);
function c = c1(phi,u0,eps)
% phi - level set function
% u0 - original image
% eps - epsilon for heaviside function

% mesh number
[N,N] = size(phi);
% assume square domain with length 1
L = 1;
% mesh spacing
dx = L / (N-1);

%%%% METHOD 1
% H = sign(phi);
% H = max(H,0);
%%%% METHOD 2
H = hs(phi,eps);

%size(u0)
%size(H)

Num = sum(sum(u0 .* H)) * dx^2;


Den = sum(sum(H)) * dx^2;

c = Num / Den;

References
[1] T. Chan and L. Vese. “Active contours without edges”, IEEE trans. on image pro-
cessing, vol. 10, no. 2, (2001)

[2] S. Osher and J. Sethian. “Fronts Propagating with Curvature Dependent Speed: Algo-
rithms Based on Hamilton-Jacobi Formulations”, Journal of Computational Physics,
79, pp.12-49, (1988)

[3] G. Sapiro. Geometric Partial Differential Equations and Image Analysis, Cambridge
Univ. Press, Cambridge, UK, 2001

22
[4] P. Perona, J. Malik, “Scale-Space and Edge Detection Using Anisotropic Diffusion,
IEEE Transactions on Pattern Analysis and Machine Intelligence, Vol. 12, No. 7,
(1990)

[5] M. Sussman, P. Smerka, S. Osher, “A Level set approach for computing solutions to
incompressible two-phase flow”, J. Comp. Phys., v. 119 pp 146-159 (1994)

23

You might also like