Search Results for

    Show / Hide Table of Contents

    Class AdaptiveThresholdFilter

    A filter to apply adaptive thresholding to an image.

    Inheritance
    System.Object
    AdaptiveThresholdFilter
    Implements
    IImageFilter
    Namespace: IronBarCode
    Assembly: IronBarCode.dll
    Syntax
    public class AdaptiveThresholdFilter : Object, IImageFilter

    When a barcode image has uneven lighting, glare, or a shadow falling across part of the frame, a single global cutoff cannot cleanly separate the bars from the background, and AdaptiveThresholdFilter is the preprocessing step that handles it. It applies a Bradley adaptive threshold, computing a local cutoff for each region of the image rather than one value for the whole picture, so a barcode that is bright on one side and dim on the other still binarizes into clean black bars on a white field. This is the filter to reach for on photos and scans with non-uniform illumination, where a plain binary threshold would lose part of the code.

    The filter is one of the IImageFilter implementations that go into the ImageFilterCollection assigned to BarcodeReaderOptions.ImageFilters. The reader applies the filters in collection order before it attempts decoding, so an adaptive threshold typically sits early in the pipeline, ahead of a sharpen or contrast pass, because it produces the clean two-tone image those later steps refine. A developer constructs the filter, adds it to the collection, and passes the options to BarcodeReader.Read along with the source image.

    Several constructors trade off convenience against control. The parameterless form uses tuned defaults, while overloads accept a Threshold, the Lower and Upper replacement colors, and a Rectangle that confines the operation to a region of interest. The Threshold property sets the sensitivity of the local comparison, Lower and Upper choose the two output colors the pixels collapse to, and Rectangle restricts the filter to part of the frame when only one zone needs correcting. Tune Threshold first; the defaults already suit most documents with patchy lighting.

    using IronBarCode;
    
    var options = new BarcodeReaderOptions
    {
        ImageFilters = new ImageFilterCollection { new AdaptiveThresholdFilter() }
    };
    var results = BarcodeReader.Read("uneven-lighting.png", options);

    The image correction how-to walks through each filter on a sample barcode, and the imperfect barcode example shows a full read of a low-quality image. For images that are also skewed or rotated, the image orientation correction how-to is the companion guide.

    Constructors

    AdaptiveThresholdFilter()

    Initializes a new instance of the AdaptiveThresholdFilter class.

    Declaration
    public AdaptiveThresholdFilter()

    AdaptiveThresholdFilter(Color, Color, Single)

    Initializes a new instance of the AdaptiveThresholdFilter class.

    Declaration
    public AdaptiveThresholdFilter(Color upper, Color lower, float threshold)
    Parameters
    Type Name Description
    IronSoftware.Drawing.Color upper

    The color to use for pixels that are above the threshold.

    IronSoftware.Drawing.Color lower

    The color to use for pixels that are below the threshold.

    System.Single threshold

    Threshold limit (0.0-1.0) to consider for binarization.

    AdaptiveThresholdFilter(Single)

    Initializes a new instance of the AdaptiveThresholdFilter class.

    Declaration
    public AdaptiveThresholdFilter(float threshold)
    Parameters
    Type Name Description
    System.Single threshold

    Threshold limit (0.0-1.0) to consider for binarization.

    AdaptiveThresholdFilter(Single, Color, Color, Rectangle)

    Initializes a new instance of the AdaptiveThresholdFilter class.

    Declaration
    public AdaptiveThresholdFilter(float threshold, Color upper, Color lower, Rectangle rectangle)
    Parameters
    Type Name Description
    System.Single threshold

    Threshold limit (0.0-1.0) to consider for binarization.

    IronSoftware.Drawing.Color upper

    The color to use for pixels that are above the threshold.

    IronSoftware.Drawing.Color lower

    The color to use for pixels that are below the threshold.

    IronSoftware.Drawing.Rectangle rectangle

    Rectangular region to apply the threshold.

    AdaptiveThresholdFilter(Single, Rectangle)

    Initializes a new instance of the AdaptiveThresholdFilter class.

    Declaration
    public AdaptiveThresholdFilter(float threshold, Rectangle rectangle)
    Parameters
    Type Name Description
    System.Single threshold

    Threshold limit (0.0-1.0) to consider for binarization.

    IronSoftware.Drawing.Rectangle rectangle

    Rectangular region to apply the adaptive threshold.

    Properties

    Lower

    The color to use for pixels that are below the threshold. Default = Black.

    Declaration
    public Color Lower { get; set; }
    Property Value
    Type Description
    IronSoftware.Drawing.Color

    Rectangle

    Rectangle region to apply the filter on.

    Declaration
    public Rectangle Rectangle { get; set; }
    Property Value
    Type Description
    IronSoftware.Drawing.Rectangle

    Threshold

    Threshold limit (0.0-1.0) to consider for binarization. Default = 1.0.

    Declaration
    public float Threshold { get; set; }
    Property Value
    Type Description
    System.Single

    Upper

    The color to use for pixels that are above the threshold. Default = White.

    Declaration
    public Color Upper { get; set; }
    Property Value
    Type Description
    IronSoftware.Drawing.Color

    Implements

    IImageFilter
    ☀
    ☾
    Downloads
    • Download with Nuget
    • Start for Free
    In This Article
    Back to top
    Install with Nuget