How to use Image Correction Filters

by Hairil Hasyimi Bin Omar

Let's face it. Not every image is perfect, and it is also one of the main factors that a barcode image is unable to be read by IronBarcode. This is not entirely the user's fault. Instead of going through the hassle of recapturing the image or using other image enhancement software, IronBarcode has come up with a feature that enables users to apply filters to the image programmatically. This would help IronBarcode read the image and improve accuracy.

Continue reading to learn about available image correction filters in IronBarcode, their effects on images, and how to apply them.

Get started with IronBarcode

Start using IronBarcode in your project today with a free trial.

First Step:
green arrow pointer


Use Image Filters to Improve Read Example

To apply the filter, instantiate the ImageFilterCollection class and create instances of each filter individually. Then assign the object to the ImageFilters property of the BarcodeReaderOptions object. pass the options object into the Read method along with the sample image.

Let's use the image below as our sample image.

Sample image

From the initial look of the image, it appears to be quite blurry. However, the brightness is acceptable, and the white and black colors are distinguishable. Therefore, we need to apply at least the SharpenFilter and ContrastFilter to improve barcode readability. Refer to the code snippet below to apply the filters to the image, read it, and display it on the console.

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-apply-filter.cs
using IronBarCode;
using System;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection()
    {
        new SharpenFilter(3.5f),
        new ContrastFilter(2)
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Write the result value to console
foreach (BarcodeResult result in results)
{
    Console.WriteLine(result.Text);
}
Imports IronBarCode
Imports System

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection() From {
		New SharpenFilter(3.5F),
		New ContrastFilter(2)
	}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Write the result value to console
For Each result As BarcodeResult In results
	Console.WriteLine(result.Text)
Next result
VB   C#

From the code snippet above, apart from applying filters and reading the barcode, we also exported the filtered image to disk. The comparison between the sample and filtered images can be seen below.

Sample image
Filtered sample

Explore Image Correction Filters

IronBarcode offers multiple image filters specifically designed for image correction. These filters can assist in reading imperfect barcode images and enhancing reading accuracy. However, users will need to understand how these filters work in order to select suitable filter and avoid performance issue due to using too much filter or using wrong filter. Below are all filters available:

  • AdaptiveThresholdFilter
  • BinaryThresholdFilter
  • BrightnessFilter
  • ContrastFilter
  • InvertFilter
  • SharpenFilter
  • Blur Filters
    • GaussianBlurFilter
    • BilateralFilter
    • MedianBlurFilter

The order in which these filters are applied is based on their placement inside the ImageFilterCollection.

Adaptive Threshold Filter

AdaptiveThresholdFilter is one of the filters available in IronBarcode that applies Bradley Adaptive Threshold technique to the image, which automatically determines the threshold for binarizing an image. This filter is ideal for images with non-uniform illumination and varying background intensity levels.

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-adaptive-threshold.cs
using IronBarCode;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new AdaptiveThresholdFilter(0.9f),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Export file to disk
results.ExportFilterImagesToDisk("adaptiveThreshold_0.9.png");
Imports IronBarCode

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New AdaptiveThresholdFilter(0.9F)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("adaptiveThreshold_0.9.png")
VB   C#

Below are the outputs of applying the filter using different values.

Default Adaptive Threshold
0.9 Adaptive Threshold

The constructor also accepts additional parameters for configuration:

  • Upper: Upper (white) color for thresholding.
  • Lower: Lower (black) color for thresholding.
  • Threshold: Threshold limit (0.0-1.0) to consider for binarization.
  • Rectangle: Rectangle region to apply the processor on.

As seen in the output image above, the image is binarized to only have black and white colors. While it still does not seem to be ideal for barcode reading as filters need to be used in combinations. Users will need to experiment with the parameter sensitivity to achieve the best results.

Binary Threshold Filter

The BinaryThresholdFilter filters an image by splitting the pixels at the given threshold, where it is used to compare the luminance of a color component. Similar to the AdaptiveThresholdFilter, this filter can introduce new or unwanted noise if it is not used correctly. However, IronBarcode has set default values for the properties of the filter.

Similar to the AdaptiveThresholdFilter, the BinaryThresholdFilter accepts the same additional parameters for configuration.

  • Upper: Upper (white) color for thresholding.
  • Lower: Lower (black) color for thresholding.
  • Threshold: Threshold limit (0.0-1.0) to consider for binarization.
  • Rectangle: Rectangle region to apply the processor on.
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-binary-threshold.cs
using IronBarCode;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new BinaryThresholdFilter(0.9f)
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Export file to disk
results.ExportFilterImagesToDisk("binaryThreshold_0.9.png");
Imports IronBarCode

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New BinaryThresholdFilter(0.9F)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("binaryThreshold_0.9.png")
VB   C#

Below is the sample output of applying filters to the sample image.

Default Binary Threshold
0.9 Binary Threshold

Observing the output image above, we can see that the sample has been binarized into black and white color. However, it can be seen that this filter is clearly not suitable for this image due to barcode bars got eliminated as well as some new noise being introduced.

Brightness Filter

BrightnessFilter is another essential filter in the image filter collection in IronBarcode. As the name suggests, this filter adjusts the brightness of the barcode image. The input to this constructor can vary the Amount of brightness in the output image. The default value is 1, which leaves the image unchanged. A value of 0 will create a completely black image, while values above 1 will make the image brighter.

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-brightness.cs
using IronBarCode;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new BrightnessFilter(1.5f),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Export file to disk
results.ExportFilterImagesToDisk("brightness_1.5.png");
Imports IronBarCode

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New BrightnessFilter(1.5F)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("brightness_1.5.png")
VB   C#

Below is the output image after applying this filter to the sample input.

Default Brightness
1.5 Brightness

Contrast Filter

The ContrastFilter is used to adjust the level of contrast in an image. Image contrast refers to the difference in color intensity between various elements in an image. Increasing the level of contrast enhances the visibility of details, making the image appear vivid and striking, while reducing contrast makes the image appear softer and more subdued.

The default value is 1, which leaves the image unchanged. A value of 0 will create a completely gray image, while values above 1 will increase the image contrast.

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-contrast.cs
using IronBarCode;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new ContrastFilter(1.5f),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Export file to disk
results.ExportFilterImagesToDisk("contrast_1.5.png");
Imports IronBarCode

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New ContrastFilter(1.5F)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("contrast_1.5.png")
VB   C#

Applying this filter to the sample input will produce the image below.

Default Contrast
1.5 Contrast

Invert Filter

This filter is used to invert the colors inside an image, making the opposite colors, such as white becomes black and black becomes white. It's particularly useful when users are trying to read a barcode image with a background color. Unlike the BinaryThresholdFilter, this filter inverts the colors directly without the need to specify sensitivity. Moreover, this filter can be used with a CropRectangle to specify the location in the image that needs the color to be inverted, instead of inverting the colors for the entire image.

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-invert.cs
using IronBarCode;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new InvertFilter(),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Export file to disk
results.ExportFilterImagesToDisk("invert.png");
Imports IronBarCode

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New InvertFilter()}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("invert.png")
VB   C#

Output image below is the result of applying this filter to the sample input image.

Original image
Inverted

Sharpen Filter

The last image correction filter in IronBarcode is the SharpenFilter. This filter enhances the sharpness of an image and is very useful to be used with blurry images. Users can manipulate this filter to adjust the sharpness of an image by adjusting the Sigma value when instantiating the filter object. The default value is 3. Increase the sigma values to increase the image sharpness.

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-sharpen.cs
using IronBarCode;
using System;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new SharpenFilter(0.5f),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Export file to disk
results.ExportFilterImagesToDisk("sharpen_0.5.png");
Imports IronBarCode
Imports System

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New SharpenFilter(0.5F)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("sharpen_0.5.png")
VB   C#

Image below is the sharpened version of the sample input image.

Default Sharpen
0.5 Sharpen

Comparing the image above with the original image, it seems more sharper and definitely would help in barcode reading using IronBarcode. In most cases, SharpenFilter is always applied together with other filters in the ImageFilterCollection class.

Blur Filters

GaussianBlur Filters

The GaussianBlurFilter is used to apply a Gaussian blur to an image. This filter is commonly used to reduce noise in an image.

The filter works by averaging neighboring pixel values in the image using a Gaussian function. The method relies on two adjustable factors:

  • Kernel: a matrix used for averaging the pixels.
  • Sigma: a value that controls the intensity of the blur.

The default kernel size is 3x3 pixels, and the default Sigma value is 3.0, which produces a moderate blur. Increasing the Sigma value will result in a stronger blur effect. You can also customize the kernel to control the size of the neighborhood that the blur filter averages.

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-gaussianblur.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new GaussianBlurFilter(3, 3, 3.0f),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sharpen.webp", myOptionsExample);

// Export file to disk
results.ExportFilterImagesToDisk("gaussianBlur.png");
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New GaussianBlurFilter(3, 3, 3.0F)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sharpen.webp", myOptionsExample)

' Export file to disk
results.ExportFilterImagesToDisk("gaussianBlur.png")
VB   C#

Applying this filter to the sample input will produce the image below.

Default Sharpen
GaussianBlur image

Bilateral Filters

The BilateralFilter is used to smooth images while preserving edges. Unlike simple blur techniques, which affect all pixels uniformly, the Bilateral Filter takes both color differences and pixel distance into account, making it effective for edge-preserving smoothing.

The method relies on three adjustable factors:

  • NeighborhoodDiameter: Specifies the diameter of the pixel neighborhood used for filtering. A larger diameter includes more surrounding pixels in the filter. The default value is 5.
  • SigmaColor: This represents the color influence. It determines how much the color difference between neighboring pixels affects the filtering. A higher value means that pixels with different colors will influence each other more. The default value is 75.0.
  • SigmaSpace: This represents the spatial influence. It determines how much the distance between pixels affects the filtering. A higher value means that pixels that are farther apart will influence each other more. The default value is 75.0.
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-bilateral.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new BilateralFilter(5, 75, 75),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sharpen.webp", myOptionsExample);

// Export file to disk
results.ExportFilterImagesToDisk("bilateral.png");
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New BilateralFilter(5, 75, 75)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sharpen.webp", myOptionsExample)

' Export file to disk
results.ExportFilterImagesToDisk("bilateral.png")
VB   C#

Applying this filter to the sample input will produce the image below.

Default Sharpen
Bilateral image

MedianBlur Filters

The MedianBlurFilter is a filter used for reducing noise in an image by replacing each pixel’s value with the median value of the surrounding pixels. This filter is particularly effective at preserving edges while removing noise.

  • KernelSize: Defines the size of the neighborhood around each pixel used to calculate the median. The value must be an odd number greater than 0. The default value is 5.
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-medianblur.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new MedianBlurFilter(5),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sharpen.webp", myOptionsExample);

// Export file to disk
results.ExportFilterImagesToDisk("medianBlur.png");
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New MedianBlurFilter(5)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sharpen.webp", myOptionsExample)

' Export file to disk
results.ExportFilterImagesToDisk("medianBlur.png")
VB   C#

Applying this filter to the sample input will produce the image below.

Default Sharpen
MedianBlur image

Save Iterations

When applying multiple filters to the barcode, it can be difficult to view the output after each filter method. This feature allows saving the filtered image after each filter is applied, in the order they are processed. To enable this feature, first pass true to the ImageFilterCollection constructor. Then, use the ExportFilterImagesToDisk method to provide the path and name for the output images.

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-save-iterations.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new SharpenFilter(3.5f),
        new AdaptiveThresholdFilter(0.5f),
        new ContrastFilter(2)
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.webp", myOptionsExample);

// Export file to disk
results.ExportFilterImagesToDisk("filteredImage.png");
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {
		New SharpenFilter(3.5F),
		New AdaptiveThresholdFilter(0.5F),
		New ContrastFilter(2)
	}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.webp", myOptionsExample)

' Export file to disk
results.ExportFilterImagesToDisk("filteredImage.png")
VB   C#

The filters are applied in the order of the code, and the output images reflect the results of each iteration:

  • Sharpen -> After Sharpen
  • Sharpen + Adaptive Threshold -> After Adaptive Threshold
  • Sharpen + Adaptive Threshold + Contrast -> After Contrast
Sample image
1.5 Contrast
Sample image
1.5 Contrast

Apart from ImageFilters properties, users can also add other properties in BarcodeReaderOptions for more accurate reading, see this article for more information.

Hairil related to Save Iterations

Hairil Hasyimi Bin Omar

Software Engineer

Like all great engineers, Hairil is an avid learner. He’s refining his knowledge of C#, Python, and Java, using that knowledge to add value to team members across Iron Software. Hairil joined the Iron Software team from Universiti Teknologi MARA in Malaysia, where he graduated with a Bachelor's degree in Chemical and Process Engineering.