Join thousands of students in our LangChain and Vector DBs in Production course, with over 50+ lessons and practical projects for FREE!.

## Publication # Geodesic Active Contours: The Key to Image Object Separation

Last Updated on May 18, 2023 by Editorial Team

#### Author(s): Abhijith S Babu

Originally published on Towards AI.

Recently I tried a photo editing app that adds custom backgrounds to my image. So I uploaded a photo of me standing in my home and applied a beach background so that the output image looks like I am standing on a beach. But when I uploaded my photo, how did the software understand what parts of the image were me and what part is the image?

Well, that is where image segmentation comes to play. Image segmentation is the task of finding the pixels that go together in an image. This technique is useful to separately identify the objects present in an image. Here we can go through how Geodesic Active Contour helps in segmenting an image into objects even if the boundary is not so well defined.

Active Contours are intelligent algorithms that can be used for precisely finding the segmentation of objects even in the presence of noise or low contrast. Here contour means the outline of the objects in an image. They represent the boundaries that enclose an object in an image. Active contour algorithms start with an initial curve which can be deformed or adjusted during the process of segmentation. The algorithm analyses the image features like texture, edges, and so on to determine the object boundaries. Active contours are dynamically evolving, adjusting their shape and position based on the features of the target object.

There are several active contour algorithms. Here we are going to see Geodesic Active Contour, which uses a level set formulation to evolve the contour.

The general steps in image segmentation using Geodesic Active Contours are as follows:

Initialization of contours
Based on the information (if any) we have regarding the shape, size, and location of the object in the image, we can estimate an initial curve. This curve is modified and deformed later to form the actual boundary. The initial contour defined can be closed or open depending on the nature of the object that we want to detect.

Level Set Initialization
In this step, the initial contour is converted to a level set representation. Level set representation refers to assigning values to the pixels in the image based on the initial contour. The contour will have a level value of zero. The pixels inside the contour will have a positive value whereas the pixels outside the contour will have a negative value. The value is found using a function known as the level set function The magnitude of the value is the distance of the pixel from the contour. Here distance means the Euclidean distance between the pixel and the nearest point in the contour.

Energy function of contours
The energy function of the contour is a mathematical function that determines the quality of the given contour. The energy functional is a combination of both external and internal energy. Let us have a look at how each of them is calculated.

Internal energy is the function that captures the smoothness of the contour, which prevents the output from being jagged or irregular. For calculating the curvature-based internal energy, we have to design a function that penalizes high curvature. Using various techniques, the curvature value of each point is calculated and summed up to get the internal energy value. The curvature of a point can be quantified using geometric techniques such as differential geometry, Frenet-Serret formula (which uses derivatives of the tangent, normal, and bi-normal vectors), etc, or using numerical approximation techniques like moving least squares or finite difference approximations.

External energy is the function that brings the contour closer to the actual image border. External energy can be calculated based on the gradient of the image, which gives information about the local intensity variation in the image. The gradient of the image is computed using filters such as the Sobel filter. The filter is applied on both the x-axis and y-axis. The gradient value along each axis can be used to find the magnitude of the gradient in the pixel using this formula

For each point in the vicinity of the contour points, the gradient value is found and they are summed up to get the external energy of the contour.

After finding the external and internal energy values, the total energy of the contour can be calculated from the weighted sum of the internal and external energies. The weights are determined based on the relative importance of smoothness and fidelity of the image features. The weight values can be adjusted according to the requirements of the segmentation task.

Contour evolution
Now the contours are evolved by altering the level set representation based on the energy of the contour. We use numerical methods that iteratively update the level set representation thus reducing the energy function. Here we can use the narrow band method for the updation of the level set.

The narrow band method defines a subset of the overall image which consists of the contours and a narrow layer around the contour. The narrow band is initialized by defining the width and selecting the contour and its neighboring points that fall inside the defined width. Now we iterate through each point in the band. Calculate the partial derivative of the level set function at each pixel. The partial derivatives at each point can be calculated using finite difference approximations. Now we can derive the evolution equation from the derivatives. The evolution equation is derived in such a way that energy function is minimized. The evolution equation has to be a combination of the internal energy term and external energy term. Using the equation, the level set function is updated iteratively.

The narrow band has to be updated after each iteration to make sure that the contour and its immediate neighbors are still in the band. The iterations will be carried out until the level set representation is converging, which means the contour captures the actual boundaries in the image. The final contour has to be extracted from the level set representation by locating the points where the level set function has a value close to zero. We can obtain a smooth and continuous contour from these points by interpolation. The resulting connected points represent the boundary of the segmented object.

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.

Published via Towards AI