如何使用图像校正滤镜

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

海瑞尔 哈西米 本 奥马尔

面对现实吧。并非每个图像都是完美的,这也是 IronBarcode 无法读取条码图像的**主要因素之一。这并不完全是用户的错。IronBarcode 提供了一项功能,使用户能够以编程方式对图像应用过滤器,而无需重新捕获图像或使用其他图像增强软件。这将有助于 IronBarcode 读取图像并提高准确性。

请继续阅读,了解 IronBarcode 中可用的图像校正过滤器、它们对图像的影响以及如何应用。

适用于的C# NuGet库

安装使用 NuGet

Install-Package BarCode
Java PDF JAR

下载 DLL

下载DLL

手动安装到你的项目中

适用于的C# NuGet库

安装使用 NuGet

Install-Package BarCode
Java PDF JAR

下载 DLL

下载DLL

手动安装到你的项目中

开始在您的项目中使用IronPDF,并立即获取免费试用。

第一步:
green arrow pointer

查看 IronBarcodeNuget 用于快速安装和部署。它有超过800万次下载,正在使用C#改变。

适用于的C# NuGet库 nuget.org/packages/BarCode/
Install-Package BarCode

考虑安装 IronBarcode DLL 直接。下载并手动安装到您的项目或GAC表单中: IronBarCode.zip

手动安装到你的项目中

下载DLL

使用图像过滤器改进读取示例

要应用过滤器,请实例化 ImageFilterCollection 类,并分别创建每个过滤器的实例。然后将该对象赋值给 BarcodeReaderOptions 对象的 ImageFilters 属性,并将该选项对象与示例图像一起传递到 Read 方法中。

让我们使用以下图像作为示例图像。

图片样本

从图像的初始外观来看,似乎相当模糊。不过,亮度还可以接受,白色和黑色也可以分辨。因此,我们至少需要应用SharpenFilterContrastFilter来提高条形码的可读性。请参考下面的代码片段,对图像应用滤镜,读取图像并将其显示在控制台上。

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-apply-filter.cs
using IronBarCode;
using System;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection()
    {
        new SharpenFilter(3.5f),
        new ContrastFilter(2)
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

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

// Write the result value to console
foreach (BarcodeResult result in results)
{
    Console.WriteLine(result.Text);
}
Imports IronBarCode
Imports System

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection() From {
		New SharpenFilter(3.5F),
		New ContrastFilter(2)
	}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("filteredSample.png")

' Write the result value to console
For Each result As BarcodeResult In results
	Console.WriteLine(result.Text)
Next result
VB   C#

从上面的代码片段来看,除了应用过滤器和读取条形码,我们还将过滤后的图像导出到磁盘。样本图像和过滤后图像的对比如下。

图片样本
过滤样本

探索图像校正过滤器

IronBarcode 提供多种专用于图像校正的图像过滤器。这些过滤器可以帮助读取不完美的条形码图像,提高读取的准确性。但是,用户需要了解这些过滤器的工作原理,以便

选择合适的过滤器避免因使用过多或错误的过滤器而导致性能问题。以下是所有可用的过滤器:

  • 自适应阈值过滤器
  • 二进制阈值过滤器
  • 亮度过滤器
  • 对比度滤镜
  • 反相滤波器
  • 锐化滤镜

应用这些滤镜的顺序取决于它们在 ImageFilterCollection 中的位置。

自适应阈值滤波器

AdaptiveThresholdFilter 是 IronBarcode 中可用的过滤器之一,适用于 布拉德利自适应阈值 该技术可自动确定图像二值化的阈值。这种滤波器适用于光照不均匀和背景强度水平变化的图像。

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-adaptive-threshold.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection() {
    new AdaptiveThresholdFilter(0.9f),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", myOptionsExample);

// Export file to disk
results.ExportFilterImagesToDisk("adaptiveThreshold_0.9.png");
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection() From {New AdaptiveThresholdFilter(0.9F)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", myOptionsExample)

' Export file to disk
results.ExportFilterImagesToDisk("adaptiveThreshold_0.9.png")
VB   C#

以下是使用不同数值进行滤波后的输出结果。

默认自适应阈值
0.9 自适应阈值

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

  • Upper:上部 (白色) 颜色进行阈值处理。
  • 较低:较低 (黑色) 颜色进行阈值处理。
  • 阈值:阈值限制 (0.0-1.0) 考虑进行二值化处理。
  • 矩形:应用处理器的矩形区域。

从上面的输出图像中可以看到,图像经过二值化处理后只有两种颜色。虽然这似乎仍不是条形码读取的理想选择,因为过滤器需要组合使用。用户需要对参数灵敏度进行试验,以获得最佳效果。

二进制阈值过滤器

二进制阈值滤波器(BinaryThresholdFilter)通过在给定阈值处分割像素来过滤图像,它用于比较颜色成分的亮度。与自适应阈值过滤器(AdaptiveThresholdFilter)类似,如果使用不当,该过滤器可能会引入新的或不需要的噪声。不过,IronBarcode 已为该过滤器的属性设置了默认值。

与 AdaptiveThresholdFilter 类似,BinaryThresholdFilter 也接受相同的附加参数进行配置。

  • 上限值上限值 (白色) 颜色进行阈值处理。
  • 较低:较低 (黑色) 颜色进行阈值处理。
  • 阈值:阈值限制 (0.0-1.0) 考虑进行二值化处理。
  • 矩形:应用处理器的矩形区域。
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-binary-threshold.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection() {
        new BinaryThresholdFilter(0.9f)
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", myOptionsExample);

// Export file to disk
results.ExportFilterImagesToDisk("binaryThreshold_0.9.png");
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection() From {New BinaryThresholdFilter(0.9F)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", myOptionsExample)

' Export file to disk
results.ExportFilterImagesToDisk("binaryThreshold_0.9.png")
VB   C#

以下是对样本图像应用滤镜后的输出示例。

默认二进制阈值
0.9 二进制阈值

观察上面的输出图像,我们可以看到样本已被二值化为黑白颜色。但是,我们可以看到,由于条形码条被消除,同时也引入了一些新的噪音,因此该滤波器显然不适合该图像。

亮度过滤器

亮度滤镜是 IronBarcode 图像滤镜系列中的另一个重要滤镜。顾名思义,该过滤器可调整条形码图像的亮度。该构造函数的输入可以改变输出图像中亮度的***数量。默认值为 1,使图像保持不变。值为 0 将创建一个完全黑色的图像,而值大于 1 则会使图像更亮。

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-brightness.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection() {
    new BrightnessFilter(1.5f),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", myOptionsExample);

// Export file to disk
results.ExportFilterImagesToDisk("brightness_1.5.png");
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection() From {New BrightnessFilter(1.5F)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", myOptionsExample)

' Export file to disk
results.ExportFilterImagesToDisk("brightness_1.5.png")
VB   C#

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

默认亮度
1.5 亮度

对比度过滤器

对比度滤镜**用于调整图像的对比度。图像对比度是指图像中不同元素之间颜色强度的差异。提高对比度可以增强细节的可视性,使图像看起来生动而醒目,而降低对比度则会使图像看起来更柔和、更沉稳。

默认值为 1,使图像保持不变。数值为 0 会生成完全灰色的图像,而数值高于 1 则会增加图像对比度。

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-contrast.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection() {
    new ContrastFilter(1.5f),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", myOptionsExample);

// Export file to disk
results.ExportFilterImagesToDisk("contrast_1.5.png");
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection() From {New ContrastFilter(1.5F)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", myOptionsExample)

' Export file to disk
results.ExportFilterImagesToDisk("contrast_1.5.png")
VB   C#

将此滤波器应用于样本输入,将生成下图。

默认对比度
1.5 对比

反相过滤器

该滤镜用于反转图像内部的颜色,使颜色相反,如白变黑、黑变白。当用户试图读取带有背景颜色的条形码图像时,它尤其有用。与二进制阈值滤波器不同,该滤波器可直接反转颜色,而无需指定灵敏度。此外,该过滤器还可与裁剪矩形一起使用,指定图像中需要反转颜色的位置,而不是反转整个图像的颜色。

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-invert.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection() {
    new InvertFilter(),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample1.png", myOptionsExample);

// Export file to disk
results.ExportFilterImagesToDisk("invert.png");
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection() From {New InvertFilter()}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample1.png", myOptionsExample)

' Export file to disk
results.ExportFilterImagesToDisk("invert.png")
VB   C#

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

原图
倒置

锐化滤波器

IronBarcode 的最后一个图像校正滤镜是锐化滤镜(SharpenFilter)。该滤镜可增强图像的清晰度,在处理模糊图像时非常有用。用户可以在实例化滤镜对象时,通过调整Sigma值来操作该滤镜,以调整图像的清晰度。默认值为 3。

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-sharpen.cs
using IronBarCode;
using System;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection()
    {
        new SharpenFilter((float)3.5),
        new ContrastFilter(2)
    },
};
// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

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

// Write the result value to console
foreach (BarcodeResult result in results)
{
    Console.WriteLine(result.Text);
}
Imports IronBarCode
Imports System

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection() From {
		New SharpenFilter(CSng(3.5)),
		New ContrastFilter(2)
	}
}
' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("filteredSample.png")

' Write the result value to console
For Each result As BarcodeResult In results
	Console.WriteLine(result.Text)
Next result
VB   C#

下图为 削尖的 版本的样本输入图像。

默认锐化
0.5 磨光

将上面的图像与原始图像进行比较,它看起来更加清晰,绝对有助于使用 IronBarcode 读取条形码。在大多数情况下,SharpenFilter 总是与 ImageFilterCollection 类中的其他过滤器一起使用。

除了 图像过滤器 属性,用户还可以在 条码阅读器选项 要获得更准确的读数,请参阅 了解更多信息。

海瑞尔 哈西米 本 奥马尔

软件工程师

像所有优秀的工程师一样,Hairil 是一个热衷学习的人。他正在精进自己的 C#、Python 和 Java 知识,并利用这些知识为 Iron Software 团队成员增添价值。Hairil 毕业于马来西亚的马来西亚工艺大学(Universiti Teknologi MARA),获得了化学与工艺工程学士学位,然后加入了 Iron Software 团队。