Active Contours: A Method for Image Segmentation in Computer Vision

Dulari Last Updated : 28 Oct, 2024
8 min read

Computer vision enables computers to interpret and analyze visual data to mimic human vision. Unlike humans, who can instantly recognize objects like animals, landscapes, or textures, computers use mathematical methods to identify image components. One of the prominent techniques in this area is Image Segmentation, which helps divide an image into distinct segments for object detection. This article delves into the concept of Active Contours, a powerful image segmentation technique used for distinguishing objects with irregular shapes and precise boundaries.

Overview:

  • Learn about the core concept of image segmentation, which partitions images by grouping pixel values to identify different surfaces or objects.
  • Understanding active contours as a segmentation approach that utilizes energy-based forces to isolate pixels of interest.
  • Explore why active contours are crucial in scenarios requiring the identification of complex shapes, such as medical imaging.
  • Understand the internal mechanics of active contours, including external and internal energy forces, that guide the contour to form an outline around objects, enhancing segmentation accuracy.
  • Discover different models like the Snake, Gradient Vector Flow, and Balloon models.

This article was published as a part of the Data Science Blogathon

What is Image Segmentation?

Image segmentation means partitioning the input image by clustering pixel values. It mainly identifies various surfaces or living or nonliving objects in an image. For example, if you have the following image as an input, you can have a tiger, green grass, blue water, and land as various surfaces in your output image.

image segmentation | Active Contours

Various image segmentation techniques exist, such as Active contours, split and merge, watershed, region splitting, region merging, graph-based segmentation, mean shift and model finding, and Normalized cut.

This article explains one of the most useful image segmentation techniquesActive Contours.

What are Active Contours?

Active contour is a segmentation method that uses energy forces and constraints to separate the pixels of interest from a picture for further processing and analysis.

An active contour is defined as an active model for the segmentation process. Contours are the boundaries that define the region of interest in an image. A contour is a collection of points that have been interpolated. Depending on how the curve in the image is described, the interpolation procedure might be linear, splines, or polynomial.

Why is Active Contours Needed?

Active contours are mainly used to identify uneven shapes in images. They are also used to define smooth shapes in images and construct closed contours for regions.

Active contours are used in various medical image segmentation applications. Active contour models are employed in various medical applications, particularly for separating desired regions from medical images. For example, a slice of a brain CT scan is examined for segmentation using active contour models.

How Does Active Contour Work?

Active contours are the technique of obtaining deformable models or structures in an image with constraints and forces for segmentation. Contour models define the object borders or other picture features to generate a parametric curve or contour.

The curvature of the models is determined using several contour techniques that employ external and internal forces. The energy function is always related to the image’s curve. External energy is described as the sum of forces caused by the picture that is specifically used to control the location of the contour onto the image, and internal energy is used to govern deformable changes.

The contour segmentation constraints for a certain image need to be determined. The desired shape is obtained by defining the energy function. A collection of points that locate a contour describes contour deformation. This shape corresponds to the desired image contour, defined by minimizing the energy function.

Active Contour Segmentation Models

Let us now look at some active contour segmentation models.

1. Snake Model

The snake model is a technique that can solve a broad range of segmentation problems. The model’s primary function is identifying and outlining the target object for segmentation. It requires prior knowledge of the target object’s shape, especially for complicated things. Active snake models, often known as snakes, are generally configured by using a spline focused on minimizing energy, followed by various forces governing the image.

Equation

A simple snake model can be denoted by a set of n points, vi for i=0,….n-1, the internal elastic energy term EInternal, and the external edge-based energy term Eexternal. The internal energy term regulates the snake’s deformations, while the exterior energy term controls the contour’s fitting onto the image. The external energy is typically a combination of forces caused by the picture Eimage and constraint forces imposed by the user Econ.

The snake’s energy function is the total of its exterior and internal energy, which can be written as below.

{displaystyle E_{text{snake}}^{*}=int limits _{0}^{1}E_{text{snake}}(mathbf {v} (s)),ds=int limits _{0}^{1}(E_{text{internal}}(mathbf {v} (s))+E_{text{image}}(mathbf {v} (s))+E_{text{con}}(mathbf {v} (s))),ds}

Advantage

The applications of the active snake model are expanding rapidly, particularly in the many imaging domains. In medical imaging, the snake model is used to segment one portion of an image with unique characteristics compared to other regions. Traditional snake model applications in medical imaging include optic disc and cup segmentation to identify glaucoma, cell image segmentation, vascular region segmentation, and several other regions segmentation for diagnosing and studying disorders or anomalies.

Disadvantage

The conventional active snake model approach has various inefficiencies, such as noise sensitivity and erroneous contour detection in high-complexity objects, addressed in advanced contour methods.

2. Gradient Vector Flow Model

The gradient vector flow model is a more developed, well-defined version of the snake or active contour models. The traditional snake model has two limitations: inadequate contour convergence for concave borders and when the snake curve flow is commenced at a great distance from the minimum. As an extension, the gradient vector flow model uses the gradient vector flow field as an energy constraint to determine the contour flow.

Equation

In 2D, the GVF vector field FGVF minimizes the energy functional.

{displaystyle E_{text{GVF}}=iint mu (u_{x}^{2}+u_{y}^{2}+v_{x}^{2}+v_{y}^{2})+|nabla f|^{2}|mathbf {v} -nabla f|^{2},dx,dy}

where ‘μ’ is a controllable smoothing term.

Advantage

The gradient vector flow model is an advanced version of the snake model used for various image processing applications, especially in medical image processing. Active contour models help segment regions with particular parameters in medical imaging. These models create a contour around the target object, separating it from the image.

Disadvantage

The main difficulty with utilizing GVF is that the smoothing term ‘μ’ causes the contour’s edges to round. Reducing the value of ‘μ’ minimizes rounding but increases the amount of smoothing.

3. Balloon Model

A snake model isn’t drawn to far-off edges. If no significant image forces apply to the snake model, its inner side will shrink. A snake larger than the minimum contour will eventually shrink into it, whereas a smaller snake will not discover the minimum contour and will instead continue to shrink. To address the constraints of the snake model, the balloon model was developed, in which an inflation factor is incorporated into the forces acting on the snake. The inflation force can overwhelm forces from weak edges, exacerbating the problem with first-guess localization.

Equation

Inflation term into the forces acting on the snake is introduced in the balloon model.

{displaystyle F_{text{inflation}}=k_{1}{vec {n}}(s)}

Where n(s) is the normal unitary vector of the curve at v(s), and k1 is the magnitude of the force.

Advantage

The balloon concept is used to segment various medical pictures. The application’s primary purpose is to propose a novel technique for segmenting 2-D images and reconstructing 3-D meshes that ensure a watertight mesh.

Disadvantage

The balloon model’s biggest problem is its slow processing, which makes it difficult to manage sharp edges and requires careful object placement. The balloon model is commonly used in analyzing picture contour extraction.

4. Geometric or Geodesic Active Contour Models

Geometric active contour (GAC) is a contour model that adjusts the smooth curve established in the Euclidean plan by moving the curve’s points perpendicular. The points move proportionately to the curvature of the image’s region. The curve’s geometric flow and the image’s recognition of items are used to characterize contours. Geometric flow encompasses internal and external geometric measures in the region of interest. A geometric replacement for snakes is utilized to detect items in an image. These contour models rely heavily on the level set functions that specify the image’s unique regions for segmentation.

Equation

For example, the gradient descent curve evolution equation of GAC is

{displaystyle {frac {partial C}{partial t}}=g(I)(c+kappa ){vec {N}}-langle ,nabla g,{vec {N}}rangle {vec {N}}} | Active Contours

Where g(I) is a halting function, c is a Lagrange multiplier, K is the curvature, and vector N is the unit inward normal. This particular curve evolution equation only depends on the velocity in the normal direction. It, therefore, can be rewritten equivalently in an Eulerian form by inserting the level set function φ into it as follows:

{displaystyle {frac {partial Phi }{partial t}}=|nabla Phi |operatorname {div} {Bigg (}g(I){frac {nabla Phi }{|nabla Phi |}}{Bigg )}+cg(I)|nabla Phi |}| Active Contours

Advantage

Geometric active contours are mostly used in medical image computing, particularly image-based segmentation. In this case, any imaging modality’s picture is examined for segmentation to research, process, and analyze the regions of interest. These regions include any aberration in the interior regions or organs of the human body, such as blood clots, traumas, lesions, cell abnormalities, metabolic interruptions, biomolecule disruptions, etc.

Disadvantage

Mostly, it has no such inefficiencies, but they are difficult to implement as they are complex.

Implementation of Snake Model Using Active Contours

Let us now look at the implementation of snake model using active contours.

Python Code:

import numpy as np
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
from skimage import data
from skimage.filters import gaussian
from skimage.segmentation import active_contour
image = data.astronaut()
image = rgb2gray(image)
s = np.linspace(0, 2*np.pi, 400)
r = 100 + 100*np.sin(s)
c = 220 + 100*np.cos(s)
init = np.array([r, c]).T
snake = active_contour(gaussian(image, 3, preserve_range=False),
init, alpha=0.015, beta=10, gamma=0.001)
fig, ax = plt.subplots(figsize=(7, 7))
ax.imshow(image, cmap=plt.cm.gray)
ax.plot(init[:, 1], init[:, 0], '--r', lw=3)
ax.plot(snake[:, 1], snake[:, 0], '-b', lw=3)
ax.set_xticks([]), ax.set_yticks([])
ax.axis([0, image.shape[1], image.shape[0], 0])
plt.show()

Conclusion

In conclusion, active contour models are essential for image processing, offering precise and adaptable solutions for image segmentation. By leveraging various methods—such as the Snake model, Gradient Vector Flow, and Balloon models—active contour algorithms effectively handle complex shapes and object boundaries, making them invaluable in fields like medical imaging and object tracking. As explored in this article, the active contour model for image segmentation provides a powerful approach through energy-based mechanics, allowing for the identification of irregular contours and structures. The active contour segmentation Python code example further illustrates the ease of implementing these models in practical applications. In short, active contours in image processing continue to be foundational, advancing both research and practical applications in computer vision.

Frequently Asked Questions

Q1. What is active contour segmentation?

A. Active contour segmentation, or “snake,” is a computer vision technique for detecting object boundaries by creating an initial contour that iteratively adjusts to fit edges accurately. Driven by image gradients, internal energy, and external constraints, it excels at handling complex shapes and occlusions, surpassing traditional edge detection methods.

Q2. What can active contour segmentation be employed with?

A. Active contour segmentation can be employed in various computer vision applications, such as medical image analysis (e.g., organ segmentation in MRI scans), object tracking in videos, image editing (e.g., precise object isolation for photo manipulation), and industrial automation (e.g., defect detection on manufactured items). It excels in cases where accurate boundary delineation of objects with complex shapes and varying contrasts is necessary.

Q3. What is the role of active contour snakes in image segmentation?

A. Active contour snakes in image segmentation identify object boundaries by iteratively adjusting a contour to fit edges precisely. Using forces from image gradients and energy constraints, snakes excel in defining complex shapes and subtle boundaries, making them valuable for detecting objects with irregular, intricate contours.

Q4. What is the purpose of image segmentation?

A. Image segmentation partitions an image into distinct regions based on pixel values, allowing for precise identification of surfaces or objects. By isolating these regions, segmentation aids in object detection, analysis, and classification in medical imaging, autonomous vehicles, and other fields.

Q5. What are the applications of contours?

A. Contours are widely used in medical imaging for segmenting organs or abnormalities, object tracking in videos, industrial quality control, and photo editing. They enable precise boundary detection and shape delineation, essential for accurately analyzing complex images and supporting various applications in visual processing.

The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.

Dr. Dulari Bhatt is Master Trainer at Edunet Foundation. She is having 12 years of experience in academia. Her main research interests are in the field of Big Data Analytics, Computer Vision, Machine Learning, and Deep Learning. She has authored 3 technical books and published several research papers in reputed international journals. She is an active writer in Analytics Vidhya and OSFY magazine. She has received a gold category award from GTU for spreading IPR awareness. Several times winner of Data Science Blogathons..

Responses From Readers

Clear

fuckyourstupidpopupwtf
fuckyourstupidpopupwtf

explanations aren't good, no visualizations of anything, code is unstructured and uncommented 2/10 (2 points for effort of using LLMs to create this)

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details