How to use Image Correction Filters For Barcode Using C#

使用 C## 图像校正过滤器改进条形码解码。

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronBarcode提供内置图像校正过滤器,如SharpenFilterContrastFilter,可通过编程增强模糊或不完美的条形码图像,提高读取准确性,而无需外部图像编辑软件或重新捕获图像。

并非每张图像都是完美的,图像质量差是妨碍 IronBarcode 成功读取条码的主要因素之一。 IronBarcode 不需要重新捕捉图像或使用外部图像增强软件,而是提供内置过滤器,以编程方式提高图像质量。 这些过滤器可帮助 IronBarcode 读取困难的图像并提高整体准确性。

继续阅读,了解 IronBarcode 中可用的图像校正过滤器、它们对图像的影响以及如何应用它们。 有关更全面的条形码阅读技巧,请查看我们的阅读条形码教程

as-heading:2(快速入门:应用锐化和对比度滤镜改进条形码读取)

只需一步,即可使用 BarcodeReaderOptions 中的 ImageFilterCollection 应用 IronBarcode 的 SharpenFilterContrastFilter 。 这可以提高条形码扫描的效率,只需最少的设置和对外部工具的零需求。

Nuget Icon立即开始使用 NuGet 创建 PDF 文件:

  1. 使用 NuGet 包管理器安装 IronBarcode

    PM > Install-Package BarCode

  2. 复制并运行这段代码。

    BarcodeResults results = IronBarCode.BarcodeReader.Read("input.png", new IronBarCode.BarcodeReaderOptions { ImageFilters = new IronBarCode.ImageFilterCollection() { new IronBarCode.SharpenFilter(3.5f), new IronBarCode.ContrastFilter(2.0f) } });
  3. 部署到您的生产环境中进行测试

    立即开始在您的项目中使用 IronBarcode,免费试用!
    arrow pointer

如何应用图像过滤器来改进条形码读取? 要应用过滤器,请实例化 `ImageFilterCollection` 类,并分别创建每个过滤器的实例。 然后将该对象赋值给 `BarcodeReaderOptions` 对象的 `ImageFilters` 属性。 将选项对象与示例图像一起传递到 `Read` 方法中。 有关高级安装选项,请访问我们的 [NuGet 软件包指南](https://ironsoftware.com/csharp/barcode/get-started/advanced-installation-nuget/)。 请使用下图作为样本图片。
Blurred barcode with number 4900203187590 showing poor image quality before filtering enhancement
图片看起来很模糊。 但是亮度尚可接受,白色和黑色也能区分。 因此,至少应用 **SharpenFilter** 和 **ContrastFilter** 以提高条形码的可读性。 请参考下面的代码片段,对图片应用过滤器、读取图片并在控制台上显示结果。 ```csharp :path=/static-assets/barcode/content-code-examples/how-to/image-correction-apply-filter.cs ``` 上面的代码片段应用过滤器,读取条形码,并将过滤后的图像导出到磁盘。 样本与过滤后图片的对比如下所示。
Blurry barcode image with number 4902030187590 demonstrating poor image quality
Barcode with improved readability after applying image filters, showing clear vertical lines and number 4902030187590

IronBarcode 提供哪些图像校正过滤器? IronBarcode 提供多种[图像过滤器](https://ironsoftware.com/csharp/barcode/features/filters/),专门用于图像校正。 这些过滤器有助于读取不完美的条形码图像并提高读取精度。 但是,要了解这些过滤器的工作原理,以**选择合适的过滤器**,**避免因使用过多过滤器或使用错误过滤器而产生性能问题**。 可用的过滤器包括 -<代码>自适应阈值过滤器二进制阈值过滤器亮度过滤对比度过滤器InvertFilterSharpenFilterErodeFilterDilateFilterHistogramEqualizationFilter高斯模糊过滤器BilateralFilterMedianBlurFilter自适应阈值过滤器如何工作? **AdaptiveThresholdFilter**是IronBarcode中可用的过滤器,它将[Bradley自适应阈值](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.)技术应用于图像,可自动确定图像二值化的阈值。 该滤镜适用于光照不均匀和背景强度水平不一的图像。 ```csharp :path=/static-assets/barcode/content-code-examples/how-to/image-correction-adaptive-threshold.cs ``` 以下是使用不同值进行筛选后的输出结果。
Vertical lines showing different adaptive threshold filter outputs with solid and dashed patterns
Low-quality barcode image showing UPC number 902030187590 with significant visual distortion
构造函数接受用于配置的附加参数: - `Upper`:阈值的上色(白色)。 - `Lower`:阈值的下色(黑色)。 - `阈值`:二值化的阈值限制(0.0-1.0)。 -<代码>矩形:应用处理器的矩形区域。 如上面的输出图像所示,图像经过二值化处理,只有**黑**和**白**两种颜色。 虽然在条形码读取方面似乎仍不理想,但需要结合使用过滤器。 请尝试使用参数灵敏度以达到最佳效果。

二进制阈值过滤器如何工作? **BinaryThresholdFilter** 通过在给定阈值处分割像素、比较颜色分量的亮度来过滤图像。 与 `AdaptiveThresholdFilter` 类似,如果使用不当,该过滤器可能会引入新的或不需要的噪声。 不过,IronBarcode 为过滤器属性设置了默认值。 与 `AdaptiveThresholdFilter` 类似,`BinaryThresholdFilter` 也接受相同的附加配置参数: - `Upper`:阈值的上色(白色)。 - `Lower`:阈值的下色(黑色)。 - `阈值`:二值化的阈值限制(0.0-1.0)。 -<代码>矩形:应用处理器的矩形区域。 ```csharp :path=/static-assets/barcode/content-code-examples/how-to/image-correction-binary-threshold.cs ``` 以下是对示例图片应用过滤器后的输出示例。
Three examples of binary threshold filter outputs showing sparse, dotted, and dense vertical line patterns
Barcode image processed with 0.9 binary threshold filter showing black and white contrast
观察上面的输出图像,样本已被二值化为黑白两色。 然而,由于消除了条形码条,并引入了新的噪声,该过滤器显然不适合该图像。 如需处理条形码疑难情况,请参阅我们的[未识别条形码疑难解答指南](https://ironsoftware.com/csharp/barcode/troubleshooting/barcode-not-recognized/)。

如何调整图像亮度以更好地读取条形码? **BrightnessFilter** 是 IronBarcode 中图像过滤器集合中另一个必不可少的过滤器。 顾名思义,该过滤器可调整 BarCode 图像的亮度。 该构造函数的输入会改变输出图像中的 **Amount** 亮度。 默认值为 1,图像保持不变。 数值为 0 时,图像完全变黑;数值超过 1 时,图像变亮。 ```csharp :path=/static-assets/barcode/content-code-examples/how-to/image-correction-brightness.cs ``` 以下是对样本输入应用该过滤器后的输出图像。
Blurry UPC barcode sample showing default brightness level before filter enhancement
Blurry barcode with product number 4902030187590, demonstrating low brightness or poor image quality

如何使用对比度过滤器来增强条形码图像? **ContrastFilter** 可调整图像的对比度。 图像对比度是指图像中不同元素之间颜色强度的差异。 提高对比度可以增强细节的可视性,使图像显得生动、醒目,而降低对比度则会使图像显得更加柔和、沉稳。 有关条形码定制的详细信息,请参阅我们的[条形码样式定制](https://ironsoftware.com/csharp/barcode/how-to/customize-barcode-style/)指南。 默认值为 1,图像保持不变。 数值为 0 时,图像完全为灰色;数值大于 1 时,图像对比度增加。 ```csharp :path=/static-assets/barcode/content-code-examples/how-to/image-correction-contrast.cs ``` 将该过滤器应用到示例输入中会生成下图。
Blurry barcode with number 4902030187590 demonstrating default contrast filter settings
Blurry barcode with number 4902030187590 demonstrating low contrast image quality

何时应使用反相过滤器? 这种滤镜可以反转图像内部的颜色,使颜色相反,如白变黑、黑变白。它在读取带背景色的 BarCode 图像时特别有用。 与 **二进制阈值过滤器**不同,该过滤器可直接反转颜色,而无需指定灵敏度。 此外,该过滤器可与 **CropRectangle** 配合使用,以指定图像中需要反转颜色的位置,而不是反转整个图像的颜色。 在我们的[作物区域教程](https://ironsoftware.com/csharp/barcode/how-to/set-crop-region/)中了解有关指定作物区域的更多信息。 ```csharp :path=/static-assets/barcode/content-code-examples/how-to/image-correction-invert.cs ``` 下面的输出图像是将该过滤器应用于样本输入图像的结果。
Blurry UPC barcode showing number 480203187590 - original image before invert filter application
Blurry inverted barcode showing white bars on dark background with number sequence 4902030187590

如何使用锐化过滤器修复模糊的 BarCode 图像? IronBarcode 提供了一个锐化过滤器。 该过滤器可增强图像的清晰度,在处理模糊图像时非常有用。 通过在实例化滤镜对象时调整 **Sigma** 值来操纵该滤镜以调整图像的清晰度。 默认值为 3,增加西格玛值可提高图像清晰度。 有关其他性能优化选项,请查看我们的[阅读速度选项](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 sharpened version of the sample input image.
Blurry barcode image demonstrating unsharpened quality before applying sharpen filter
Blurred barcode example showing effects of image quality degradation
将上面的图片与原始图片相比,显得更加清晰,有助于使用 IronBarcode 读取条形码。 大多数情况下, **SharpenFilter**总是与`ImageFilterCollection`类中的其他滤镜一起应用。

Erode 过滤器有哪些用途? **ErodeFilter**可去除微小的白噪声,并通过去除形状边缘附近的像素来加粗条形码条。该过滤器最适用于条形码背景有大量白色斑点的场景,或者条形码图像分辨率过低或模糊,导致条形码合并的情况。 **ErodeFilter**可使条形图变粗,同时去除背景中的白色斑点。 有关处理不完美图像的更多信息,请参阅我们的[不完美条形码示例](https://ironsoftware.com/csharp/barcode/examples/imperfect-barcode-with-image-correction/)。 为过滤器输入一个代表 `kernelSize` 的整数,以增强侵蚀效果。 内核尺寸越大,对输入图像的影响越强。 请注意,`kernelSize` 是一个正方形,在本例中是一个 `5x5` 内核。 例如,使用内核尺寸较大的 **ErodeFilter** 来展示过滤器的效果。 ```cs :path=/static-assets/barcode/content-code-examples/how-to/image-correction-erode.cs ```
Blurry barcode showing number 4002030187590 - example for erode filter demonstration
Blurred barcode showing vertical black and white stripes with numerical code
比较上面的输入和输出图像,有些条形图明显变粗,这是因为输入了更大的内核尺寸进行过滤。 然而,整体画面中的白色斑点减少了。 根据侵蚀过滤器的性质,内核尺寸越大,如果应用得过于激烈,可能会擦除细条,如上图所示。 通过更改输入到 **ErodeFilter** 的内核大小值来测试和完善效果。

Dilate 过滤器如何帮助条码读取? **DilateFilter**的功能与**ErodeFilter**相反,它通过在对象边界添加像素来扩大明亮区域(通常是背景)。 虽然该过滤器可通过填补小缝隙或增强低对比度区域来修复损坏或模糊的条形码,但请注意其对条形码条的效果与直觉不同。 由于扩张会放大明亮的空间,因此会间接稀释黑色条形码条(假设背景为白色)等暗色元素。 这使得过滤器在条形码条显得过粗或合并的情况下特别有效,但过度使用会使条形码过分变窄,从而降低扫描精度。 与上述类似,通过输入一个代表过滤器 `kernelSize` 的整数来增加过滤器的效果。 在下面的示例中,请使用较大的内核尺寸来展示 **DilateFilter**的效果。 ```cs :path=/static-assets/barcode/content-code-examples/how-to/image-correction-dilate.cs ```
Blurry barcode image showing number 4902030187590 - example for dilate filter processing
Blurred barcode with numerical sequence below vertical bars
如上图所示,积极使用 **DilateFilter** 有可能破坏条形码结构,合并间距较近的条形码并在条形码中创建静区。 根据输入图像的不同,通过改变内核大小值的大小来测试和完善图像效果。

何时应使用直方图均衡化筛选器? **HistogramEqualizationFilter** 通过重新分配像素强度来增强图像对比度,从而提高清晰度。 它最常用于条形码对比度较低的情况,如褪色或冲淡的图像,或者光照不均匀的图像,如暗影或强光。 通过分析图像直方图(即像素亮度分布图),它可以重新分配像素值,通过拉伸强度范围来增强对比度,使暗像素变暗,亮像素变亮。 ```cs :path=/static-assets/barcode/content-code-examples/how-to/image-correction-histogram-equalization-filter.cs ```
Blurry barcode with number 4902030187590 used as test image for histogram equalization filter
Barcode with vertical black and white stripes showing number 4902030187590
如上图所示,与原图相比,黑条明显变暗,空格明显变亮。

哪些模糊过滤器可帮助降低 BarCode 噪声?

高斯模糊过滤器如何降低图像噪音? **GaussianBlurFilter** 将高斯模糊应用于图像。 这种滤波器通常可以减少图像中的噪点。 有关处理不完美条形码的综合指南,请参阅我们的[图像方向校正教程](https://ironsoftware.com/csharp/barcode/how-to/image-orientation-correction/)。 滤波器的工作原理是使用高斯函数平均图像中的相邻像素值。 该方法依赖于两个可调整的因素: - **内核**:用于平均像素的矩阵。 - **Sigma**:控制模糊强度的值。 默认`内核`大小为`3x3`像素,默认`Sigma`值为`3.0`,可产生适度的模糊。 增加 `Sigma` 值可获得更强的模糊效果。 您还可以自定义 ` 内核`,以控制模糊过滤器平均邻域的大小。 ```csharp :path=/static-assets/barcode/content-code-examples/how-to/image-correction-gaussianblur.cs ``` 将该过滤器应用到示例输入中会生成下图。
Blurry barcode with number 4902030187590 demonstrating poor image quality
Barcode image with Gaussian blur filter applied, showing blurred vertical lines and distorted numbers

何时使用双边过滤器? **BilateralFilter** 在保留边缘的同时平滑图像。 与均匀影响所有像素的简单模糊技术不同,双边滤波器同时考虑了颜色差异和像素距离,使其成为有效的边缘保护平滑技术。 该方法依赖于三个可调整的因素: - `邻域直径`:像素邻域直径(默认值:5)。 -<代码>SigmaColor:确定色差影响的颜色影响(默认值:75.0)。 -<代码>SigmaSpace:决定距离影响的空间影响(默认值:75.0)。 ```csharp :path=/static-assets/barcode/content-code-examples/how-to/image-correction-bilateral.cs ``` 将该过滤器应用到示例输入中会生成下图。
Blurred barcode demonstrating poor image quality with vertical lines and numbers 4902030187590
Blurred barcode with numbers 4902030187590 demonstrating poor image quality

是什么让 MedianBlur 滤波器在降噪方面与众不同? **MedianBlurFilter**通过将每个像素的值替换为周围像素的中值来减少图像中的噪声。 该过滤器尤其擅长在去除噪声的同时保留边缘。要探索有关条码读取设置的更多信息,请访问我们的[条码阅读器设置指南](https://ironsoftware.com/csharp/barcode/examples/barcode-reader-settings-csharp/)。 - `KernelSize`:用于计算中位数的邻域大小(必须为奇数,默认为 5)。 ```csharp :path=/static-assets/barcode/content-code-examples/how-to/image-correction-medianblur.cs ``` 将该过滤器应用到示例输入中会生成下图。
Blurry barcode example showing poor image quality with digital artifacts and reduced readability
Barcode with median blur filter applied showing blurred vertical lines and number 4902030187590

如何在每个处理步骤中保存过滤后的图像? 在对 BarCode 应用多个过滤器时,查看每个过滤器方法后的输出可能会比较困难。 该功能允许在应用每个过滤器后,按照处理顺序保存过滤后的图像。 要启用此功能,请首先将 `true` 传递给 **ImageFilterCollection** 构造函数。 然后使用 `ExportFilterImagesToDisk` 方法提供输出图像的路径和名称。 有关保存 BarCode 的更多示例,请参阅我们的[将条形码转换为图像示例](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 ``` 过滤器按代码顺序应用,输出图像反映每次迭代的结果: - `Sharpen` -> `Sharpen` 之后 - `Sharpen` + `AdaptiveThreshold` -> 在 `AdaptiveThreshold` 之后 - `Sharpen` + `AdaptiveThreshold` + `Contrast` -> `Contrast` 之后
Blurry barcode with number 4902030187590
Blurry UPC barcode showing number 4902030187590
Degraded barcode example showing poor image quality with number 9020301875905
Heavily pixelated barcode with UPC number 902030187590
除了 `ImageFilters` 属性外,还可在 `BarcodeReaderOptions` 中添加其他属性,以便更准确地读取; 请参阅此[文章](https://ironsoftware.com/csharp/barcode/how-to/read-barcodes-from-images/#setting-barcode-reader-options)以获取更多信息。

常见问题解答

什么是图像校正过滤器,为什么条形码读取需要它们?

IronBarcode 中的图像校正过滤器是内置工具,可通过编程增强模糊或不完美的条码图像。图像质量差是妨碍成功读取条码的主要因素之一,因此图像校正过滤器是必不可少的。IronBarcode 提供的 SharpenFilter 和 ContrastFilter 等过滤器可在不需要外部图像编辑软件或重新捕获图像的情况下提高读取准确性。

如何应用图像校正过滤器来改进 BarCode 扫描?

要在 IronBarcode 中应用过滤器,需要创建一个 ImageFilterCollection 实例,并向其中添加单个过滤器实例。然后将此集合分配给 BarcodeReaderOptions 的 ImageFilters 属性,并将其传递给读取方法。例如:new BarcodeReaderOptions { ImageFilters = new ImageFilterCollection() { new SharpenFilter(3.5f), new ContrastFilter(2.0f) }.}.

建议使用哪些图像过滤器来处理模糊的 BarCode 图像?

对于模糊的条码图像,IronBarcode 建议至少使用 SharpenFilter 和 ContrastFilter。SharpenFilter 可增强模糊图像的边缘清晰度,而 ContrastFilter 则可改善明暗区域之间的区别。这些滤镜共同作用,使 BarCode 在不进行外部图像处理的情况下更加易读。

能否自定义图像校正滤镜的强度?

是的,IronBarcode 允许您用自定义值配置每个过滤器。例如,锐化过滤器(SharpenFilter)接受一个浮点参数(如 3.5f)来控制锐化强度,对比度过滤器(ContrastFilter)接受一个参数(如 2.0f)来调整对比度水平。这种自定义功能有助于针对不同的图像条件优化滤镜效果。

我是否需要外部图像编辑工具来增强 BarCode 图像?

不,IronBarcode 通过提供内置图像校正过滤器,消除了对外部图像编辑工具的需求。这些编程过滤器(如 SharpenFilter 和 ContrastFilter)可直接在您的 .NET 应用程序中提高图像质量,从而节省时间并避免依赖第三方软件。

Hairil Hasyimi Bin Omar
软件工程师
如所有伟大的工程师一般,Hairil 是个热心的学习者。他正在提高对 C#、Python 和 Java 的知识,并利用这些知识为 Iron Software 团队成员增值。Hairil 从马来西亚的玛拉工业大学加入 Iron Software 团队,获得化学与工艺工程学士学位。
准备开始了吗?
Nuget 下载 2,002,059 | 版本: 2025.12 刚刚发布