IDS peak ICV v1.6.0
Loading...
Searching...
No Matches
Thresholding and Segmentation

Image segmentation is the process of partitioning an image into multiple regions to locate objects of interest in images as a basic step of image analysis. A segmentation is typically carried out in several stages:

  • Thresholding
  • Determining connected components
  • Selecting regions by various criteria

Thresholding separates pixels into two categories (foreground and background) based on their gray value. The simplest thresholding method uses a fixed gray value range. A pixel value is classified as foreground if it belongs to the range. For example, if we assume dark objects on a bright background, we can detect the pixels by applying an interval between 0 and a maximum value (which is smaller than the background intensity). See: Apply thresholding to the image

The result of the threshold is exactly one region. Since usually several objects are visible in the image and a separate region (segment) is desirable for each object, the set of connected pixels (components) has to be determined. See: Determine connected components

To remove regions you are not interested in, you can select regions with a certain feature (e.g. area). See: Select regions with a certain feature

Recommended application scenario

Thresholding is most effective in scenarios where the illumination conditions remain consistent throughout the image. It performs well when the overall lighting remains stable, and there is minimal variation in brightness levels across the image. Additionally, external light sources, such as sunlight or artificial lighting, should have minimal impact on the image. This ensures that the objects of interest are clearly distinguishable from the background, making it easier to define suitable thresholds.

Requirements

The Thresholding technique is applicable to single-channel images with various pixel formats. The supported formats are listed in the reference of the function.

Example results

Source Image Result for interval [25, 255] Result for interval [90, 255]
Source Image Result Image Result Image

Usage instructions

Example

To process a segmentation using one of the provided interfaces, follow the steps below:

  • C++
    // Apply a threshold in the range 25 to 255
    peak::icv::Threshold threshold{ 25, 255 };
    auto region = threshold.Process(image);
    //
    // Determine connected components
    auto regions = region.GetConnectedComponents();
    //
    // Select regions by area
    peak::icv::RegionSelector selector{ regions };
    auto selectedRegions = selector.SelectByArea({ 150, 300 }).GetRegions();
  • C#
    // Apply a threshold in the range 25 to 255
    var threshold = new Threshold(new Interval(25, 255));
    using var region = threshold.Process(image);
    //
    // Determine connected components
    using var regions = region.ConnectedComponents;
    //
    // Select regions by area
    var selector = new RegionSelector(regions);
    using var selectedRegions = selector.SelectByArea(new IntervalU(150, 300)).Regions;
  • Python
    # Apply a threshold in the range 25 to 255
    threshold = Threshold(Interval(25, 255))
    region = threshold.process(image)
    #
    # Determine connected components
    regions = region.connected_components
    #
    # Select regions by area
    selector = RegionSelector(regions)
    selected_regions = selector.select_by_area(Interval(150, 300)).regions

Individual steps

Apply thresholding to the image

The following library functions return all pixel positions with gray values within the specified interval. The result is given in the form of one Region.

  • C++
    peak::icv::Threshold threshold{min, max};
    peak::icv::Region region = threshold.Process(image);
    Represents a region defined by a set of points in the image plane.
    Definition peak_icv_region.hpp:52
    Class for thresholding an image.
    Definition peak_icv_threshold.hpp:33

  • C#
    var threshold = new Threshold(new Interval(min, max));
    using var region = threshold.Process(image);

  • Python
    threshold = Threshold(Interval(min, max))
    region = threshold.process(image)

Determine connected components

The result of the threshold is exactly one region. Since usually several objects are visible in the image and a separate region (segment) is desirable for each object, the set of connected pixels, the connected components, have to be determined. Use the following functions to determine connected components.

Select regions with a certain feature

In a final step, select regions with a certain feature (e.g. area) in order to remove regions you are not interested in. Use the following functions, and replace <feature> with the feature you want to use:

  • C++
    peak::icv::RegionSelector selector{regions};
    auto selectedRegions = selector.SelectBy<Feature>({min, max}).GetRegions();
    Selects regions based on feature criteria.
    Definition peak_icv_region_selector.hpp:32
  • C#
    var selector = new RegionSelector(regions);
    using var selectedRegions = selector.SelectBy<Feature>(new IntervalU(min, max)).Regions;
  • Python
    selector = RegionSelector(regions)
    selected_regions = selector.select_by_<feature>(Interval(min, max)).regions