How to use Image Correction Filters
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.
How to use Image Correction Filters
- Download the C# library to use image correction filters
- Explore all available image correction filters
- Configure each image correction filter with custom values
- Apply the filters to the imperfect image sample
- Retrieve the barcode value from the image with the help of filters
Get started with IronBarcode
Start using IronBarcode in your project today with a free trial.
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 System;
using IronBarCode;
namespace BarcodeReaderExample
{
class Program
{
static void Main(string[] args)
{
// Create an instance of BarcodeReaderOptions
// These options allow us to specify image processing filters to improve barcode reading accuracy
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
// Define the image filters to be applied in sequence
ImageFilters = new ImageFilterCollection()
{
new ImageFilter.Sharpen(3.5f), // Apply sharpen filter with intensity 3.5
new ImageFilter.Contrast(2) // Apply contrast filter with intensity 2
},
};
try
{
// Apply options and read the barcode from the specified image file
BarcodeResults results = BarcodeReader.Read("sample.png", options);
// Loop through the results and output the text from each detected barcode
foreach (BarcodeResult result in results)
{
Console.WriteLine(result.Text);
}
}
catch (Exception ex)
{
// Output error message if barcode reading fails
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
}
Imports System
Imports IronBarCode
Namespace BarcodeReaderExample
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Create an instance of BarcodeReaderOptions
' These options allow us to specify image processing filters to improve barcode reading accuracy
Dim options As New BarcodeReaderOptions() With {
.ImageFilters = New ImageFilterCollection() From {
New ImageFilter.Sharpen(3.5F),
New ImageFilter.Contrast(2)
}
}
Try
' Apply options and read the barcode from the specified image file
Dim results As BarcodeResults = BarcodeReader.Read("sample.png", options)
' Loop through the results and output the text from each detected barcode
For Each result As BarcodeResult In results
Console.WriteLine(result.Text)
Next result
Catch ex As Exception
' Output error message if barcode reading fails
Console.WriteLine($"An error occurred: {ex.Message}")
End Try
End Sub
End Class
End Namespace
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 issues due to using too many filters or using the 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;
// This code demonstrates how to configure a barcode reader to read a barcode from an image.
// It applies image filters to improve the accuracy of the barcode reading process.
// Configure barcode reader options
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
// Create a collection of image filters and apply them in sequence.
ImageFilters = new ImageFilterCollection(true)
{
// Apply an Adaptive Threshold filter with a threshold value of 0.9.
// This filter enhances the image to make the barcode more visible.
new AdaptiveThresholdFilter(0.9f),
},
};
// Use the configured options to read the barcode from the specified image file
BarcodeResults results = BarcodeReader.Read("sample.png", options);
// Export the processed images that have been filtered to the disk.
// This allows users to inspect or use the filtered images for further processing.
results.ExportFilterImagesToDisk("adaptiveThreshold_0.9.png");
Imports IronBarCode
' This code demonstrates how to configure a barcode reader to read a barcode from an image.
' It applies image filters to improve the accuracy of the barcode reading process.
' Configure barcode reader options
Private options As New BarcodeReaderOptions() With {
.ImageFilters = New ImageFilterCollection(True) From {New AdaptiveThresholdFilter(0.9F)}
}
' Use the configured options to read the barcode from the specified image file
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)
' Export the processed images that have been filtered to the disk.
' This allows users to inspect or use the filtered images for further processing.
results.ExportFilterImagesToDisk("adaptiveThreshold_0.9.png")
Below are the outputs of applying the filter using different values.

Default value

0.9 value
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) 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) for binarization.
- Rectangle: Rectangle region to apply the processor on.
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-binary-threshold.cs
// Import necessary libraries for barcode processing
using IronBarCode;
// Set up barcode reader options with specified image filters
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
// Choose which filters are to be applied (in order)
ImageFilters = new ImageFilterCollection(true)
{
new BinaryThresholdFilter(0.9f) // Applying a binary threshold to the image
}
};
// Read the barcode from an image file using specified options
BarcodeResults results = BarcodeReader.Read("sample.png", options);
// Check if any barcodes were found and print the result
if (results != null && results.Count > 0)
{
foreach (var result in results)
{
Console.WriteLine($"Barcode Found: {result.Text}"); // Display the barcode text
}
}
else
{
Console.WriteLine("No barcodes found."); // Inform the user if no barcodes were found
}
// Export the filtered images generated during barcode reading process to disk
results.ExportFilterImagesToDisk("binaryThreshold_0.9.png");
' Import necessary libraries for barcode processing
Imports IronBarCode
' Set up barcode reader options with specified image filters
Private options As New BarcodeReaderOptions() With {
.ImageFilters = New ImageFilterCollection(True) From {New BinaryThresholdFilter(0.9F)}
}
' Read the barcode from an image file using specified options
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)
' Check if any barcodes were found and print the result
If results IsNot Nothing AndAlso results.Count > 0 Then
For Each result In results
Console.WriteLine($"Barcode Found: {result.Text}") ' Display the barcode text
Next result
Else
Console.WriteLine("No barcodes found.") ' Inform the user if no barcodes were found
End If
' Export the filtered images generated during barcode reading process to disk
results.ExportFilterImagesToDisk("binaryThreshold_0.9.png")
Below is the sample output of applying filters to the sample image.

Default value

0.9 value
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 being 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;
// Create barcode reader options and specify image filters
BarcodeReaderOptions options = new BarcodeReaderOptions
{
// Initialize a collection of image filters
ImageFilters = new ImageFilterCollection(true)
{
// Adjust image brightness. 1.5 factor increases brightness by 50%
new BrightnessFilter(1.5f)
}
};
// Try to read the barcode from an image file using the specified options
BarcodeResults results = BarcodeReader.Read("sample.png", options);
// Check if any barcodes were successfully read before proceeding
if (results != null && results.Any())
{
// Export filtered images to disk with the filename "brightness_1.5.png"
results.ExportFilterImagesToDisk("brightness_1.5.png");
}
else
{
// Output an error message if no barcodes were detected
Console.WriteLine("No barcodes were detected in the image.");
}
Imports IronBarCode
' Create barcode reader options and specify image filters
Private options As New BarcodeReaderOptions With {
.ImageFilters = New ImageFilterCollection(True) From {New BrightnessFilter(1.5F)}
}
' Try to read the barcode from an image file using the specified options
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)
' Check if any barcodes were successfully read before proceeding
If results IsNot Nothing AndAlso results.Any() Then
' Export filtered images to disk with the filename "brightness_1.5.png"
results.ExportFilterImagesToDisk("brightness_1.5.png")
Else
' Output an error message if no barcodes were detected
Console.WriteLine("No barcodes were detected in the image.")
End If
Below is the output image after applying this filter to the sample input.

Default value

1.5 value
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
// Import the necessary library for barcode reading
using IronBarCode;
// Set the options for the barcode reader
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
// Choose which filters are to be applied (in order)
ImageFilters = new ImageFilterCollection(true)
{
// Apply a contrast filter with a strength of 1.5
new ContrastFilter(1.5f),
}
};
// Read the barcode from the specified image file using the options set
BarcodeResults results = BarcodeReader.Read("sample.png", options);
// Check if the barcode reading was successful and handle any errors
if (results != null && results.Barcodes.Count > 0)
{
// Export the filtered images used in the processing to a file on disk
results.ExportFilterImagesToDisk("contrast_1.5.png");
Console.WriteLine("Barcode reading and image export successful.");
}
else
{
// Handle the case when no barcodes are found or there is an error
Console.WriteLine("No barcodes found or there was an error in reading the image.");
}
' Import the necessary library for barcode reading
Imports IronBarCode
' Set the options for the barcode reader
Private options As New BarcodeReaderOptions() With {
.ImageFilters = New ImageFilterCollection(True) From {New ContrastFilter(1.5F)}
}
' Read the barcode from the specified image file using the options set
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)
' Check if the barcode reading was successful and handle any errors
If results IsNot Nothing AndAlso results.Barcodes.Count > 0 Then
' Export the filtered images used in the processing to a file on disk
results.ExportFilterImagesToDisk("contrast_1.5.png")
Console.WriteLine("Barcode reading and image export successful.")
Else
' Handle the case when no barcodes are found or there is an error
Console.WriteLine("No barcodes found or there was an error in reading the image.")
End If
Applying this filter to the sample input will produce the image below.

Default value

1.5 value
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;
// Initialize BarcodeReaderOptions with specific settings
// These options allow for customization of barcode reading behaviors.
BarcodeReaderOptions options = new BarcodeReaderOptions
{
// Specify image filters to be applied in order.
// This is useful for preprocessing the image before attempting to read barcodes.
ImageFilters = new ImageFilterCollection(true)
{
// Apply an inversion filter to the images.
// This step is helpful if the barcode is on a light background.
new InvertFilter()
}
};
// Read the barcode from the specified image using the defined options.
// The Read method attempts to decode any barcode present in the 'sample.png' file.
BarcodeResults results = BarcodeReader.Read("sample.png", options);
// Export the filtered images that were used to find barcodes to disk.
// This can be helpful for debugging purposes to ensure that the filters are being applied correctly.
results.ExportFilterImagesToDisk("invert.png");
Imports IronBarCode
' Initialize BarcodeReaderOptions with specific settings
' These options allow for customization of barcode reading behaviors.
Private options As New BarcodeReaderOptions With {
.ImageFilters = New ImageFilterCollection(True) From {New InvertFilter()}
}
' Read the barcode from the specified image using the defined options.
' The Read method attempts to decode any barcode present in the 'sample.png' file.
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)
' Export the filtered images that were used to find barcodes to disk.
' This can be helpful for debugging purposes to ensure that the filters are being applied correctly.
results.ExportFilterImagesToDisk("invert.png")
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;
// Create and configure options for barcode reading
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
// Enable image filters for optimized barcode scanning
ImageFilters = new ImageFilterCollection(true)
{
// Apply a sharpen filter to enhance image edges, improving barcode clarity
new SharpenFilter(0.5f),
},
};
try
{
// Attempt to read barcode(s) from the specified image file with the provided options
BarcodeResults results = BarcodeReader.Read("sample.png", options);
// Verify if any barcode(s) have been detected
if (results == null || results.Count == 0)
{
Console.WriteLine("No barcodes were detected in the image.");
}
else
{
// Save the processed image(s) with applied filters to disk
results.ExportFilterImagesToDisk("sharpen_0.5.png");
Console.WriteLine("Filtered image(s) exported successfully.");
}
}
catch (Exception ex)
{
// Catch and display any errors encountered during barcode processing
Console.WriteLine($"An error occurred while reading the barcode: {ex.Message}");
}
Imports IronBarCode
Imports System
' Create and configure options for barcode reading
Private options As New BarcodeReaderOptions() With {
.ImageFilters = New ImageFilterCollection(True) From {New SharpenFilter(0.5F)}
}
Try
' Attempt to read barcode(s) from the specified image file with the provided options
Dim results As BarcodeResults = BarcodeReader.Read("sample.png", options)
' Verify if any barcode(s) have been detected
If results Is Nothing OrElse results.Count = 0 Then
Console.WriteLine("No barcodes were detected in the image.")
Else
' Save the processed image(s) with applied filters to disk
results.ExportFilterImagesToDisk("sharpen_0.5.png")
Console.WriteLine("Filtered image(s) exported successfully.")
End If
Catch ex As Exception
' Catch and display any errors encountered during barcode processing
Console.WriteLine($"An error occurred while reading the barcode: {ex.Message}")
End Try
Image below is the sharpened version of the sample input image.

Default value

0.5 value
Comparing the image above with the original image, it seems sharper and would definitely 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
// Include the necessary namespace for working with IronBarCode.
using IronBarCode;
public class BarcodeReaderDemo
{
public static void Main()
{
// Create an instance of BarcodeReaderOptions to configure the reading process.
BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
// Initialize an image filter collection to apply specific filters to the image.
// This collection is set to active with 'true' as a parameter.
ImageFilters = new ImageFilterCollection(true)
{
// Applying a GaussianBlurFilter with specified parameters.
// The parameters (3, 3, 3.0f) refer to the kernel size and sigma value for the blur effect.
new GaussianBlurFilter(3, 3, 3.0f),
},
};
try
{
// Use the BarcodeReader class to read a barcode from the specified image file.
// The image file "sharpen.webp" is processed using the options specified in 'myOptionsExample'.
BarcodeResults results = BarcodeReader.Read("sharpen.webp", myOptionsExample);
// Check if any barcode was successfully read from the image.
if (results.Barcodes.Length > 0)
{
// Export the filtered image(s) to disk.
// The result of applying the Gaussian blur filter is saved as "gaussianBlur.png".
results.ExportFilterImagesToDisk("gaussianBlur.png");
// Optionally, output the results to the console.
Console.WriteLine("Barcodes successfully read and filtered image exported.");
// Display the content of the first barcode as an example.
Console.WriteLine("First barcode content: " + results.Barcodes[0].Text);
}
else
{
Console.WriteLine("No barcodes found in the image.");
}
}
catch (Exception ex)
{
// Handle any errors that occur during barcode reading and filtering.
Console.WriteLine("An error occurred: " + ex.Message);
}
}
}
' Include the necessary namespace for working with IronBarCode.
Imports IronBarCode
Public Class BarcodeReaderDemo
Public Shared Sub Main()
' Create an instance of BarcodeReaderOptions to configure the reading process.
Dim myOptionsExample As New BarcodeReaderOptions() With {
.ImageFilters = New ImageFilterCollection(True) From {New GaussianBlurFilter(3, 3, 3.0F)}
}
Try
' Use the BarcodeReader class to read a barcode from the specified image file.
' The image file "sharpen.webp" is processed using the options specified in 'myOptionsExample'.
Dim results As BarcodeResults = BarcodeReader.Read("sharpen.webp", myOptionsExample)
' Check if any barcode was successfully read from the image.
If results.Barcodes.Length > 0 Then
' Export the filtered image(s) to disk.
' The result of applying the Gaussian blur filter is saved as "gaussianBlur.png".
results.ExportFilterImagesToDisk("gaussianBlur.png")
' Optionally, output the results to the console.
Console.WriteLine("Barcodes successfully read and filtered image exported.")
' Display the content of the first barcode as an example.
Console.WriteLine("First barcode content: " & results.Barcodes(0).Text)
Else
Console.WriteLine("No barcodes found in the image.")
End If
Catch ex As Exception
' Handle any errors that occur during barcode reading and filtering.
Console.WriteLine("An error occurred: " & ex.Message)
End Try
End Sub
End Class
Applying this filter to the sample input will produce the image below.

Sharpen image

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;
// Define barcode reading options with specific image filtering settings.
BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
// Choose which filters are to be applied (in order).
// Using a bilateral filter to reduce noise while maintaining edges.
ImageFilters = new ImageFilterCollection(true)
{
new BilateralFilter(5, 75, 75) // Filter parameters: diameter, sigmaColor, sigmaSpace.
},
};
try
{
// Attempt to read the barcode from the specified image.
BarcodeResults results = BarcodeReader.Read("sharpen.webp", myOptionsExample);
if (results.Barcodes.Count > 0)
{
// If barcodes are found, export the filtered image(s) to disk.
results.ExportFilterImagesToDisk("bilateral.png");
Console.WriteLine("Barcodes detected and filtered images exported.");
}
else
{
// Handle the case where no barcodes are detected.
Console.WriteLine("No barcodes were found in the image.");
}
}
catch (Exception ex)
{
// Catch and display any errors that occur during the process.
Console.WriteLine($"An error occurred: {ex.Message}");
}
Imports IronBarCode
' Define barcode reading options with specific image filtering settings.
Private myOptionsExample As New BarcodeReaderOptions() With {
.ImageFilters = New ImageFilterCollection(True) From {New BilateralFilter(5, 75, 75)}
}
Try
' Attempt to read the barcode from the specified image.
Dim results As BarcodeResults = BarcodeReader.Read("sharpen.webp", myOptionsExample)
If results.Barcodes.Count > 0 Then
' If barcodes are found, export the filtered image(s) to disk.
results.ExportFilterImagesToDisk("bilateral.png")
Console.WriteLine("Barcodes detected and filtered images exported.")
Else
' Handle the case where no barcodes are detected.
Console.WriteLine("No barcodes were found in the image.")
End If
Catch ex As Exception
' Catch and display any errors that occur during the process.
Console.WriteLine($"An error occurred: {ex.Message}")
End Try
Applying this filter to the sample input produces the image below.

Sharpen image

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;
// Create BarcodeReaderOptions with specific settings
BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions
{
// Specify which image processing filters to apply (in order)
ImageFilters = new ImageFilterCollection(true)
{
new MedianBlurFilter(5) // Apply median blur with a filter size of 5
}
};
// Apply options and attempt to read the barcode from the given image file
BarcodeResult[] results = BarcodeReader.Read("sharpen.webp", myOptionsExample);
// Check if any barcodes were successfully detected
if (results != null && results.Length > 0)
{
// If barcodes were detected, export the filtered image to the disk
results.ExportFilterImagesToDisk("medianBlur.png");
}
else
{
// Handle the case where no barcodes were detected
Console.WriteLine("No barcodes were detected in the image.");
}
Imports IronBarCode
' Create BarcodeReaderOptions with specific settings
Private myOptionsExample As New BarcodeReaderOptions With {
.ImageFilters = New ImageFilterCollection(True) From {New MedianBlurFilter(5)}
}
' Apply options and attempt to read the barcode from the given image file
Private results() As BarcodeResult = BarcodeReader.Read("sharpen.webp", myOptionsExample)
' Check if any barcodes were successfully detected
If results IsNot Nothing AndAlso results.Length > 0 Then
' If barcodes were detected, export the filtered image to the disk
results.ExportFilterImagesToDisk("medianBlur.png")
Else
' Handle the case where no barcodes were detected
Console.WriteLine("No barcodes were detected in the image.")
End If
Applying this filter to the sample input produces the image below.

Sharpen image

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;
// Configure options for reading the barcode with specific image filters
BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
// Apply a sequence of image filters to preprocess the image, improving barcode detection accuracy
ImageFilters = new ImageFilterCollection(true)
{
new SharpenFilter(3.5f), // Enhances the details of the image
new AdaptiveThresholdFilter(0.5f), // Converts image to binary for better pattern detection
new ContrastFilter(2) // Increases the contrast of the image
}
};
// Read the barcode from the specified image file using the configured options
BarcodeResults results = BarcodeReader.Read("sample.webp", myOptionsExample);
// Check if any barcodes were successfully detected
if (results.BarcodeValues.Count > 0)
{
// Output the detected barcode values
foreach (var barcode in results.BarcodeValues)
{
Console.WriteLine("Detected Barcode: " + barcode);
}
}
else
{
Console.WriteLine("No barcodes detected.");
}
// Export the filtered images to disk; useful for debugging or processing analysis
results.ExportFilterImagesToDisk("filteredImages", ImageFormat.Png);
```
Imports IronBarCode
' Configure options for reading the barcode with specific image filters
Private myOptionsExample As New BarcodeReaderOptions() With {
.ImageFilters = New ImageFilterCollection(True) From {
New SharpenFilter(3.5F),
New AdaptiveThresholdFilter(0.5F),
New ContrastFilter(2)
}
}
' Read the barcode from the specified image file using the configured options
Private results As BarcodeResults = BarcodeReader.Read("sample.webp", myOptionsExample)
' Check if any barcodes were successfully detected
If results.BarcodeValues.Count > 0 Then
' Output the detected barcode values
For Each barcode In results.BarcodeValues
Console.WriteLine("Detected Barcode: " & barcode)
Next barcode
Else
Console.WriteLine("No barcodes detected.")
End If
' Export the filtered images to disk; useful for debugging or processing analysis
results.ExportFilterImagesToDisk("filteredImages", ImageFormat.Png)
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'```
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

After Sharpen

After Adaptive Threshold

After Contrast
Apart from the ImageFilters
properties, users can also add other properties to BarcodeReaderOptions
for more accurate reading; see this article for more information.
Frequently Asked Questions
What is the purpose of image correction filters in IronBarcode?
Image correction filters in IronBarcode are used to enhance images so that barcode reading accuracy is improved. They allow users to apply various filters to correct imperfections in barcode images programmatically.
How do I apply image correction filters using IronBarcode?
To apply image correction filters, you need to instantiate the ImageFilterCollection class, add the desired filters, set the ImageFilters property in BarcodeReaderOptions, and then pass this options object to the Read method along with the image.
What types of filters are available in IronBarcode for image correction?
IronBarcode offers several filters, including AdaptiveThresholdFilter, BinaryThresholdFilter, BrightnessFilter, ContrastFilter, InvertFilter, SharpenFilter, and various Blur Filters like GaussianBlurFilter, BilateralFilter, and MedianBlurFilter.
How does the AdaptiveThresholdFilter work?
The AdaptiveThresholdFilter uses the Bradley Adaptive Threshold technique to automatically determine the threshold for binarizing an image, making it suitable for images with non-uniform illumination and varying background intensity.
Can I save each step of image filtering?
Yes, by enabling iteration saving in the ImageFilterCollection constructor and using the ExportFilterImagesToDisk method, you can save the filtered image after each filter is applied.
What is the impact of using too many filters?
Using too many filters or the wrong filters can introduce new noise or performance issues. It's essential to understand how each filter works and apply only necessary filters to achieve the best results.
How can I adjust brightness using IronBarcode?
The BrightnessFilter can be used to adjust the brightness of an image. You can specify the Amount property to control the brightness level, with 1 being the default value that leaves the image unchanged.
How do I use the SharpenFilter in IronBarcode?
To use the SharpenFilter, create an instance with the desired Sigma value to adjust image sharpness, add it to the ImageFilterCollection, and apply it to the image for enhanced clarity.
What is the function of the InvertFilter?
The InvertFilter is used to invert the colors of an image, making white become black and vice versa. It is particularly useful for barcode images with background colors.
How do Gaussian and Bilateral Blur Filters differ?
GaussianBlurFilter applies a Gaussian blur to reduce noise, while BilateralFilter smooths images while preserving edges by considering both color differences and pixel distance.