Image Binarization

You are currently viewing the documentation for a pre-release version (2.2.0). To access documentation for other versions, click the "Switch Version" button located in the upper-right corner of the page.

■ If you're unsure about the version of the product you are using, please contact Mech-Mind Technical Support for assistance.

Function

This Step is used to binarize pixels based on whether their values are above or below a specified threshold.

When the pixel grayscale values in an image have only two possible values (maximum and minimum), i.e., "black or white," such an image is called a binary image.

The process of converting a non-binary image into a binary image through calculation is called image binarization.

Usage Scenario

This Step is generally used for image processing. It is typically applied to segment pixels in 2D images based on a specified threshold.

Input and Output

Input

Input ports Data type Description

Image

Image

The input image will be binarized.

Output

Output ports Data type Description

Processed Image

Image

The binarized image.

Parameter Description

Parameter Description

Binary Method

Parameter description: This parameter specifies the method for image binarization.

Value list:

  • Adaptive thresholding: This method automatically calculates an appropriate global threshold based on the input image and segments pixels that meet the threshold condition.

  • Dual thresholding: This method uses defined high and low threshold values to segment pixels that meet the threshold condition.

  • Dynamic thresholding: This method divides the image into multiple small regions and binarizes each using a local threshold.

  • Thresholding: This method uses a manually defined fixed global threshold to segment pixels that meet the threshold condition.

Default value: Adaptive thresholding

Invert Binary Image

Parameter description: Select this option to invert the binary result of the entire image: pixels that would be 255 become 0, and pixels that would be 0 become 255.
Default value: Not selected

Adaptive Thresholding

The following parameters are available after selecting the Adaptive thresholding method.

Parameter Description

Image Channel Type

Parameter description: Thresholding is applied based on the selected image channel.
Value list: Grayscale, Hue, Saturation, Value

Threshold Calculation Method

Parameter description: This parameter specifies a method for calculating the threshold for each pixel in the image.

Value list: Mean, Weighted mean

  • Mean: The threshold is the mean of the pixel’s neighborhood minus the constant.

  • Weighted mean: The threshold is the weighted mean of the pixel’s neighborhood minus the constant.

Thresholding Type

Parameter description: This parameter determines the rule for binarizing the image.

Value list: Binary, Binary inverted

  • Binary: If the pixel’s grayscale value is greater than the calculated threshold, set it to 255; otherwise, set it to 0.

  • Binary inverted: If the pixel’s grayscale value is greater than the calculated threshold, set it to 0; otherwise, set it to 255.

Kernel Size

Parameter description: The neighborhood size used during threshold calculation, in pixels (px).
Default value: 21

Constant

Parameter description: A constant used for threshold calculation. Increasing this value darkens the output image, while decreasing it brightens the image.
Default value: 0

Dual Thresholding

The following parameters are available after selecting the Dual thresholding method.

Parameter Description

Threshold 1, Threshold 2

Parameter description:
If Threshold 1 < Threshold 2, any pixel with a value between the two thresholds is set to 255, and all other pixels are set to 0.
If Threshold 1 > Threshold 2, any pixel with a value outside the threshold range is set to 255, and all other pixels are set to 0.
If Threshold 1 = Threshold 2, any pixel with a value equal to the threshold is set to 255, and all other pixels are set to 0.

Dynamic Thresholding

The following parameters are available after selecting the Dynamic thresholding method.

Parameter Description

Thresholding Type

Parameter description: This parameter determines the rule for binarizing the image.

Value list:

  • Bright region: If P ≥ P' + offset, set P to 255; otherwise, set it to 0.

  • Dark region: If P ≤ P' - offset, set P to 255; otherwise, set it to 0.

  • In range: If P' - offset ≤ P ≤ P' + offset, set P to 255; otherwise, set it to 0.

  • Out of range: If P < P' - offset or P > P' + offset, set P to 255; otherwise, set it to 0.

Filter Type

Parameter description: This parameter selects the filter to apply to the image.

Value list:

  • Mean: Smooths the image and reduces random noise by replacing the value of each pixel with the mean of the pixel and its neighbors.

  • Gaussian: Effectively removes certain image noise to smooth the image while preserving edges and details as much as possible, commonly used for image smoothing, noise reduction, and preprocessing before edge detection.

  • Median: Effectively reduces image noise by replacing the value of each pixel with the median of the pixel and its neighbors, especially salt and pepper noise (typically appearing as random black and white points).

Pixel Value Offset

Parameter description: Adds a constant value to, or subtracts it from, the filtered pixel values to adjust the binarization result.

Kernel Size

Parameter description: This parameter sets the filter window size in pixels (px). Please enter an odd number as there should always be a center pixel in the window. Any even numbers entered will be incremented by one.

Thresholding

The following parameters are available after selecting the Thresholding method.

Parameter Description

Image Channel Type

Parameter description: Thresholding is applied based on the selected image channel.
Value list: Grayscale, Hue, Saturation, Value

Threshold (0–255)

Parameter description: Manually define a fixed global threshold used to segment pixels that meet the threshold condition.

Thresholding Type

Parameter description: This parameter determines the rule for binarizing the image.

Value list:

  • Binary: If the pixel’s grayscale value is greater than the set threshold, set it to 255; otherwise, set it to 0.

  • Binary inverted: If the pixel’s grayscale value is greater than the set threshold, set it to 0; otherwise, set it to 255.

  • Truncate: If the pixel’s grayscale value is greater than the set threshold, set it to the threshold value; otherwise, it remains unchanged.

  • To zero: If the pixel’s grayscale value is greater than the set threshold, preserve the original grayscale value; otherwise, set it to 0.

  • To zero inverted: If the pixel’s grayscale value is greater than the set threshold, set the original grayscale value to 0; otherwise, it remains unchanged.

  • Otsu: A global threshold is calculated using the Otsu algorithm.

  • Triangle: A global threshold is calculated using the Triangle algorithm.

Parameter Tuning Examples

Case 1: Clean Background with High Target-Background Contrast

  • Scenario:The target object and background have a significant grayscale difference with stable lighting conditions, such as detecting the position of a black object on a white background.

  • Recommended approach: Select the Thresholding method and manually set a fixed threshold for quick and effective separation of the target from the background.

  • Tuning approach:

    1. First observe the grayscale histogram of the image to determine the grayscale value ranges of the target and background.

    2. Select a value between the two grayscale ranges as the initial threshold (e.g., if the target range is 0–80 and the background range is 150–255, set the initial threshold to 115).

    3. Adjust the threshold during the process to progressively optimize the segmentation result.

    4. If the target is incompletely segmented, lower the threshold; if there is too much background noise, raise the threshold.

    5. Select "Binary" or "Binary inverted" as needed to adjust the foreground and background colors.

Case 2: Uneven Lighting with Background Gradient

  • Scenario: In natural lighting environments or scenes with shadows, brightness varies significantly across different regions of the image. Using a global threshold can easily lead to under-segmentation or over-segmentation.

  • Recommended approach: Select the Adaptive thresholding method. The system dynamically calculates the threshold based on the pixel neighborhood, adapting to lighting variations.

  • Tuning approach:

    1. Set the initial "Kernel Size" to 21 pixels and observe the segmentation result.

    2. If the target contour is blurry or boundary detection is unclear, increase the kernel size (e.g., 31 or 41) to enlarge the neighborhood and make the threshold calculation more stable.

    3. If target details are lost, decrease the kernel size (e.g., 11 or 15) to make the threshold more sensitive.

    4. Adjust the "Constant" parameter: increasing the constant darkens the overall image (more foreground), while decreasing the constant brightens it (more background).

    5. Select the appropriate channel (Grayscale, Hue, Saturation, etc.) based on the image channels to achieve the best segmentation.

Case 3: Image Contains Noise, Need to Remove Salt-and-Pepper Noise

  • Scenario: Images acquired by low-quality cameras or in harsh environments contain random black and white noise points (salt-and-pepper noise). Direct binarization would retain the noise.

  • Recommended approach: Select the Dynamic thresholding method combined with Median filtering to effectively remove salt-and-pepper noise.

  • Tuning approach:

    1. Select the "Dynamic thresholding" method.

    2. Select "Median" as the filter type. Median filtering is particularly suitable for removing salt-and-pepper noise.

    3. Set the "Kernel Size" to 5 or 7 (odd number) to define the filter window size.

    4. Select the "Thresholding Type" based on the target characteristics:

      • To detect brighter targets, select "Bright region".

      • To detect darker targets, select "Dark region".

      • To detect pixels within a certain range, select "In range".

      • To detect pixels outside a certain range, select "Out of range".

    5. Adjust the "Pixel Value Offset" (offset) to control the segmentation tolerance: increasing the offset includes more pixels, while decreasing it makes the segmentation stricter.

    6. Observe the result and progressively fine-tune the kernel size and offset value.

Case 4: Need to Extract a Specific Brightness Range

  • Scenario: The target object’s grayscale values are concentrated in a specific range, and pixels within this range need to be precisely extracted, such as detecting regions of specific brightness on a semiconductor chip surface.

  • Recommended approach: Select the Dual thresholding method to precisely control the pixel range by setting two threshold boundaries.

  • Tuning approach:

    1. Observe the grayscale value range of the target region, e.g., target pixel grayscale is 100–180.

    2. Set Threshold 1 to the lower bound and Threshold 2 to the upper bound of the range (e.g., Threshold 1 = 100, Threshold 2 = 180).

    3. This extracts pixels between the two thresholds as 255, and all other pixels as 0.

    4. If reverse extraction is needed (extracting pixels outside the range), adjust Threshold 1 and Threshold 2 so that Threshold 1 > Threshold 2.

    5. Progressively adjust the boundary values of both thresholds based on the segmentation result to achieve precise grayscale range filtering.

Is this page helpful?

You can give a feedback in any of the following ways:

We Value Your Privacy

We use cookies to provide you with the best possible experience on our website. By continuing to use the site, you acknowledge that you agree to the use of cookies. If you decline, a single cookie will be used to ensure you're not tracked or remembered when you visit this website.