C# 指南:使用 IronOCR 图像过滤器提升 OCR 识别效果
IronOCR 提供读取图像所需的工具,这些图像可能需要以滤镜的形式进行预处理。 您可以从各种各样的滤镜中进行选择,这些滤镜可以处理您的图像,使其可以进行后续处理。
快速入门:应用滤镜清理 OCR 图像
只需一个简单的调用链,您就可以在 OCR 之前应用 DeNoise, Binarize 和 Deskew 过滤器来提高扫描的清晰度。 本示例展示了使用 IronOCR 的内置滤镜增强图像是多么容易,并且可以立即上手。
立即开始使用 NuGet 创建 PDF 文件:
使用 NuGet 包管理器安装 IronOCR
复制并运行这段代码。
using var input = new IronOcr.OcrInput("scan.jpg"); input.DeNoise(true).Binarize().Deskew(45); var result = new IronOcr.IronTesseract().Read(input);部署到您的生产环境中进行测试
OCR图像过滤器列表
以下图像滤镜可以显著提升性能:
Filters to change the Image OrientationRotate- 将图像顺时针旋转指定角度。逆时针旋转请使用负数。Deskew- 将图像旋转成正确的方向和正交角度。 这对于 OCR 非常有用,因为 Tesseract 对倾斜扫描的容忍度可以低至 5 度。Scale- 按比例缩放 OCR 输入页面。Filters to manipulate Image ColorsBinarize- 此图像滤镜将每个像素变为黑色或白色,没有中间色。 这可以提高文本与背景对比度非常低时的 OCR 性能。ToGrayScale- 此图像滤镜将每个像素转换为灰度。 不太可能提高OCR识别准确率,但可能会提高速度。Invert- 反转所有颜色。 例如,白色变成黑色,反之亦然。ReplaceColor- 将图像中的一种颜色替换为在一定阈值范围内的另一种颜色。
Filters to improve Contrast in an ImageContrast- 自动增加对比度。 该滤镜通常可以提高低对比度扫描图像的 OCR 速度和准确性。Dilate- 高级形态学。 膨胀操作是指在图像中对象的边界上添加像素。 与侵蚀相反。Erode- 高级形态学。 腐蚀会从物体边界移除像素。 与扩张相反。
Filters to reduce Image NoiseSharpen- 锐化模糊的 OCR 文档,并将 alpha 通道展平为白色。DeNoise- 去除数字噪声。此滤波器仅应用于预期会出现噪声的场景。DeepCleanBackgroundNoise- 消除严重的背景噪音。 仅当已知文档背景噪声极大时才使用此过滤器,因为它可能会降低干净文档的 OCR 准确率,并且会占用大量 CPU 资源。EnhanceResolution- 提高低质量图像的分辨率。 这个过滤器并不经常需要,因为OcrInput.MinimumDPI和OcrInput.TargetDPI会自动捕获并解决低分辨率输入。
过滤器示例及用法
下面的示例将演示如何在代码中应用过滤器。
:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-1.csusing IronOcr;
using System;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("my_image.png");
input.Deskew();
var result = ocr.Read(input);
Console.WriteLine(result.Text);Imports IronOcr
Imports System
Private ocr = New IronTesseract()
Private input = New OcrInput()
input.LoadImage("my_image.png")
input.Deskew()
Dim result = ocr.Read(input)
Console.WriteLine(result.Text)调试过滤器/过滤器正在执行什么操作?
如果您在程序中读取图像或条形码时遇到困难,可以保存筛选结果的图像。 这样,你就可以进行调试,并准确地看到每个滤镜的作用以及它是如何处理图像的。
:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-2.csusing IronOcr;
using System;
var file = "skewed_image.tiff";
var ocr = new IronTesseract();
using var input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(file, pageindices);
// Here we apply the filter: Deskew
input.Deskew();
// Save the input with filter(s) applied
input.SaveAsImages("my_deskewed");
// We read, then print the text to the console
var result = ocr.Read(input);
Console.WriteLine(result.Text);Imports IronOcr
Imports System
Private file = "skewed_image.tiff"
Private ocr = New IronTesseract()
Private input = New OcrInput()
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames(file, pageindices)
' Here we apply the filter: Deskew
input.Deskew()
' Save the input with filter(s) applied
input.SaveAsImages("my_deskewed")
' We read, then print the text to the console
Dim result = ocr.Read(input)
Console.WriteLine(result.Text)过滤器使用案例
旋转
筛选说明
旋转滤镜用于手动设置图像的已知旋转角度,使其尽可能接近直线。 IronOCR 具有运行Deskew()功能,但是,其容差范围相当窄,最适合用于几乎完全笔直的图像(偏差在 15 度左右)。 对于偏离 90 度或上下颠倒的输入图像,我们应该调用Rotate() 。
用例代码示例
这是一个调用 Rotate() 来纠正颠倒图像的示例:
:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-3.csusing IronOcr;
using System;
var image = "screenshot.png";
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);
// Rotate 180 degrees because image is upside-down
input.Rotate(180);
// Read image into variable: result
var result = ocr.Read(input);
// Example print to console
Console.WriteLine(result.Text);Imports IronOcr
Imports System
Private image = "screenshot.png"
Private ocr = New IronTesseract()
Private input = New OcrInput()
' Load at least one image
input.LoadImage(image)
' Rotate 180 degrees because image is upside-down
input.Rotate(180)
' Read image into variable: result
Dim result = ocr.Read(input)
' Example print to console
Console.WriteLine(result.Text)在输入Before旋转(180)`` | 输入After旋转(180)`` |
|---|---|
IronOCR | IronOCR |
去歪
筛选说明
使用霍夫变换来尝试在一定容差范围内矫正图像。 对于不完全水平的图像来说,这一点很重要,因为倾斜的文档可能会导致误读。
[{i:(此方法返回一个布尔值,如果应用了过滤器,则返回 true;如果由于无法检测图像方向而未能应用,则返回 false。 如果页面没有内容来定义方向,此操作将失败。
用例代码示例
这是一个调用 Deskew() 来纠正倾斜图像的示例:
:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-4.csusing IronOcr;
using System;
var image = @"paragraph_skewed.png";
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);
// Apply deskew with 15 degree snap
bool didDeskew = input.Deskew(15);
if (didDeskew)
{
// Read image into variable: result
var result = ocr.Read(input);
Console.WriteLine(result.Text);
}
else
{
Console.WriteLine("Deskew not applied because Image Orientation could not be determined.");
}Imports IronOcr
Imports System
Private image = "paragraph_skewed.png"
Private ocr = New IronTesseract()
Private input = New OcrInput()
' Load at least one image
input.LoadImage(image)
' Apply deskew with 15 degree snap
Dim didDeskew As Boolean = input.Deskew(15)
If didDeskew Then
' Read image into variable: result
Dim result = ocr.Read(input)
Console.WriteLine(result.Text)
Else
Console.WriteLine("Deskew not applied because Image Orientation could not be determined.")
End If规模
筛选说明
缩放是一个有用的图像处理滤镜,可以利用已有的像素来调整图像大小。 当条形码无法扫描时,可以使用此方法,因为图像只有十几像素宽,每个条形都是一个像素,或者如果文本太小且没有抗锯齿效果时。
对于1000px x 1000px的条形码尺寸来说,存在一个最佳尺寸范围,在这个范围内条形码可以很好地被读取。如果您的条形码无法被找到,则应考虑此尺寸范围。
用例代码示例
这是一个调用 Scale() 放大条形码中条形之间的间隙以便扫描的示例:
:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-5.csusing IronOcr;
using System;
var image = @"small_barcode.png";
var ocr = new IronTesseract();
// Optional: This example uses a barcode
ocr.Configuration.ReadBarCodes = true;
using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);
// Apply scale
input.Scale(400); // 400% is 4 times larger
// Read image into variable: result
var result = ocr.Read(input);
// Example print to console
Console.WriteLine(result.Text);Imports IronOcr
Imports System
Private image = "small_barcode.png"
Private ocr = New IronTesseract()
' Optional: This example uses a barcode
ocr.Configuration.ReadBarCodes = True
Dim input = New OcrInput()
' Load at least one image
input.LoadImage(image)
' Apply scale
input.Scale(400) ' 400% is 4 times larger
' Read image into variable: result
Dim result = ocr.Read(input)
' Example print to console
Console.WriteLine(result.Text)二值化
筛选说明
二值化滤波器根据自适应算法将图像中的所有像素分类为黑色或白色。 这会移除所有颜色,并将背景变成纯白色,所有被识别为文本的部分都会变成全黑色,以便于阅读。
用例代码示例
这是一个调用 Binarize() 以对齐彩色文本并去除背景颜色和噪声的示例:
:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-6.csusing IronOcr;
using System;
var image = @"no-binarize.jpg";
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);
// Apply Binarize
input.Binarize();
// Read image into variable: result
var result = ocr.Read(input);
// Example print to console
Console.WriteLine(result.Text);Imports IronOcr
Imports System
Private image = "no-binarize.jpg"
Private ocr = New IronTesseract()
Private input = New OcrInput()
' Load at least one image
input.LoadImage(image)
' Apply Binarize
input.Binarize()
' Read image into variable: result
Dim result = ocr.Read(input)
' Example print to console
Console.WriteLine(result.Text)在二值化Before ()`` | 二进制化After ()`` |
|---|---|
IronOCR | IronOCR |
倒置
筛选说明
IronOCR 识别black text on a white background图像效果最佳。 Invert 滤镜通过反转图像上的所有颜色来实现这一目的。
用例代码示例
这是一个调用 Invert() 将黑底白字转换为白底黑字的示例:
:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-7.csusing IronOcr;
using System;
var image = @"before-invert.png";
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);
// Apply Invert
input.Invert(true);
// Read image into variable: result
var result = ocr.Read(input);
// Example print to console
Console.WriteLine(result.Text);Imports IronOcr
Imports System
Private image = "before-invert.png"
Private ocr = New IronTesseract()
Private input = New OcrInput()
' Load at least one image
input.LoadImage(image)
' Apply Invert
input.Invert(True)
' Read image into variable: result
Dim result = ocr.Read(input)
' Example print to console
Console.WriteLine(result.Text)Before | After |
|---|---|
IronOCR | IronOCR |
常见问题解答
图像过滤器如何提高C#中的OCR精度?
IronOCR中的图像过滤器可以预处理图像以增强其质量,从而提高OCR精度。诸如二值化和对比度等过滤器通过调整颜色和对比度增加可读性,而旋转和去斜则可以纠正图像方向。
有哪些过滤器可用于纠正图像方向?
IronOCR提供旋转和去斜过滤器来纠正图像方向问题。旋转允许手动调整图像角度,而去斜则自动矫正轻微倾斜的图像。
二值化过滤器如何影响图像预处理?
IronOCR中的二值化过滤器将图像像素转为黑白,去除背景色,增强文本可见性,特别在低对比度条件下提高OCR精度。
何时适合使用去噪过滤器?
去噪过滤器如锐化和去噪应在图像中存在数字噪声时使用。这些过滤器清理图像,使文本更加清晰,以获得更好的IronOCR结果。
提高图像分辨率是否会影响OCR性能?
是的,使用增强分辨率过滤器可以通过增加低质量图像的分辨率来提高OCR性能。虽然IronOCR的默认MinimumDPI和TargetDPI设置通常已经足够,过滤器在需要时可以提供额外的分辨率增强。
颜色操作过滤器在OCR中扮演什么角色?
IronOCR中的颜色操作过滤器如反转、转灰度和二值化调整图像颜色以增强文本可读性。反转改变配色方案,转灰度将图像转为灰度,二值化将图像减少为黑白。
对比度和锐化过滤器有什么区别?
IronOCR中的对比度过滤器增加了明暗区域之间的差异,提升了文本清晰度,而锐化过滤器则增强了边缘,使文本更清晰,两者都帮助更好地进行OCR识别。
如何在IronOCR中保存和调试过滤后的图像?
在IronOCR中过滤后可以使用SaveAsImages功能保存和调试图像。这有助于可视化过滤器效果,确保预处理步骤已经提升了OCR的图像质量。
IronOCR有哪些高级形态学过滤器?
IronOCR提供高级形态学过滤器如膨胀和腐蚀。膨胀向对象边界添加像素以增强特征,而腐蚀则去掉像素,两者均用于清晰化图像细节以提高OCR准确性。
IronOCR
IronOCR
IronOCR
IronOCR
IronOCR
IronOCR






