Chan-Vese Segmentation
Data Structures | Macros | Functions | Variables
chanvese.c File Reference

Chan-Vese active contours without edges image segmentation. More...

#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "chanvese.h"

Go to the source code of this file.

Data Structures

struct  chanvesestruct
 Options handling for ChanVese. More...
 

Macros

#define Malloc(s)   malloc(s)
 
#define Free(p)   free(p)
 
#define DIVIDE_EPS   ((num)1e-16)
 
#define M_PI   3.14159265358979323846264338327950288
 The constant pi.
 

Functions

int ChanVeseSimplePlot (int State, int Iter, num Delta, const num *c1, const num *c2, const num *Phi, int Width, int Height, int NumChannels, void *Param)
 
int ChanVese (num *Phi, const num *f, int Width, int Height, int NumChannels, const chanveseopt *Opt)
 Chan-Vese two-phase image segmentation. More...
 
void ChanVeseInitPhi (num *Phi, int Width, int Height)
 Default initialization for Phi.
 
void RegionAverages (num *c1, num *c2, const num *Phi, const num *f, int Width, int Height, int NumChannels)
 Compute averages inside and outside of the segmentation contour.
 
chanveseoptChanVeseNewOpt ()
 Create a new chanveseopt options object. More...
 
void ChanVeseFreeOpt (chanveseopt *Opt)
 Free chanveseopt options object.
 
void ChanVeseSetMu (chanveseopt *Opt, num Mu)
 Specify mu, the edge length penalty.
 
void ChanVeseSetNu (chanveseopt *Opt, num Nu)
 Specify nu, the area penalty (may be positive or negative)
 
void ChanVeseSetLambda1 (chanveseopt *Opt, num Lambda1)
 Specify lambda1, the fit weight inside the curve.
 
void ChanVeseSetLambda2 (chanveseopt *Opt, num Lambda2)
 Specify lambda2, the fit weight outside the curve.
 
void ChanVeseSetTol (chanveseopt *Opt, num Tol)
 Specify the convergence tolerance.
 
void ChanVeseSetDt (chanveseopt *Opt, num dt)
 Specify the timestep.
 
void ChanVeseSetMaxIter (chanveseopt *Opt, int MaxIter)
 Specify the maximum number of iterations.
 
void ChanVeseSetPlotFun (chanveseopt *Opt, int(*PlotFun)(int, int, num, const num *, const num *, const num *, int, int, int, void *), void *PlotParam)
 Specify plotting function. More...
 
void ChanVesePrintOpt (const chanveseopt *Opt)
 

Variables

static struct chanvesestruct DefaultChanVeseOpt
 Default options struct. More...
 

Detailed Description

Chan-Vese active contours without edges image segmentation.

Author
Pascal Getreuer getre.nosp@m.uer@.nosp@m.gmail.nosp@m..com

This file implements Chan-Vese active contours without edges two-phase image segmentation.

Copyright (c) 2007-2012, Pascal Getreuer All rights reserved.

This program is free software: you can use, modify and/or redistribute it under the terms of the simplified BSD License. You should have received a copy of this license along this program. If not, see http://www.opensource.org/licenses/bsd-license.html.

Definition in file chanvese.c.

Function Documentation

int ChanVese ( num *  Phi,
const num *  f,
int  Width,
int  Height,
int  NumChannels,
const chanveseopt Opt 
)

Chan-Vese two-phase image segmentation.

Parameters
Phipointer to array to hold the resulting segmentation
fthe input image
Width,Height,NumChannelsthe size of f
Tolconvergence tolerance
MaxItermaximum number of iterations
Mulength penalty
Nuarea penalty (positive penalizes area inside the curve)
Lambda1fit penalty inside the curve
Lambda2fit penalty outside the curve
dttimestep
PlotFunfunction for outputting intermediate results

This function performs Chan-Vese active contours two-phase image segmentation by minimizing the functional

\[ \begin{aligned}\operatorname*{arg\,min}_{c_1,c_2,C}\;& \mu \operatorname{Length}(C) + \nu\operatorname{Area}(\mathit{inside}(C)) \\ &+ \lambda_1 \int_{\mathit{inside}(C)}|f(x)-c_1|^2 \, dx + \lambda_2 \int_{\mathit{outside}(C)} |f(x) - c_2|^2 \, dx, \end{aligned} \]

where the minimization is over all set boundaries C and scalars c1 and c2. The boundary C is implicitly represented by level set function Phi.

The input f can be a grayscale image or an image with any number of channels, i.e., three channels for a color image, or possibly many more in a hyperspectral image. If f is a multichannel image, the segmentation is done using the Chan, Sandberg, Vese vector extension of the Chan-Vese model,

\[ \begin{aligned}\operatorname*{arg\,min}_{c_1,c_2,C}\;& \mu \operatorname{Length}(C)+\nu\operatorname{Area}(\mathit{inside}(C)) \\ &+ \lambda_1 \int_{\mathit{inside}(C)}\|f(x)-c_1\|^2 \,dx+\lambda_2\int_{ \mathit{outside}(C)}\|f(x)-c_2\|^2\,dx,\end{aligned} \]

where $ \|\cdot\| $ denotes the Euclidean norm.

The data for f should be stored as a contiguous block of data of Width*Height*NumChannels elements, where the elements are ordered so that f[x + Width*(y + Height*k)] = kth component of the pixel at (x,y)

The array Phi is a contiguous array of size Width by Height with the same order as f. Phi is a level set function of the segmentation, meaning the segmentation is indicated by its sign: Phi[x + Width*y] >= 0 means (x,y) is inside the segmentation curve, Phi[x + Width*y] < 0 means (x,y) is outside. Before calling this routine, Phi should be initialized either by calling InitPhi or by setting it to a level set function of an initial guess of the segmentation. After this routine, the final segmentation is obtained from the sign of Phi.

The routine runs at most MaxIter number of iterations and stops when the change between successive iterations is less than Tol. Set Tol=0 to force the routine to run exactly MaxIter iterations.

Definition at line 127 of file chanvese.c.

chanveseopt* ChanVeseNewOpt ( )

Create a new chanveseopt options object.

This routine creates a new chanveseopt options object and initializes it to default values. It is the caller's responsibility to call ChanVeseFreeOpt to free the chanveseopt object when done.

Definition at line 369 of file chanvese.c.

void ChanVeseSetPlotFun ( chanveseopt Opt,
int(*)(int, int, num, const num *, const num *, const num *, int, int, int, void *)  PlotFun,
void *  PlotParam 
)

Specify plotting function.

Parameters
Optchanveseopt options object
PlotFunplotting function
PlotParamvoid pointer for passing addition parameters

Specifying the plotting function gives control over how ChanVese displays information. Setting PlotFun = NULL disables all normal display (error messages are still displayed).

An example PlotFun is

int ExamplePlotFun(int State, int Iter, num Delta,
const num *c1, const num *c2, const num *Phi,
int Width, int Height, int NumChannels, void *Param)
{
switch(State)
{
case 0:
fprintf(stderr, " RUNNING Iter=%4d, Delta=%7.4f\r", Iter, Delta);
break;
case 1:
fprintf(stderr, " CONVERGED Iter=%4d, Delta=%7.4f\n", Iter, Delta);
break;
case 2:
fprintf(stderr, " Maximum number of iterations exceeded!\n");
break;
}
return 1;
}

The State argument is either 0, 1, or 2, and indicates ChanVese's status. Iter is the number of Bregman iterations completed, Delta is the change in the solution Delta = ||u^cur - u^prev||_2 / ||f||_2. Argument u gives a pointer to the current solution, which can be used to plot an animated display of the solution progress. PlotParam is a void pointer that can be used to pass additional information to PlotFun if needed.

Definition at line 483 of file chanvese.c.

Variable Documentation

struct chanvesestruct DefaultChanVeseOpt
static
Initial value:
=
{(num)1e-3, 500, (num)0.25, 0, 1, 1, (num)0.5,
ChanVeseSimplePlot, NULL}

Default options struct.

Definition at line 71 of file chanvese.c.