Contour detection

Active contours

Active countours are also often called snakes. We start of with a rough contour that is not exactly snug with the object. It is then evolved slowly to fit the boundary of the object.

For detecting specific shapes and their locations, look at Hough transform.

Evolving the contour

We start off by find the magnitudes of edges in the image, using the gradient operator. Now, we square it and then apply a gaussian blur. The purpose of this gaussian blur is for the edge magnitudes to kind of "leak" out further and attract control points from the preliminary contour.

Now our goal is to end up with a contour on a boundary. This obviously means that the control points will lie in an area with a large squared gradient (large gradient means edge). More formally, for control points , we are to maximize

Which is the same as minimizing its negative value, let us call this the cost of this image.

Greedy approach

We take a window (like a kernel but not actually going to convolve) and center it at a control point. We then check all locations in the window to see, which one has the least cost. We move the control point to the least cost location in the window.
Repeat this process for all control points and we're done.

This has a few shortcomings. During the evolution of the contour, some control points get tangled and latch onto noisy clusters. And because we check every location in a window for every control point, it is slow.

Better greedy approach

To better fit the countour, we are going to treat it as a physical object with elasticity and smoothness. Elasticity is the first derivative, if two control points are at vastly different locations, then they have a high elasticity. Smoothness if the second derivative. We define a new cost (usually called energy but sticking to cost because of previous convention),

A larger would mean elasticity is given more weight, and it acts more elastic.
We now define a total cost,

Instead of optimizing for , we will use the greedy approach to optimize for , by uniformly sampling a control point from the contour.