使用C#图像校正滤镜提升条形码解码效果

Hairil Hasyimi Bin Omar
Hairil Hasyimi Bin Omar
更新: 2026年8月2日

IronBarcode 提供内置图像校正滤镜,如 SharpenFilter 和 ContrastFilter,程序化地增强模糊或不完美的条码图像,提高读取精度,而无需使用外部图像编辑软件或重拍图像。

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

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

快速入门:应用锐化和对比度滤镜以改善条码读取

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

  1. 1使用 IronBarcode 安装 NuGet Package Manager

    PM > Install-Package BarCode

  2. 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. 3部署到您的生产环境中进行测试

    今天就用免费试用在您的项目中开始使用IronBarcode
    arrow pointer

如何应用图像滤镜以提高条码读取?

要应用滤镜,请实例化 ImageFilterCollection 类,并分别创建每个滤镜的实例。 然后将对象分配给ImageFilters对象的BarcodeReaderOptions属性。 将选项对象与示例图像一起传递给 Read 方法。 有关高级安装选项,请访问我们的NuGet 包指南。

请使用下图作为样本图片。

Blurred barcode with number 4900203187590 showing poor image quality before filtering enhancement

图片看起来很模糊。 但是亮度尚可接受,白色和黑色也能区分。 因此,至少应用 SharpenFilter 和 ContrastFilter 以提高条码可读性。 请参考下面的代码片段,对图片应用过滤器、读取图片并在控制台上显示结果。

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);
}

上面的代码片段应用过滤器,读取条形码,并将过滤后的图像导出到磁盘。 样本与过滤后图片的对比如下所示。

Blurry barcode image with number 4902030187590 demonstrating poor image quality
应用图像滤镜后可读性提高的条形码,显示清晰的垂直线条和数字 4902030187590

IronBarcode 中可用的图像校正滤镜有哪些?

IronBarcode 提供了多个专为图像校正设计的图像滤镜。 这些过滤器有助于读取不完美的条形码图像并提高读取精度。 但是,要了解这些过滤器的工作原理,以选择合适的过滤器,避免因使用过多过滤器或使用错误过滤器而产生性能问题。 可用的过滤器包括

  • AdaptiveThresholdFilter
  • BinaryThresholdFilter
  • BrightnessFilter
  • ContrastFilter
  • InvertFilter
  • SharpenFilter
  • ErodeFilter
  • DilateFilter
  • HistogramEqualizationFilter
  • 模糊过滤器
    • GaussianBlurFilter
    • BilateralFilter
    • MedianBlurFilter

滤镜应用的顺序基于它们在 ImageFilterCollection 中的排列。 有关这些滤镜的详细 API 文档,请访问我们的API 参考。

自适应阈值滤镜是如何工作的?

AdaptiveThresholdFilter 是 IronBarcode 中可用的滤镜,应用 Bradley 自适应阈值技术到图像上,自动确定图像二值化的阈值。 该滤镜适用于光照不均匀和背景强度水平不一的图像。

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");

以下是使用不同值进行筛选后的输出结果。

垂直线条显示不同的自适应阈值滤镜输出,带有实线和虚线图案
低质量条形码图像,显示 UPC 编号 902030187590,存在明显的视觉失真

构造函数接受用于配置的附加参数:

  • Upper:用于阈值处理的上限(白色)。
  • Lower:用于阈值处理的下限(黑色)。
  • Threshold:二值化的阈值限制(0.0-1.0)。
  • Rectangle:应用处理器的矩形区域。

如上面的输出图像所示,图像经过二值化处理,只有黑和白两种颜色。 虽然在条形码读取方面似乎仍不理想,但需要结合使用过滤器。 请尝试使用参数灵敏度以达到最佳效果。

二值化阈值滤镜是如何工作的?

BinaryThresholdFilter 通过在给定阈值拆分像素来过滤图像,比较颜色组件的亮度。 与 AdaptiveThresholdFilter 类似,如果使用不当,此滤镜可能引入新的或不需要的噪声。 不过,IronBarcode 为过滤器属性设置了默认值。

与 AdaptiveThresholdFilter 类似,BinaryThresholdFilter 接受用于配置的相同附加参数:

  • Upper:用于阈值处理的上限(白色)。
  • Lower:用于阈值处理的下限(黑色)。
  • Threshold:二值化的阈值限制(0.0-1.0)。
  • Rectangle:应用处理器的矩形区域。
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");

以下是对示例图片应用过滤器后的输出示例。

二值阈值滤镜输出的三个示例,显示稀疏、点状和密集的垂直线条图案
使用 0.9 二值化阈值滤镜处理的条形码图像,显示黑白对比

观察上面的输出图像,样本已被二值化为黑白两色。 然而,由于消除了条形码条,并引入了新的噪声,该过滤器显然不适合该图像。 处理困难的条码场景,请参阅我们的未识别条码故障排除指南。

如何调整图像亮度以改善条码读取?

BrightnessFilter 是 IronBarcode 图像滤镜集中另一个重要的滤镜。 顾名思义,该过滤器可调整 BarCode 图像的亮度。 该构造函数的输入会改变输出图像中的 Amount 亮度。 默认值为 1,图像保持不变。 数值为 0 时,图像完全变黑;数值超过 1 时,图像变亮。

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");

以下是对样本输入应用该过滤器后的输出图像。

Blurry UPC barcode sample showing default brightness level before filter enhancement
带有产品编号 4902030187590 的模糊条形码,展示低亮度或较差的图像质量

如何使用对比度滤镜增强条码图像?

ContrastFilter 调整图像中的对比度水平。 图像对比度是指图像中不同元素之间颜色强度的差异。 提高对比度可以增强细节的可视性,使图像显得生动、醒目,而降低对比度则会使图像显得更加柔和、沉稳。 有关条码定制的更多详细信息,请参阅我们的条码样式定制指南。

默认值为 1,图像保持不变。 数值为 0 时,图像完全为灰色;数值大于 1 时,图像对比度增加。

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");

将该过滤器应用到示例输入中会生成下图。

Blurry barcode with number 4902030187590 demonstrating default contrast filter settings
带有数字 4902030187590 的模糊条形码,展示低对比度图像质量

什么时候应该使用反转滤镜?

这种滤镜可以反转图像内部的颜色,使颜色相反,如白变黑、黑变白。它在读取带背景色的 BarCode 图像时特别有用。 与 BinaryThresholdFilter 不同,此滤镜直接反转颜色,无需指定灵敏度。 此外,该过滤器可与 CropRectangle 配合使用,以指定图像中需要反转颜色的位置,而不是反转整个图像的颜色。 了解更多关于指定裁剪区域的信息,请参阅我们的裁剪区域教程。

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");

下面的输出图像是将该过滤器应用于样本输入图像的结果。

Blurry UPC barcode showing number 480203187590 - original image before invert filter application
模糊的反色条形码,在深色背景上显示白色条形,带有数字序列 4902030187590

如何使用锐化滤镜修复模糊的条码图像?

IronBarcode 提供了一个锐化过滤器。 该过滤器可增强图像的清晰度,在处理模糊图像时非常有用。 通过在实例化滤镜对象时调整 Sigma 值来操纵该滤镜以调整图像的清晰度。 默认值为 3,增加西格玛值可提高图像清晰度。 有关其他性能优化选项,请查看我们的读取速度选项指南。

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");

下图是示例输入图像的 锐化版本。

Blurry barcode image demonstrating unsharpened quality before applying sharpen filter
模糊的条形码示例,展示图像质量下降的效果

将上面的图片与原始图片相比,显得更加清晰,有助于使用 IronBarcode 读取条形码。 在大多数情况下,SharpenFilter 总是与 ImageFilterCollection 类中的其他滤镜一起应用。

侵蚀滤镜有什么用途?

ErodeFilter 通过移除形状边缘附近的像素来消除微小的白色噪声并加粗条码条。在条码背景有很多白斑或条码图像分辨率过低或模糊,导致条形条合并的场景中,此滤镜效果最好。 ErodeFilter 使条形条增强,同时去除背景中的白色斑点。 有关处理不完美图像的更多信息,请参阅我们的不完美条码示例。

通过为滤镜输入代表 kernelSize 的整数来增强侵蚀的效果。 内核尺寸越大,对输入图像的影响越强。 请注意,kernelSize 是一个正方形,在此示例中将是 5x5 核。

例如,使用更大的核大小的 ErodeFilter 来展示滤镜的效果。

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");
Blurry barcode showing number 4002030187590 - example for erode filter demonstration
模糊的条形码,显示黑白垂直条纹和数字代码

比较上面的输入和输出图像,有些条形图明显变粗,这是因为输入了更大的内核尺寸进行过滤。 然而,整体画面中的白色斑点减少了。 根据侵蚀过滤器的性质,内核尺寸越大,如果应用得过于激烈,可能会擦除细条,如上图所示。 通过更改输入到 ErodeFilter 的核大小值来测试和优化效果。

膨胀滤镜如何帮助条码读取?

DilateFilter 作为 ErodeFilter 的反义词工作,通过向对象边界添加像素来扩展亮区(通常是背景)。 虽然该过滤器可通过填补小缝隙或增强低对比度区域来修复损坏或模糊的条形码,但请注意其对条形码条的效果与直觉不同。 由于扩张会放大明亮的空间,因此会间接稀释黑色条形码条(假设背景为白色)等暗色元素。 这使得过滤器在条形码条显得过粗或合并的情况下特别有效,但过度使用会使条形码过分变窄,从而降低扫描精度。

与上面类似,通过为滤镜输入代表 kernelSize 的整数来增强其效果。

对于下面的示例,使用更大的核大小以显示 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 barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Export file to disk
results.ExportFilterImagesToDisk("dilateFilter.jpg");
Blurry barcode image showing number 4902030187590 - example for dilate filter processing
模糊的条形码,垂直条形下方带有数字序列

如上图所示,激进使用 DilateFilter 可能会破坏条码结构,合并间隔紧密的条形条,并在条码中创建静音区。 根据输入图像的不同,通过改变内核大小值的大小来测试和完善图像效果。

何时应该使用直方图均衡化滤镜?

HistogramEqualizationFilter 通过重新分配像素强度来增强图像对比度以提高清晰度。 它最常用于条形码对比度较低的情况,如褪色或冲淡的图像,或者光照不均匀的图像,如暗影或强光。 通过分析图像直方图(即像素亮度分布图),它可以重新分配像素值,通过拉伸强度范围来增强对比度,使暗像素变暗,亮像素变亮。

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");
Blurry barcode with number 4902030187590 used as test image for histogram equalization filter
带有黑白垂直条纹的条形码,显示数字 4902030187590

如上图所示,与原图相比,黑条明显变暗,空格明显变亮。

哪些模糊滤镜可以帮助降低条码噪声?

高斯模糊滤镜如何降低图像噪声?

GaussianBlurFilter 对图像应用高斯模糊。 这种滤波器通常可以减少图像中的噪点。 有关处理不完美条码的指南,请参阅我们的图像方向校正教程。

滤波器的工作原理是使用高斯函数平均图像中的相邻像素值。 该方法依赖于两个可调整的因素:

  • 核: 用于平均像素的矩阵。
  • Sigma:控制模糊强度的值。

默认的 kernel 大小是 3x3 像素,默认的 Sigma 值是 3.0,产生适度的模糊。 增加 Sigma 值会导致更强的模糊效果。 您还可以自定义 kernel 以控制模糊滤镜平均的邻域大小。

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");

将该过滤器应用到示例输入中会生成下图。

Blurry barcode with number 4902030187590 demonstrating poor image quality
应用高斯模糊滤镜的条形码图像,显示模糊的垂直线条和扭曲的数字

何时应该使用双边滤镜?

BilateralFilter 在平滑图像的同时保留边缘。 与均匀影响所有像素的简单模糊技术不同,双边滤波器同时考虑了颜色差异和像素距离,使其成为有效的边缘保护平滑技术。

该方法依赖于三个可调整的因素:

  • NeighborhoodDiameter:像素邻域的直径(默认:5)。
  • SigmaColor:确定颜色差异影响的颜色影响(默认:75.0)。
  • SigmaSpace:确定距离影响的空间影响(默认: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 barcode
BarcodeResults results = BarcodeReader.Read("sharpen.webp", myOptionsExample);

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

将该过滤器应用到示例输入中会生成下图。

Blurred barcode demonstrating poor image quality with vertical lines and numbers 4902030187590
带有数字 4902030187590 的模糊条形码,展示较差的图像质量

什么使中值模糊滤镜在降噪方面与众不同?

MedianBlurFilter 通过用周围像素的中值替换每个像素的值来减少图像中的噪声。 此滤镜特别擅长在去除噪声的同时保留边缘。要探索更多关于条码读取设置的信息,请访问我们的条码读取器设置指南。

  • KernelSize:用于中值计算的邻域大小(必须为奇数,默认: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 barcode
BarcodeResults results = BarcodeReader.Read("sharpen.webp", myOptionsExample);

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

将该过滤器应用到示例输入中会生成下图。

模糊的条形码示例,展示带有数字伪影和可读性降低的较差图像质量
应用中值模糊滤镜的条形码,显示模糊的垂直线条和数字 4902030187590

如何在每个处理步骤中保存滤过的图像?

在对 BarCode 应用多个过滤器时,查看每个过滤器方法后的输出可能会比较困难。 该功能允许在应用每个过滤器后,按照处理顺序保存过滤后的图像。 要启用此功能,首先将 true 传递给 ImageFilterCollection 构造函数。 然后使用 ExportFilterImagesToDisk 方法提供输出图像的路径和名称。 有关保存条码的更多示例,请参阅我们的将条码转换为图像示例。

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");

过滤器按代码顺序应用,输出图像反映每次迭代的结果:

  • Sharpen -> 经过 Sharpen
  • Sharpen + AdaptiveThreshold -> 经过 AdaptiveThreshold
  • Sharpen + AdaptiveThreshold + Contrast -> 经过 Contrast
带有数字 4902030187590 的模糊条形码
Blurry UPC barcode showing number 4902030187590
退化的条形码示例,展示带有数字 9020301875905 的较差图像质量
带有 UPC 编号 902030187590 的严重像素化条形码

除了 ImageFilters 属性,还可以向 BarcodeReaderOptions 添加其他属性以进行更准确的读取; 请参阅这篇文章以获取更多信息。

常见问题

IronBarcode中可用哪些图像校正滤镜?

IronBarcode提供多种图像校正滤镜,如SharpenFilter, ContrastFilter, AdaptiveThresholdFilter, BinaryThresholdFilter, BrightnessFilter, InvertFilter, ErodeFilter, DilateFilter, HistogramEqualizationFilter,以及包括GaussianBlurFilter, BilateralFilter和MedianBlurFilter在内的多种模糊滤镜。这些滤镜通过改善图像质量来提高条码读取的准确性。

如何在IronBarcode中应用Sharpen和Contrast滤镜?

要在IronBarcode中应用Sharpen和Contrast滤镜,使用这些滤镜初始化ImageFilterCollection类,并将其分配给BarcodeReaderOptions对象的ImageFilters属性。与图像一起将该选项对象传递给Read方法以增强条码可读性。

自适应阈值滤镜的用途是什么?

自适应阈值滤镜使用Bradley自适应阈值技术自动确定图像二值化的阈值,这对具有非均匀照明和背景强度变化的图像特别有用。

反转滤镜如何工作?

反转滤镜会反转图像的颜色,将白色转换为黑色,反之亦然,这对于具有背景颜色的条码图像很有帮助。可以将其应用于整个图像或使用CropRectangle指定的部分。

腐蚀滤镜对条码图像的效果是什么?

腐蚀滤镜通过去除边缘附近的像素来加厚条码条,从而减少背景中的白噪声。它对于低分辨率或模糊图像很有用,但可能需要仔细调整以避免抹去细条。

何时应该使用直方图均衡滤镜?

直方图均衡滤镜通过重新分布像素强度来增强对比度,适用于对比度低或光线不均的条码。该滤镜通过拉伸强度范围来改善可读性。

高斯模糊滤镜的作用是什么?

高斯模糊滤镜通过使用高斯函数平均像素值来减少图像中的噪声。它可以通过内核大小和西格玛值进行配置,并应用中度模糊来帮助减少图像噪声。

双边滤镜与其他模糊滤镜有何不同?

双边滤镜在保留边缘的同时减少噪声,并考虑颜色差异和像素距离。它通过考虑邻域直径、西格玛颜色和西格玛空间参数来平滑图像。

中值模糊滤镜的主要用途是什么?

中值模糊滤镜在最小化噪声的同时擅于保留边缘,通过用邻居中值替换每个像素的值,使其在边缘保留的噪声减少方面效果显著。

如何在IronBarcode中保存每个滤镜应用后的图像?

在IronBarcode中,通过将true传递给ImageFilterCollection构造函数来启用在每个滤镜步骤后导出图像,然后使用ExportFilterImagesToDisk方法在每个处理步骤保存输出图像。

准备开始了吗?

NuGet 下载量 2,575,633版本:2026.11刚刚发布