# Level Set Method Part II: Image Segmentation

Last post, we looked at the intuition and the formulation of Level Set Method. It’s useful to create a physical simulation like front propagation, e.g. wave simulation, wildfire simulation, or gas simulation. In this post, we are going to see into Level Set Method application in Computer Vision, to solve image segmentation problem.

## Image Segmentation with Level Set Method

Recall that the Level Set PDE that we have derived in the last post is as follows:

\[\phi' = \phi + \Delta t F {\lVert \nabla \phi \rVert}\]Here, \( F \) would need our special attention, as the key to adapting Level Set Method into new problem is by formulating this term. \( F \) is intuitively a force that drive curve propagation. In other words, we could think of \( F \) as a velocity field, i.e. \( F \) is a vector field where at every point it tells us the direction and magnitude of movement of our surface \( \phi \).

As we want to segment an image, then we need to derive \( F \) from the image. Let’s say we have this image:

As \( F \) is a velocity field and consider the Level Set PDE above, we want \( F \) to be high at all region that are not the border of the object we want to segment, and low otherwise. Intuitively, we want the curve to propagate quickly in the background of the image, and we want the curve to slowly propagate or even stop the propagation at the border of the object.

One way to do it is obviously derive our \( F \) from edge detector. Simplest way to do edge detection is to take the gradients of the image:

\[g(I) = \frac{1}{1 + {\lVert \nabla I \rVert}^2}\]So, we could implement it with the code below:

So that’s it, we have our \( F \). We could plug it into the PDE directly then:

And here’s the segmentation result after several iteration:

This is the naive method for Level Set image segmentation. We could do better by using more complicated formulation for \( F \).

## Geodesic Active Contour

In Geodesic Active Contour (GAC) formulation of Level Set Method, we define the force as:

\[\frac{\partial \phi}{\partial t} = g(I) {\lVert \nabla \phi \rVert} div \left( \frac{\nabla \phi}{\lVert \nabla \phi \rVert} \right) + g(I) {\lVert \nabla \phi \rVert} v + \nabla g(I) \cdot \nabla \phi\]The first term is the smoothing term, it moves the curve into the direction of its curvature. The second term is the balloon term, controlling the speed of the curve propagation with parameter \( v \). Lastly, the third term is the image attachment term that helps the curve to converge.

Finally, here’s the result of using GAC formulation of Level Set Method for segmenting the same image above:

Notice that qualitatively the segmentation result is better than before, i.e. smoother and better fit.

## Conclusion

In this post we looked into the application of Level Set Method on Computer Vision problem, that is image segmentation. We saw the intuition on applying it for image segmentation. We also saw the example of implementation of it. Finally we saw the more complicated formulation, i.e. GAC, for better segmentation.

## References

- Richard Szeliski. 2010. Computer Vision: Algorithms and Applications (1st ed.). Springer-Verlag New York, Inc., New York, NY, USA.