# Using C# Image Correction Filters to Improve Barcode Decoding
[IronBarcode](https://ironsoftware.com/csharp/barcode/docs/) provides built-in image correction filters like `SharpenFilter` and `ContrastFilter` that programmatically enhance blurry or imperfect barcode images, improving reading accuracy without needing external image editing software or recapturing images.
Not every image is perfect, and poor image quality is one of the **main factors** preventing successful barcode reads in IronBarcode. Rather than recapturing images or using external image enhancement software, IronBarcode provides built-in filters that programmatically improve image quality. These filters help IronBarcode read difficult images and improve overall accuracy.
Continue reading to learn about available image correction filters in IronBarcode, their effects on images, and how to apply them. For more comprehensive barcode reading techniques, check our [Reading Barcodes tutorial](https://ironsoftware.com/csharp/barcode/tutorials/reading-barcodes/).
*as-heading:2(Quickstart: Apply Sharpen and Contrast Filters to Improve Barcode Readings)*
In just one step, apply IronBarcode's `SharpenFilter` and `ContrastFilter` using the `ImageFilterCollection` in `BarcodeReaderOptions`. This improves barcode scanning with minimal setup and zero need for external tools.
```cs
:title=Boost Barcode Readability — Try Image Correction Now
BarcodeResults results = IronBarCode.BarcodeReader.Read("input.png", new IronBarCode.BarcodeReaderOptions { ImageFilters = new IronBarCode.ImageFilterCollection() { new IronBarCode.SharpenFilter(3.5f), new IronBarCode.ContrastFilter(2.0f) } });
```
<div class="hsg-featured-snippet">
<h3>Minimal Workflow (5 steps)</h3>
<ol>
<li><a class="js-modal-open" data-modal-id="trial-license-after-download" href="https://www.nuget.org/packages/BarCode/">Download the C# library to use image correction filters</a></li>
<li>Explore all available image correction filters</li>
<li>Configure each filter with custom values</li>
<li>Apply filters to imperfect image samples</li>
<li>Retrieve barcode values from enhanced images</li>
</ol>
</div>
## How Do I Apply Image Filters to Improve Barcode Reading?
To apply filters, 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. For advanced installation options, visit our [NuGet packages guide](https://ironsoftware.com/csharp/barcode/get-started/advanced-installation-nuget/).
Use the image below as our sample image.
<div class="content-img-align-center">
<div class="center-image-wrapper">
<img src="/static-assets/barcode/how-to/image-correction/sample.webp" alt="Blurred barcode with number 4900203187590 showing poor image quality before filtering enhancement" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic; margin-bottom: 20px;">Sample image</p>
</div>
</div>
The image appears quite blurry. However, the brightness is acceptable, and the white and black colors are distinguishable. Therefore, apply at least the `SharpenFilter` and `ContrastFilter` to improve barcode readability. Refer to the code snippet below to apply filters to the image, read it, and display results on the console.
```csharp
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-apply-filter.cs
```
The code snippet above applies filters, reads the barcode, and exports the filtered image to disk. The comparison between the sample and filtered images is shown below.
<div class="competitors-section__wrapper-even-1">
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/sample.webp" alt="Blurry barcode image with number 4902030187590 demonstrating poor image quality" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">Sample image</p>
</div>
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/filteredSample.webp" alt="Barcode with improved readability after applying image filters, showing clear vertical lines and number 4902030187590" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">Filtered sample</p>
</div>
</div>
## What Image Correction Filters Are Available in IronBarcode?
IronBarcode offers multiple [image filters](https://ironsoftware.com/csharp/barcode/features/filters/) specifically designed for image correction. These filters assist in reading imperfect barcode images and enhancing reading accuracy. However, understand how these filters work to **select suitable filters** and **avoid performance issues** from using too many filters or using the wrong filter. Available filters include:
- `AdaptiveThresholdFilter`
- `BinaryThresholdFilter`
- `BrightnessFilter`
- `ContrastFilter`
- `InvertFilter`
- `SharpenFilter`
- `ErodeFilter`
- `DilateFilter`
- `HistogramEqualizationFilter`
- Blur Filters
- `GaussianBlurFilter`
- `BilateralFilter`
- `MedianBlurFilter`
The order in which filters are applied is based on their placement inside the `ImageFilterCollection`. For detailed API documentation on these filters, visit our [API Reference](https://ironsoftware.com/csharp/barcode/object-reference/api/).
## How Does the Adaptive Threshold Filter Work?
`AdaptiveThresholdFilter` is a filter available in IronBarcode that applies [Bradley Adaptive Threshold](https://saturncloud.io/blog/bradley-adaptive-thresholding-algorithm-a-powerful-tool-for-image-segmentation/#:~:text=The%20Bradley%20adaptive%20thresholding%20algorithm%2C%20proposed%20by%20Derek%20Bradley%20and,illumination%20or%20varying%20contrast%20levels.) 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.
```csharp
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-adaptive-threshold.cs
```
Below are the outputs of applying the filter using different values.
<div class="competitors-section__wrapper-even-1">
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/adaptiveThreshold.webp" alt="Vertical lines showing different adaptive threshold filter outputs with solid and dashed patterns" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">Default value</p>
</div>
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/adaptiveThreshold_0.9.webp" alt="Low-quality barcode image showing UPC number 902030187590 with significant visual distortion" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">0.9 value</p>
</div>
</div>
The constructor 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 shown in the output image above, the image is binarized to only have **black** and **white** colors. While it still does not seem ideal for barcode reading, filters need to be used in combinations. Experiment with the parameter sensitivity to achieve the best results.
## How Does the Binary Threshold Filter Work?
The `BinaryThresholdFilter` filters an image by splitting the pixels at the given threshold, comparing the luminance of a color component. Similar to the `AdaptiveThresholdFilter`, this filter can introduce new or unwanted noise if not used correctly. However, IronBarcode has set default values for the filter properties.
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.
```csharp
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-binary-threshold.cs
```
Below is the sample output of applying filters to the sample image.
<div class="competitors-section__wrapper-even-1">
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/binaryThreshold.webp" alt="Three examples of binary threshold filter outputs showing sparse, dotted, and dense vertical line patterns" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">Default value</p>
</div>
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/binaryThreshold_0.9.webp" alt="Barcode image processed with 0.9 binary threshold filter showing black and white contrast" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">0.9 value</p>
</div>
</div>
Observing the output image above, the sample has been binarized into black and white color. However, this filter is clearly not suitable for this image due to barcode bars being eliminated and new noise being introduced. For handling difficult barcode scenarios, refer to our [troubleshooting guide for unrecognized barcodes](https://ironsoftware.com/csharp/barcode/troubleshooting/barcode-not-recognized/).
## How Do I Adjust Image Brightness for Better Barcode Reading?
`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 varies the **Amount** of brightness in the output image. The default value is 1, which leaves the image unchanged. A value of 0 creates a completely black image, while values above 1 make the image brighter.
```csharp
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-brightness.cs
```
Below is the output image after applying this filter to the sample input.
<div class="competitors-section__wrapper-even-1">
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/sample.webp" alt="Blurry UPC barcode sample showing default brightness level before filter enhancement" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">Default value</p>
</div>
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/brightness_1.5.webp" alt="Blurry barcode with product number 4902030187590, demonstrating low brightness or poor image quality" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">1.5 value</p>
</div>
</div>
## How Do I Use the Contrast Filter to Enhance Barcode Images?
The `ContrastFilter` adjusts 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. For more details on barcode customization, see our guide on [customizing barcode styles](https://ironsoftware.com/csharp/barcode/how-to/customize-barcode-style/).
The default value is 1, which leaves the image unchanged. A value of 0 creates a completely gray image, while values above 1 increase the image contrast.
```csharp
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-contrast.cs
```
Applying this filter to the sample input produces the image below.
<div class="competitors-section__wrapper-even-1">
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/sample.webp" alt="Blurry barcode with number 4902030187590 demonstrating default contrast filter settings" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">Default value</p>
</div>
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/contrast_1.5.webp" alt="Blurry barcode with number 4902030187590 demonstrating low contrast image quality" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">1.5 value</p>
</div>
</div>
## When Should I Use the Invert Filter?
This filter inverts the colors inside an image, making opposite colors, such as white becomes black and black becomes white. It's particularly useful when reading a barcode image with a background color. Unlike the `BinaryThresholdFilter`, this filter inverts the colors directly without needing to specify sensitivity. Moreover, this filter can be used with a **CropRectangle** to specify the location in the image that needs the color inverted, instead of inverting colors for the entire image. Learn more about specifying crop regions in our [crop region tutorial](https://ironsoftware.com/csharp/barcode/how-to/set-crop-region/).
```csharp
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-invert.cs
```
The output image below is the result of applying this filter to the sample input image.
<div class="competitors-section__wrapper-even-1">
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/sample.webp" alt="Blurry UPC barcode showing number 480203187590 - original image before invert filter application" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">Original image</p>
</div>
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/invert.webp" alt="Blurry inverted barcode showing white bars on dark background with number sequence 4902030187590" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">Inverted</p>
</div>
</div>
## How Do I Fix Blurry Barcode Images with the Sharpen Filter?
IronBarcode provides a sharpening filter. This filter enhances the sharpness of an image and is very useful when dealing with blurry images. 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 value to increase the image sharpness. For other performance optimization options, check our guide on [reading speed options](https://ironsoftware.com/csharp/barcode/how-to/reading-speed-options/).
```csharp
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-sharpen.cs
```
The image below is the <em>sharpened</em> version of the sample input image.
<div class="competitors-section__wrapper-even-1">
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/sharpen.webp" alt="Blurry barcode image demonstrating unsharpened quality before applying sharpen filter" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">Default value</p>
</div>
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/sharpen_0.5.webp" alt="Blurred barcode example showing effects of image quality degradation" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">0.5 value</p>
</div>
</div>
Comparing the image above with the original image, it appears sharper and helps in barcode reading using IronBarcode. In most cases, `SharpenFilter` is always applied together with other filters in the `ImageFilterCollection` class.
## What Is the Erode Filter Used For?
The `ErodeFilter` removes tiny white noise and thickens barcode bars by removing pixels near the edge of shapes. This filter is best used in scenarios where the barcode background has lots of white speckles or if the barcode image is too low resolution or blurred, resulting in merged bars. The `ErodeFilter` makes bars thicker while removing white specks in the background. For more information on handling imperfect images, see our [imperfect barcode example](https://ironsoftware.com/csharp/barcode/examples/imperfect-barcode-with-image-correction/).
Increase the erosion's effect by inputting an integer representing `kernelSize` for the filter. The bigger the kernel size, the stronger the effect on the input image. Note that the `kernelSize` is a square and in this example would be a `5x5` kernel.
As an example, use the `ErodeFilter` with a larger kernel size to showcase the filter's effects.
```cs
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-erode.cs
```
<div class="competitors-section__wrapper-even-1">
<div class="competitors__card" style="width: 46%;">
<img src="/static-assets/barcode/how-to/image-correction/sample.webp" alt="Blurry barcode showing number 4002030187590 - example for erode filter demonstration" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">Original image</p>
</div>
<div class="competitors__card" style="width: 46%;">
<img src="/static-assets/barcode/how-to/image-correction/erodeFilter.webp" alt="Blurred barcode showing vertical black and white stripes with numerical code" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">Erode Filter Applied</p>
</div>
</div>
Comparing the input and output images above, some bars are visibly thicker due to the more aggressive nature of inputting a larger kernel size to filter. However, the white speckles in the overall picture have decreased. By nature of the erosion filter, the larger the kernel size, you might erase thin bars if applied too aggressively, as shown in the picture above. Test and refine the effect by changing the kernel size value input to the `ErodeFilter`.
## How Does the Dilate Filter Help with Barcode Reading?
The `DilateFilter` functions as the inverse of the `ErodeFilter`, operating by expanding bright regions - typically the background - through the addition of pixels to object boundaries. While this filter repairs damaged or faint barcodes by filling in small gaps or enhancing low-contrast areas, note that its effect on barcode bars differs from intuition. Since dilation enlarges bright spaces, it indirectly thins out dark elements such as black barcode bars (assuming a white background). This makes the filter particularly effective in scenarios where barcode bars appear overly thick or merged, but excessive use can degrade scan accuracy by excessively narrowing the bars.
Similar to above, increase the filter's effect by inputting an integer representing the `kernelSize` for the filter.
For the example below, use a larger kernel size to showcase the effects of the `DilateFilter`.
```cs
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-dilate.cs
```
<div class="competitors-section__wrapper-even-1">
<div class="competitors__card" style="width: 46%;">
<img src="/static-assets/barcode/how-to/image-correction/sample.webp" alt="Blurry barcode image showing number 4902030187590 - example for dilate filter processing" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">Original image</p>
</div>
<div class="competitors__card" style="width: 46%;">
<img src="/static-assets/barcode/how-to/image-correction/dilateFilter.webp" alt="Blurred barcode with numerical sequence below vertical bars" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">Dilation Filter Applied</p>
</div>
</div>
As shown in the image above, aggressive use of `DilateFilter` can potentially destroy the barcode structure, merging closely spaced bars and creating quiet zones in the barcodes. Test and refine the effects on the image by changing the kernel size value to be larger or smaller, depending on the input image.
## When Should I Use the HistogramEqualization Filter?
The `HistogramEqualizationFilter` enhances image contrast by redistributing pixel intensities to improve clarity. It is most commonly used when the barcode has either low contrast, such as faded or washed out images, or images with uneven lighting, such as dark shadows or bright glare. By analyzing the image histogram, which is the distribution of pixel brightness, it redistributes the pixel values by boosting contrast by stretching the intensity range, where dark pixels become darker and light pixels become lighter.
```cs
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-histogram-equalization-filter.cs
```
<div class="competitors-section__wrapper-even-1">
<div class="competitors__card" style="width: 46%;">
<img src="/static-assets/barcode/how-to/image-correction/sample.webp" alt="Blurry barcode with number 4902030187590 used as test image for histogram equalization filter" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">Original image</p>
</div>
<div class="competitors__card" style="width: 46%;">
<img src="/static-assets/barcode/how-to/image-correction/histogramEqualizationFilter.webp" alt="Barcode with vertical black and white stripes showing number 4902030187590" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">Histogram Equalization Filter Applied</p>
</div>
</div>
As shown in the image above, the black bars are visibly darker, and the spaces are visibly brighter compared to the original image.
## Which Blur Filters Can Help with Barcode Noise Reduction?
### How Does GaussianBlur Filter Reduce Image Noise?
The `GaussianBlurFilter` applies a Gaussian blur to an image. This filter commonly reduces noise in an image. For a comprehensive guide on dealing with imperfect barcodes, refer to our [image orientation correction tutorial](https://ironsoftware.com/csharp/barcode/how-to/image-orientation-correction/).
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 controlling the blur intensity.
The default `kernel` size is `3x3` pixels, and the default `Sigma` value is `3.0`, producing a moderate blur. Increasing the `Sigma` value results in a stronger blur effect. You can also customize the `kernel` to control the size of the neighborhood that the blur filter averages.
```csharp
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-gaussianblur.cs
```
Applying this filter to the sample input produces the image below.
<div class="competitors-section__wrapper-even-1">
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/sharpen.webp" alt="Blurry barcode with number 4902030187590 demonstrating poor image quality" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">Sharpen image</p>
</div>
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/gaussianBlur.webp" alt="Barcode image with Gaussian blur filter applied, showing blurred vertical lines and distorted numbers" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">GaussianBlur image</p>
</div>
</div>
### When Should I Use the Bilateral Filter?
The `BilateralFilter` smooths images while preserving edges. Unlike simple blur techniques affecting 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`: Diameter of pixel neighborhood (default: 5).
- `SigmaColor`: Color influence determining color difference impact (default: 75.0).
- `SigmaSpace`: Spatial influence determining distance impact (default: 75.0).
```csharp
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-bilateral.cs
```
Applying this filter to the sample input produces the image below.
<div class="competitors-section__wrapper-even-1">
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/sharpen.webp" alt="Blurred barcode demonstrating poor image quality with vertical lines and numbers 4902030187590" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">Sharpen image</p>
</div>
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/bilateral.webp" alt="Blurred barcode with numbers 4902030187590 demonstrating poor image quality" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">Bilateral image</p>
</div>
</div>
### What Makes MedianBlur Filter Different for Noise Reduction?
The `MedianBlurFilter` reduces noise in an image by replacing each pixel's value with the median value of the surrounding pixels. This filter particularly excels at preserving edges while removing noise. To explore more about barcode reading settings, visit our [barcode reader settings guide](https://ironsoftware.com/csharp/barcode/examples/barcode-reader-settings-csharp/).
- `KernelSize`: Size of neighborhood for median calculation (must be odd, default: 5).
```csharp
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-medianblur.cs
```
Applying this filter to the sample input produces the image below.
<div class="competitors-section__wrapper-even-1">
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/sharpen.webp" alt="Blurry barcode example showing poor image quality with digital artifacts and reduced readability" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">Sharpen image</p>
</div>
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/medianBlur.webp" alt="Barcode with median blur filter applied showing blurred vertical lines and number 4902030187590" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">MedianBlur image</p>
</div>
</div>
## How Can I Save Filtered Images at Each Processing Step?
When applying multiple filters to the barcode, viewing the output after each filter method can be difficult. 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. For more examples on saving barcodes, see our [convert barcode to image example](https://ironsoftware.com/csharp/barcode/examples/csharp-convert-barcode-to-image/).
```csharp
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-save-iterations.cs
```
The filters are applied in the order of the code, and the output images reflect the results of each iteration:
- `Sharpen` -> After `Sharpen`
- `Sharpen` + `AdaptiveThreshold` -> After `AdaptiveThreshold`
- `Sharpen` + `AdaptiveThreshold` + `Contrast` -> After `Contrast`
<div class="competitors-section__wrapper-even-1" style="margin-bottom: 25px;">
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/sample.webp" alt="Blurry barcode with number 4902030187590" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">Sample image</p>
</div>
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/filteredImage-1.webp" alt="Blurry UPC barcode showing number 4902030187590" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">After Sharpen</p>
</div>
</div>
<div class="competitors-section__wrapper-even-1">
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/filteredImage-2.webp" alt="Degraded barcode example showing poor image quality with number 9020301875905" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">After Adaptive Threshold</p>
</div>
<div class="competitors__card" style="width: 48%;">
<img src="/static-assets/barcode/how-to/image-correction/filteredImage-3.webp" alt="Heavily pixelated barcode with UPC number 902030187590" class="img-responsive add-shadow" />
<p class="competitors__download-link" style="color: #181818; font-style: italic;">After Contrast</p>
</div>
</div>
Apart from the `ImageFilters` properties, add other properties to `BarcodeReaderOptions` for more accurate reading; see this [article](https://ironsoftware.com/csharp/barcode/how-to/read-barcodes-from-images/#setting-barcode-reader-options) for more information.
IronBarcode provides built-in image correction filters like SharpenFilter and ContrastFilter that programmatically enhance blurry or imperfect barcode images, improving reading accuracy without needing external image editing software or recapturing images.
Not every image is perfect, and poor image quality is one of the main factors preventing successful barcode reads in IronBarcode. Rather than recapturing images or using external image enhancement software, IronBarcode provides built-in filters that programmatically improve image quality. These filters help IronBarcode read difficult images and improve overall accuracy.
Continue reading to learn about available image correction filters in IronBarcode, their effects on images, and how to apply them. For more comprehensive barcode reading techniques, check our Reading Barcodes tutorial.
Quickstart: Apply Sharpen and Contrast Filters to Improve Barcode Readings
In just one step, apply IronBarcode's SharpenFilter and ContrastFilter using the ImageFilterCollection in BarcodeReaderOptions. This improves barcode scanning with minimal setup and zero need for external tools.
1Install IronBarcode with NuGet Package Manager
PM > Install-Package BarCode
Install-Package BarCode
2Copy and run this code snippet.
BarcodeResults results = IronBarCode.BarcodeReader.Read("input.png", new IronBarCode.BarcodeReaderOptions { ImageFilters = new IronBarCode.ImageFilterCollection() { new IronBarCode.SharpenFilter(3.5f), new IronBarCode.ContrastFilter(2.0f) } });
BarcodeResults results = IronBarCode.BarcodeReader.Read("input.png", new IronBarCode.BarcodeReaderOptions { ImageFilters = new IronBarCode.ImageFilterCollection() { new IronBarCode.SharpenFilter(3.5f), new IronBarCode.ContrastFilter(2.0f) } });
C#
3Deploy to test on your live environment
Start using IronBarcode in your project today with a free trial
How Do I Apply Image Filters to Improve Barcode Reading?
To apply filters, 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. For advanced installation options, visit our NuGet packages guide.
Use the image below as our sample image.
Sample image
The image appears quite blurry. However, the brightness is acceptable, and the white and black colors are distinguishable. Therefore, apply at least the SharpenFilter and ContrastFilter to improve barcode readability. Refer to the code snippet below to apply filters to the image, read it, and display results on the console.
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 barcodeBarcodeResults results = BarcodeReader.Read("sample.png", options);// Write the result value to consoleforeach (BarcodeResult result in results){Console.WriteLine(result.Text);}
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);
}
ImportsIronBarCodeImportsSystemPrivate options As New BarcodeReaderOptions() With { .ImageFilters = New ImageFilterCollection() From { New SharpenFilter(3.5F), New ContrastFilter(2) }}' Apply options and read the barcodePrivate results AsBarcodeResults = BarcodeReader.Read("sample.png", options)' Write the result value to consoleFor Each result AsBarcodeResultIn resultsConsole.WriteLine(result.Text)Next result
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
The code snippet above applies filters, reads the barcode, and exports the filtered image to disk. The comparison between the sample and filtered images is shown below.
Sample image
Filtered sample
What Image Correction Filters Are Available in IronBarcode?
IronBarcode offers multiple image filters specifically designed for image correction. These filters assist in reading imperfect barcode images and enhancing reading accuracy. However, understand how these filters work to select suitable filters and avoid performance issues from using too many filters or using the wrong filter. Available filters include:
AdaptiveThresholdFilter
BinaryThresholdFilter
BrightnessFilter
ContrastFilter
InvertFilter
SharpenFilter
ErodeFilter
DilateFilter
HistogramEqualizationFilter
Blur Filters
GaussianBlurFilter
BilateralFilter
MedianBlurFilter
The order in which filters are applied is based on their placement inside the ImageFilterCollection. For detailed API documentation on these filters, visit our API Reference.
How Does the Adaptive Threshold Filter Work?
AdaptiveThresholdFilter is a filter 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.
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 barcodeBarcodeResults results = BarcodeReader.Read("sample.png", options);// Export file to diskresults.ExportFilterImagesToDisk("adaptiveThreshold_0.9.png");
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");
ImportsIronBarCodePrivate options As New BarcodeReaderOptions() With { .ImageFilters = New ImageFilterCollection(True) From {New AdaptiveThresholdFilter(0.9F)}}' Apply options and read the barcodePrivate results AsBarcodeResults = BarcodeReader.Read("sample.png", options)' Export file to diskresults.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")
Below are the outputs of applying the filter using different values.
Default value
0.9 value
The constructor 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 shown in the output image above, the image is binarized to only have black and white colors. While it still does not seem ideal for barcode reading, filters need to be used in combinations. Experiment with the parameter sensitivity to achieve the best results.
How Does the Binary Threshold Filter Work?
The BinaryThresholdFilter filters an image by splitting the pixels at the given threshold, comparing the luminance of a color component. Similar to the AdaptiveThresholdFilter, this filter can introduce new or unwanted noise if not used correctly. However, IronBarcode has set default values for the filter properties.
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.
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 barcodeBarcodeResults results = BarcodeReader.Read("sample.png", options);// Export file to diskresults.ExportFilterImagesToDisk("binaryThreshold_0.9.png");
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");
ImportsIronBarCodePrivate options As New BarcodeReaderOptions() With { .ImageFilters = New ImageFilterCollection(True) From {New BinaryThresholdFilter(0.9F)}}' Apply options and read the barcodePrivate results AsBarcodeResults = BarcodeReader.Read("sample.png", options)' Export file to diskresults.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")
Below is the sample output of applying filters to the sample image.
Default value
0.9 value
Observing the output image above, the sample has been binarized into black and white color. However, this filter is clearly not suitable for this image due to barcode bars being eliminated and new noise being introduced. For handling difficult barcode scenarios, refer to our troubleshooting guide for unrecognized barcodes.
How Do I Adjust Image Brightness for Better Barcode Reading?
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 varies the Amount of brightness in the output image. The default value is 1, which leaves the image unchanged. A value of 0 creates a completely black image, while values above 1 make the image brighter.
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 barcodeBarcodeResults results = BarcodeReader.Read("sample.png", options);// Export file to diskresults.ExportFilterImagesToDisk("brightness_1.5.png");
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");
ImportsIronBarCodePrivate options As New BarcodeReaderOptions() With { .ImageFilters = New ImageFilterCollection(True) From {New BrightnessFilter(1.5F)}}' Apply options and read the barcodePrivate results AsBarcodeResults = BarcodeReader.Read("sample.png", options)' Export file to diskresults.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")
Below is the output image after applying this filter to the sample input.
Default value
1.5 value
How Do I Use the Contrast Filter to Enhance Barcode Images?
The ContrastFilter adjusts 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. For more details on barcode customization, see our guide on customizing barcode styles.
The default value is 1, which leaves the image unchanged. A value of 0 creates a completely gray image, while values above 1 increase the image contrast.
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 barcodeBarcodeResults results = BarcodeReader.Read("sample.png", options);// Export file to diskresults.ExportFilterImagesToDisk("contrast_1.5.png");
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");
ImportsIronBarCodePrivate options As New BarcodeReaderOptions() With { .ImageFilters = New ImageFilterCollection(True) From {New ContrastFilter(1.5F)}}' Apply options and read the barcodePrivate results AsBarcodeResults = BarcodeReader.Read("sample.png", options)' Export file to diskresults.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")
Applying this filter to the sample input produces the image below.
Default value
1.5 value
When Should I Use the Invert Filter?
This filter inverts the colors inside an image, making opposite colors, such as white becomes black and black becomes white. It's particularly useful when reading a barcode image with a background color. Unlike the BinaryThresholdFilter, this filter inverts the colors directly without needing to specify sensitivity. Moreover, this filter can be used with a CropRectangle to specify the location in the image that needs the color inverted, instead of inverting colors for the entire image. Learn more about specifying crop regions in our crop region tutorial.
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 barcodeBarcodeResults results = BarcodeReader.Read("sample.png", options);// Export file to diskresults.ExportFilterImagesToDisk("invert.png");
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");
ImportsIronBarCodePrivate options As New BarcodeReaderOptions() With { .ImageFilters = New ImageFilterCollection(True) From {New InvertFilter()}}' Apply options and read the barcodePrivate results AsBarcodeResults = BarcodeReader.Read("sample.png", options)' Export file to diskresults.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")
The output image below is the result of applying this filter to the sample input image.
Original image
Inverted
How Do I Fix Blurry Barcode Images with the Sharpen Filter?
IronBarcode provides a sharpening filter. This filter enhances the sharpness of an image and is very useful when dealing with blurry images. 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 value to increase the image sharpness. For other performance optimization options, check our guide on reading speed options.
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 barcodeBarcodeResults results = BarcodeReader.Read("sample.png", options);// Export file to diskresults.ExportFilterImagesToDisk("sharpen_0.5.png");
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");
ImportsIronBarCodeImportsSystemPrivate options As New BarcodeReaderOptions() With { .ImageFilters = New ImageFilterCollection(True) From {New SharpenFilter(0.5F)}}' Apply options and read the barcodePrivate results AsBarcodeResults = BarcodeReader.Read("sample.png", options)' Export file to diskresults.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")
The 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 appears sharper and helps in barcode reading using IronBarcode. In most cases, SharpenFilter is always applied together with other filters in the ImageFilterCollection class.
What Is the Erode Filter Used For?
The ErodeFilter removes tiny white noise and thickens barcode bars by removing pixels near the edge of shapes. This filter is best used in scenarios where the barcode background has lots of white speckles or if the barcode image is too low resolution or blurred, resulting in merged bars. The ErodeFilter makes bars thicker while removing white specks in the background. For more information on handling imperfect images, see our imperfect barcode example.
Increase the erosion's effect by inputting an integer representing kernelSize for the filter. The bigger the kernel size, the stronger the effect on the input image. Note that the kernelSize is a square and in this example would be a 5x5 kernel.
As an example, use the ErodeFilter with a larger kernel size to showcase the filter's effects.
using IronBarCode;BarcodeReaderOptions options = new BarcodeReaderOptions(){ // Choose which filters are to be applied (in order)ImageFilters = new ImageFilterCollection(true) { new ErodeFilter(5), },};// Apply options and read the barcodeBarcodeResults results = BarcodeReader.Read("sample.png", options);// Export file to diskresults.ExportFilterImagesToDisk("erodeFilter.jpg");
using IronBarCode;
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
// Choose which filters are to be applied (in order)
ImageFilters = new ImageFilterCollection(true) {
new ErodeFilter(5),
},
};
// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);
// Export file to disk
results.ExportFilterImagesToDisk("erodeFilter.jpg");
ImportsIronBarCodeDim options As New BarcodeReaderOptions() With { .ImageFilters = New ImageFilterCollection(True) From { New ErodeFilter(5) }}' Apply options and read the barcodeDim results AsBarcodeResults = BarcodeReader.Read("sample.png", options)' Export file to diskresults.ExportFilterImagesToDisk("erodeFilter.jpg")
Imports IronBarCode
Dim options As New BarcodeReaderOptions() With {
.ImageFilters = New ImageFilterCollection(True) From {
New ErodeFilter(5)
}
}
' Apply options and read the barcode
Dim results As BarcodeResults = BarcodeReader.Read("sample.png", options)
' Export file to disk
results.ExportFilterImagesToDisk("erodeFilter.jpg")
Original image
Erode Filter Applied
Comparing the input and output images above, some bars are visibly thicker due to the more aggressive nature of inputting a larger kernel size to filter. However, the white speckles in the overall picture have decreased. By nature of the erosion filter, the larger the kernel size, you might erase thin bars if applied too aggressively, as shown in the picture above. Test and refine the effect by changing the kernel size value input to the ErodeFilter.
How Does the Dilate Filter Help with Barcode Reading?
The DilateFilter functions as the inverse of the ErodeFilter, operating by expanding bright regions - typically the background - through the addition of pixels to object boundaries. While this filter repairs damaged or faint barcodes by filling in small gaps or enhancing low-contrast areas, note that its effect on barcode bars differs from intuition. Since dilation enlarges bright spaces, it indirectly thins out dark elements such as black barcode bars (assuming a white background). This makes the filter particularly effective in scenarios where barcode bars appear overly thick or merged, but excessive use can degrade scan accuracy by excessively narrowing the bars.
Similar to above, increase the filter's effect by inputting an integer representing the kernelSize for the filter.
For the example below, use a larger kernel size to showcase the effects of the DilateFilter.
using IronBarCode;BarcodeReaderOptions options = new BarcodeReaderOptions(){ // Choose which filters are to be applied (in order)ImageFilters = new ImageFilterCollection(true) { new DilateFilter(5), },};// Apply options and read the barcodeBarcodeResults results = BarcodeReader.Read("sample.png", options);// Export file to diskresults.ExportFilterImagesToDisk("dilateFilter.jpg");
using IronBarCode;
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
// Choose which filters are to be applied (in order)
ImageFilters = new ImageFilterCollection(true) {
new DilateFilter(5),
},
};
// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);
// Export file to disk
results.ExportFilterImagesToDisk("dilateFilter.jpg");
ImportsIronBarCodeDim options As New BarcodeReaderOptions() With { .ImageFilters = New ImageFilterCollection(True) From { New DilateFilter(5) }}' Apply options and read the barcodeDim results AsBarcodeResults = BarcodeReader.Read("sample.png", options)' Export file to diskresults.ExportFilterImagesToDisk("dilateFilter.jpg")
Imports IronBarCode
Dim options As New BarcodeReaderOptions() With {
.ImageFilters = New ImageFilterCollection(True) From {
New DilateFilter(5)
}
}
' Apply options and read the barcode
Dim results As BarcodeResults = BarcodeReader.Read("sample.png", options)
' Export file to disk
results.ExportFilterImagesToDisk("dilateFilter.jpg")
Original image
Dilation Filter Applied
As shown in the image above, aggressive use of DilateFilter can potentially destroy the barcode structure, merging closely spaced bars and creating quiet zones in the barcodes. Test and refine the effects on the image by changing the kernel size value to be larger or smaller, depending on the input image.
When Should I Use the HistogramEqualization Filter?
The HistogramEqualizationFilter enhances image contrast by redistributing pixel intensities to improve clarity. It is most commonly used when the barcode has either low contrast, such as faded or washed out images, or images with uneven lighting, such as dark shadows or bright glare. By analyzing the image histogram, which is the distribution of pixel brightness, it redistributes the pixel values by boosting contrast by stretching the intensity range, where dark pixels become darker and light pixels become lighter.
using IronBarCode;BarcodeReaderOptions options = new BarcodeReaderOptions(){ // Choose which filters are to be applied (in order)ImageFilters = new ImageFilterCollection(true) { new HistogramEqualizationFilter(), },};// Apply options and read the barcodeBarcodeResults results = BarcodeReader.Read("sample.png", options);// Export file to diskresults.ExportFilterImagesToDisk("histogramEqualizationFilter.jpg");
using IronBarCode;
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
// Choose which filters are to be applied (in order)
ImageFilters = new ImageFilterCollection(true) {
new HistogramEqualizationFilter(),
},
};
// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);
// Export file to disk
results.ExportFilterImagesToDisk("histogramEqualizationFilter.jpg");
ImportsIronBarCodeDim options As New BarcodeReaderOptions() With { .ImageFilters = New ImageFilterCollection(True) From { New HistogramEqualizationFilter() }}' Apply options and read the barcodeDim results AsBarcodeResults = BarcodeReader.Read("sample.png", options)' Export file to diskresults.ExportFilterImagesToDisk("histogramEqualizationFilter.jpg")
Imports IronBarCode
Dim options As New BarcodeReaderOptions() With {
.ImageFilters = New ImageFilterCollection(True) From {
New HistogramEqualizationFilter()
}
}
' Apply options and read the barcode
Dim results As BarcodeResults = BarcodeReader.Read("sample.png", options)
' Export file to disk
results.ExportFilterImagesToDisk("histogramEqualizationFilter.jpg")
Original image
Histogram Equalization Filter Applied
As shown in the image above, the black bars are visibly darker, and the spaces are visibly brighter compared to the original image.
Which Blur Filters Can Help with Barcode Noise Reduction?
How Does GaussianBlur Filter Reduce Image Noise?
The GaussianBlurFilter applies a Gaussian blur to an image. This filter commonly reduces noise in an image. For a comprehensive guide on dealing with imperfect barcodes, refer to our image orientation correction tutorial.
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 controlling the blur intensity.
The default kernel size is 3x3 pixels, and the default Sigma value is 3.0, producing a moderate blur. Increasing the Sigma value results in a stronger blur effect. You can also customize the kernel to control the size of the neighborhood that the blur filter averages.
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 barcodeBarcodeResults results = BarcodeReader.Read("sharpen.webp", myOptionsExample);// Export file to diskresults.ExportFilterImagesToDisk("gaussianBlur.png");
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");
ImportsIronBarCodePrivate myOptionsExample As New BarcodeReaderOptions() With { .ImageFilters = New ImageFilterCollection(True) From {New GaussianBlurFilter(3, 3, 3.0F)}}' Apply options and read the barcodePrivate results AsBarcodeResults = BarcodeReader.Read("sharpen.webp", myOptionsExample)' Export file to diskresults.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")
Applying this filter to the sample input produces the image below.
Sharpen image
GaussianBlur image
When Should I Use the Bilateral Filter?
The BilateralFilter smooths images while preserving edges. Unlike simple blur techniques affecting 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: Diameter of pixel neighborhood (default: 5).
SigmaColor: Color influence determining color difference impact (default: 75.0).
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 barcodeBarcodeResults results = BarcodeReader.Read("sharpen.webp", myOptionsExample);// Export file to diskresults.ExportFilterImagesToDisk("bilateral.png");
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");
ImportsIronBarCodePrivate myOptionsExample As New BarcodeReaderOptions() With { .ImageFilters = New ImageFilterCollection(True) From {New BilateralFilter(5, 75, 75)}}' Apply options and read the barcodePrivate results AsBarcodeResults = BarcodeReader.Read("sharpen.webp", myOptionsExample)' Export file to diskresults.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")
Applying this filter to the sample input produces the image below.
Sharpen image
Bilateral image
What Makes MedianBlur Filter Different for Noise Reduction?
The MedianBlurFilter reduces noise in an image by replacing each pixel's value with the median value of the surrounding pixels. This filter particularly excels at preserving edges while removing noise. To explore more about barcode reading settings, visit our barcode reader settings guide.
KernelSize: Size of neighborhood for median calculation (must be odd, default: 5).
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 barcodeBarcodeResults results = BarcodeReader.Read("sharpen.webp", myOptionsExample);// Export file to diskresults.ExportFilterImagesToDisk("medianBlur.png");
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");
ImportsIronBarCodePrivate myOptionsExample As New BarcodeReaderOptions() With { .ImageFilters = New ImageFilterCollection(True) From {New MedianBlurFilter(5)}}' Apply options and read the barcodePrivate results AsBarcodeResults = BarcodeReader.Read("sharpen.webp", myOptionsExample)' Export file to diskresults.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")
Applying this filter to the sample input produces the image below.
Sharpen image
MedianBlur image
How Can I Save Filtered Images at Each Processing Step?
When applying multiple filters to the barcode, viewing the output after each filter method can be difficult. 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. For more examples on saving barcodes, see our convert barcode to image example.
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 barcodeBarcodeResults results = BarcodeReader.Read("sample.webp", myOptionsExample);// Export file to diskresults.ExportFilterImagesToDisk("filteredImage.png");
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");
ImportsIronBarCodePrivate 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 barcodePrivate results AsBarcodeResults = BarcodeReader.Read("sample.webp", myOptionsExample)' Export file to diskresults.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")
The filters are applied in the order of the code, and the output images reflect the results of each iteration:
Sharpen -> After Sharpen
Sharpen + AdaptiveThreshold -> After AdaptiveThreshold
Sharpen + AdaptiveThreshold + Contrast -> After Contrast
Sample image
After Sharpen
After Adaptive Threshold
After Contrast
Apart from the ImageFilters properties, add other properties to BarcodeReaderOptions for more accurate reading; see this article for more information.
Frequently Asked Questions
What image correction filters are available in IronBarcode?
IronBarcode offers a variety of image correction filters such as SharpenFilter, ContrastFilter, AdaptiveThresholdFilter, BinaryThresholdFilter, BrightnessFilter, InvertFilter, ErodeFilter, DilateFilter, HistogramEqualizationFilter, and several Blur filters including GaussianBlurFilter, BilateralFilter, and MedianBlurFilter. These filters enhance barcode reading accuracy by improving image quality.
How do I apply the Sharpen and Contrast filters in IronBarcode?
To apply the Sharpen and Contrast filters in IronBarcode, instantiate the ImageFilterCollection class with these filters and assign it to the ImageFilters property of a BarcodeReaderOptions object. Pass this options object to the Read method along with the image to enhance barcode readability.
What is the purpose of the Adaptive Threshold Filter?
The Adaptive Threshold Filter uses the Bradley Adaptive Thresholding technique to automatically determine thresholds for binarizing an image, which is particularly useful for images with non-uniform illumination and varying background intensities.
How does the Invert Filter work?
The Invert Filter inverts the colors of an image, converting white to black and vice versa, which is beneficial for barcode images with a background color. It can be applied to the entire image or specified sections using CropRectangle.
What is the effect of the Erode Filter on barcode images?
The Erode Filter thickens barcode bars by removing pixels near the edges, which reduces white noise in the background. It is useful for low-resolution or blurred images but may require careful adjustment to avoid erasing thin bars.
When should I use the HistogramEqualization Filter?
The HistogramEqualization Filter enhances contrast by redistributing pixel intensities, making it suitable for barcodes with low contrast or uneven lighting. This filter stretches the intensity range to improve readability.
What does the GaussianBlur Filter do?
The GaussianBlur Filter reduces noise in an image by averaging pixel values using a Gaussian function. It is configurable via kernel size and sigma value, and it applies a moderate blur to help reduce image noise.
How does the Bilateral Filter differ from other blur filters?
The Bilateral Filter reduces noise while preserving edges and takes into account both color differences and pixel distance. It smooths images by considering neighborhood diameter, sigma color, and sigma space parameters.
What is the primary use of the MedianBlur Filter?
The MedianBlur Filter excels at preserving edges while minimizing noise by replacing each pixel's value with the median of its neighbors, making it effective for noise reduction with edge preservation.
How can I save images after each filter is applied in IronBarcode?
In IronBarcode, enable exporting images after each filter step by passing true to the ImageFilterCollection constructor, then use the ExportFilterImagesToDisk method to save the output images at each processing step.