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

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.

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 Sharpen Filter

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.