如何使用 C# 中的筛选向导实现更好的 OCR。

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

IronOCR 滤镜向导会自动测试图像上的所有预处理滤镜组合,以找到最佳 OCR 设置,同时返回最高置信度分数和重现结果所需的准确 C# 代码。

对图像进行 OCR 预处理是一项挑战。 多重过滤器可以提高识别率,但要找到正确的组合需要大量的试验和失误。 每张图片都会带来独特的挑战,因此人工测试非常耗时。 在处理 低质量扫描文件或具有不同噪声和失真度的图像时,这一点尤为重要。

IronOCR 的 OcrInputFilterWizard 解决了这一问题。 过滤器向导会自动评估过滤器组合,以最大限度地提高 OCR 的可信度和准确性。 它对最佳设置进行了详尽的测试,并将最佳过滤器组合作为代码片段返回,以便于复制结果。 该功能与OcrInput类无缝集成,简化了滤镜对图像的应用。

本指南演示了过滤器向导的工作原理,并展示了其使用的代码片段和参数。 有关其他 OCR 工作流程优化,请浏览我们的图像质量校正指南。

快速入门:自动发现理想的图像滤镜链

使用 IronOCR 的过滤器向导测试所有预处理过滤器组合,并获得性能最佳的代码片段。 一行返回您的最高置信度分数以及类似图片的准确 C# 过滤链。

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

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

    PM > Install-Package IronOcr

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

    string code = OcrInputFilterWizard.Run("image.png", out double confidence, new IronTesseract());
  3. 部署到您的生产环境中进行测试

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

过滤器向导如何工作?

OcrInputFilterWizard.Run 方法需要三个参数:输入图像、结果置信度的输出参数和 Tesseract 引擎实例。 有关高级引擎控制,请参阅我们的Tesseract详细配置指南。

它测试了预处理过滤器的多种组合,以获得最佳置信度得分。 最高置信度分数将决定对输入图像应用哪套过滤器。 这种方法在处理需要图像方向校正或其他复杂预处理步骤的高难度图像时非常有效。

过滤器向导没有预设或组合限制。 翻译的重点是通过全面的过滤测试获得尽可能高的置信度。 为了在处理过程中获得实时反馈,请实施进度跟踪以监控向导操作。

组合测试中的可用过滤器:

  • input.Contrast() - 调整对比度以提高文本清晰度
  • input.Sharpen() - 增强边缘定义
  • input.Binarize() - 转换为黑白图像
  • input.ToGrayScale() - 删除颜色信息
  • input.Invert() - 反转颜色
  • input.Deskew() - 纠正倾斜的文本
  • input.Scale(...) - 调整大小至最佳尺寸
  • input.Denoise() - 去除像素噪声
  • input.DeepCleanBackgroundNoise() - 高级噪声去除
  • input.EnhanceResolution() - 提高低质量分辨率
  • input.Dilate(), input.Erode() - 文本细化操作

有关详细的过滤器信息,请参阅此 图像过滤器教程图像校正滤镜指南中提供了其他预处理技术。

这种详尽的测试方法需要处理时间。对于大规模操作,请使用多线程支持同时处理多个图像。

我应该使用哪种类型的图片进行测试?

本示例使用一张带有严重人工噪音的截图来演示过滤器向导的功能。 过滤器向导可有效处理各种图像类型,从扫描文档带有文本的照片

严重破坏的测试图像,带有噪声模式,显示出退化的文本,用于过滤器向导演示

在选择测试图像时,应考虑以下因素:

  • 图像分辨率:DPI 越高的图像通常效果越好。 有关优化技巧,请参阅我们的 DPI 设置指南。
  • 文档类型:不同的文档类型受益于特定的过滤器组合。 身份文档可能需要进行与标准文本文档不同的预处理。
  • 源质量:过滤器向导擅长处理有问题的图像,但在可能的情况下,它也会从最高质量的可用源开始。

如何在我的代码中运行过滤器向导?

:path=/static-assets/ocr/content-code-examples/how-to/filter-wizard-process.cs
using IronOcr;
using System;

// Initialize the Tesseract engine
var ocr = new IronTesseract();

// 1. Pass the image path ("noise.png").
// 2. Pass an 'out' variable to store the best confidence score found.
// 3. Pass the tesseract instance to be used for testing.
string codeToRun = OcrInputFilterWizard.Run("noise.png", out double confidence, ocr);

// The 'confidence' variable is now populated with the highest score achieved.
Console.WriteLine($"Best Confidence Score: {confidence}");

// 'codeToRun' holds the exact C# code snippet that achieved this score.
// The returned string is the code you can use to filter similar images.
Console.WriteLine("Recommended Filter Code:");
Console.WriteLine(codeToRun);
Imports IronOcr
Imports System

' Initialize the Tesseract engine
Dim ocr As New IronTesseract()

' 1. Pass the image path ("noise.png").
' 2. Pass an 'out' variable to store the best confidence score found.
' 3. Pass the tesseract instance to be used for testing.
Dim confidence As Double
Dim codeToRun As String = OcrInputFilterWizard.Run("noise.png", confidence, ocr)

' The 'confidence' variable is now populated with the highest score achieved.
Console.WriteLine($"Best Confidence Score: {confidence}")

' 'codeToRun' holds the exact C# code snippet that achieved this score.
' The returned string is the code you can use to filter similar images.
Console.WriteLine("Recommended Filter Code:")
Console.WriteLine(codeToRun)
$vbLabelText   $csharpLabel

过滤器向导可处理各种输入格式。 有关支持的格式信息,请参阅我们的输入图像指南。 您还可以处理PDF文件,或直接使用处理动态图像源。

对于批处理场景,请参考以下扩展示例:

/* :path=/static-assets/ocr/content-code-examples/how-to/filter-wizard-batch.cs */
using IronOcr;
using System;
using System.IO;

// Process multiple similar images
var ocr = new IronTesseract();
string[] imageFiles = Directory.GetFiles(@"C:\Images", "*.png");

// Run Filter Wizard on first image to discover optimal settings
string optimalCode = OcrInputFilterWizard.Run(imageFiles[0], out double baselineConfidence, ocr);
Console.WriteLine($"Baseline confidence: {baselineConfidence:P2}");
Console.WriteLine($"Optimal filter sequence discovered");

// Apply discovered filters to all images
foreach (string imagePath in imageFiles)
{
    using (var input = new OcrImageInput(imagePath))
    {
        // Apply the filter sequence discovered by the wizard
        // The actual filters would be applied here based on the wizard output
        var result = ocr.Read(input);
        Console.WriteLine($"Processed: {Path.GetFileName(imagePath)} - Confidence: {result.Confidence:P2}");
    }
}
/* :path=/static-assets/ocr/content-code-examples/how-to/filter-wizard-batch.cs */
using IronOcr;
using System;
using System.IO;

// Process multiple similar images
var ocr = new IronTesseract();
string[] imageFiles = Directory.GetFiles(@"C:\Images", "*.png");

// Run Filter Wizard on first image to discover optimal settings
string optimalCode = OcrInputFilterWizard.Run(imageFiles[0], out double baselineConfidence, ocr);
Console.WriteLine($"Baseline confidence: {baselineConfidence:P2}");
Console.WriteLine($"Optimal filter sequence discovered");

// Apply discovered filters to all images
foreach (string imagePath in imageFiles)
{
    using (var input = new OcrImageInput(imagePath))
    {
        // Apply the filter sequence discovered by the wizard
        // The actual filters would be applied here based on the wizard output
        var result = ocr.Read(input);
        Console.WriteLine($"Processed: {Path.GetFileName(imagePath)} - Confidence: {result.Confidence:P2}");
    }
}
Imports IronOcr
Imports System
Imports System.IO

' Process multiple similar images
Dim ocr As New IronTesseract()
Dim imageFiles As String() = Directory.GetFiles("C:\Images", "*.png")

' Run Filter Wizard on first image to discover optimal settings
Dim baselineConfidence As Double
Dim optimalCode As String = OcrInputFilterWizard.Run(imageFiles(0), baselineConfidence, ocr)
Console.WriteLine($"Baseline confidence: {baselineConfidence:P2}")
Console.WriteLine("Optimal filter sequence discovered")

' Apply discovered filters to all images
For Each imagePath As String In imageFiles
    Using input As New OcrImageInput(imagePath)
        ' Apply the filter sequence discovered by the wizard
        ' The actual filters would be applied here based on the wizard output
        Dim result = ocr.Read(input)
        Console.WriteLine($"Processed: {Path.GetFileName(imagePath)} - Confidence: {result.Confidence:P2}")
    End Using
Next
$vbLabelText   $csharpLabel

过滤器向导将返回哪些结果?

过滤器向导控制台输出,显示 65% 的置信度得分和生成的带有图像处理方法的 C# 代码

过滤向导输出显示 65% 的置信度是该特定图像的最佳结果。 置信度分数是评估 OCR 准确性的关键指标。 了解有关 结果信心的更多信息,请参阅我们的专用指南。

输入图像包含极度失真和人工噪音。这展示了滤镜向导在具有挑战性的场景中的能力。 如需用于生产,请尽可能使用质量更高的源图像。

生成的代码片段提供

  • 精确的过滤顺序:操作顺序对获得最佳结果至关重要
  • 方法链:简洁、可读的代码,易于实现
  • 无需猜测参数:每个过滤器都经过配置,以获得最佳性能

如何应用推荐的过滤器组合?

运行过滤向导后,将提供的代码片段设置应用于输入图像,以验证结果和可信度。 这可确保在文档处理管道中的类似图像上获得可重现的结果。

如何实现推荐的代码?

:path=/static-assets/ocr/content-code-examples/how-to/filter-wizard-best-combination.cs
using IronOcr;
using System;

// Initialize the Tesseract engine
var ocrTesseract = new IronTesseract();

// Load the image into an OcrInput object
using (var input = new OcrImageInput("noise.png"))
{
    // Apply the exact filter chain recommended by the Wizard's output
    input.Invert();
    input.DeNoise();
    input.Contrast();
    input.AdaptiveThreshold();

    // Run OCR on the pre-processed image
    OcrResult result = ocrTesseract.Read(input);

    // Print the final result and confidence
    Console.WriteLine($"Result: {result.Text}");
    Console.WriteLine($"Confidence: {result.Confidence}");
}
Imports IronOcr
Imports System

' Initialize the Tesseract engine
Dim ocrTesseract As New IronTesseract()

' Load the image into an OcrInput object
Using input As New OcrImageInput("noise.png")
    ' Apply the exact filter chain recommended by the Wizard's output
    input.Invert()
    input.DeNoise()
    input.Contrast()
    input.AdaptiveThreshold()

    ' Run OCR on the pre-processed image
    Dim result As OcrResult = ocrTesseract.Read(input)

    ' Print the final result and confidence
    Console.WriteLine($"Result: {result.Text}")
    Console.WriteLine($"Confidence: {result.Confidence}")
End Using
$vbLabelText   $csharpLabel

筛选应用程序的顺序非常重要。 过滤器向导可确定要使用的过滤器及其最佳顺序。 这种智能排序方式使过滤器向导在复杂的预处理场景中发挥了重要作用。

为了加强对 OCR 过程的控制,可以考虑实施错误处理和验证:

/* :path=/static-assets/ocr/content-code-examples/how-to/filter-wizard-validation.cs */
using IronOcr;
using System;

var ocrEngine = new IronTesseract();

try
{
    using (var input = new OcrImageInput(@"C:\Images\document.png"))
    {
        // Apply Filter Wizard recommended sequence
        input.Invert();
        input.DeNoise();
        input.Contrast();
        input.AdaptiveThreshold();

        // Configure additional OCR settings
        ocrEngine.Configuration.ReadBarCodes = false;
        ocrEngine.Configuration.RenderSearchablePdf = true;

        // Perform OCR with timeout protection
        var result = ocrEngine.Read(input);

        // Validate results
        if (result.Confidence >= 0.6)
        {
            Console.WriteLine("OCR successful with high confidence");
            // Process the extracted text
        }
        else
        {
            Console.WriteLine("Low confidence result - consider manual review");
        }
    }
}
catch (Exception ex)
{
    Console.WriteLine($"OCR processing error: {ex.Message}");
}
/* :path=/static-assets/ocr/content-code-examples/how-to/filter-wizard-validation.cs */
using IronOcr;
using System;

var ocrEngine = new IronTesseract();

try
{
    using (var input = new OcrImageInput(@"C:\Images\document.png"))
    {
        // Apply Filter Wizard recommended sequence
        input.Invert();
        input.DeNoise();
        input.Contrast();
        input.AdaptiveThreshold();

        // Configure additional OCR settings
        ocrEngine.Configuration.ReadBarCodes = false;
        ocrEngine.Configuration.RenderSearchablePdf = true;

        // Perform OCR with timeout protection
        var result = ocrEngine.Read(input);

        // Validate results
        if (result.Confidence >= 0.6)
        {
            Console.WriteLine("OCR successful with high confidence");
            // Process the extracted text
        }
        else
        {
            Console.WriteLine("Low confidence result - consider manual review");
        }
    }
}
catch (Exception ex)
{
    Console.WriteLine($"OCR processing error: {ex.Message}");
}
Imports IronOcr
Imports System

Dim ocrEngine As New IronTesseract()

Try
    Using input As New OcrImageInput("C:\Images\document.png")
        ' Apply Filter Wizard recommended sequence
        input.Invert()
        input.DeNoise()
        input.Contrast()
        input.AdaptiveThreshold()

        ' Configure additional OCR settings
        ocrEngine.Configuration.ReadBarCodes = False
        ocrEngine.Configuration.RenderSearchablePdf = True

        ' Perform OCR with timeout protection
        Dim result = ocrEngine.Read(input)

        ' Validate results
        If result.Confidence >= 0.6 Then
            Console.WriteLine("OCR successful with high confidence")
            ' Process the extracted text
        Else
            Console.WriteLine("Low confidence result - consider manual review")
        End If
    End Using
Catch ex As Exception
    Console.WriteLine($"OCR processing error: {ex.Message}")
End Try
$vbLabelText   $csharpLabel

应用过滤器后的最终 OCR 结果如何?

显示 OCR 结果的终端:提取文本'Testing:应用过滤向导后,测试置信度为 65.61%

IronOCR 即使在严重扭曲的条件下也能提取大部分文本。 置信度与筛选向导的报告相匹配。 有关 OCR 结果处理的详细信息,请查阅我们的数据输出指南。

我应该考虑哪些高级使用技巧?

在生产中使用过滤器向导时,请考虑这些最佳实践:

1.批量处理:在具有代表性的样本上进行测试,然后将过滤链应用于类似的图像。

2.性能优化:过滤器向导非常全面但耗时。 如需更快的 OCR,请参阅快速 OCR 配置

3.自定义语言支持:对于非英语文本,探索 多种语言 以优化识别。

4.API集成:请访问我们的 API 参考,获取完整的文档。

5.特定文档优化:不同的文档类型可以从专门的方法中获益:

6.内存管理:使用 using 语句正确处理 OcrInput 对象。

7.错误恢复:针对低置信度结果实施后备策略。 考虑对关键文件进行人工审核。

过滤器向导提供强大的自动预处理发现功能,以获得最佳的 OCR 结果。 通过自动查找特定图像的最佳预处理管道,它消除了图像准备过程中的猜测,并确保在您的应用程序中进行一致、高质量的文本提取。

常见问题解答

什么是 OCR 过滤向导,它如何帮助进行图像预处理?

IronOCR 滤镜向导是一款自动化工具,可测试图像上所有可能的预处理滤镜组合,以找到最佳 OCR 设置。它通过自动评估各种过滤器组合,最大限度地提高 OCR 的可信度和准确度,然后将最佳过滤器组合作为即用型 C# 代码片段返回,从而消除了手动试错过程。

如何在 C# 应用程序中使用过滤器向导?

使用 IronOCR 的过滤向导非常简单,只需调用 OcrInputFilterWizard.Run(),并输入图像路径、置信度分数的输出参数和 IronTesseract 实例即可。例如:string code = OcrInputFilterWizard.Run("image.png", out double confidence, new IronTesseract());

OcrInputFilterWizard.Run 方法接受哪些参数?

IronOCR 中的 OcrInputFilterWizard.Run 方法需要三个参数:输入图像(作为文件路径)、返回结果置信度的 out 参数以及用于处理的 IronTesseract 引擎实例。

为什么要使用筛选器向导而不是手动测试筛选器?

手动预处理滤波器测试既耗时又具有挑战性,尤其是在低质量扫描或图像噪声水平不一的情况下。IronOCR 的过滤器向导可将这一过程自动化,它可对过滤器组合进行详尽测试,并返回置信度最高的分数,同时提供所需的精确 C# 代码,从而节省大量开发时间。

过滤器向导如何确定最佳过滤器组合?

IronOCR 的过滤器向导可在图像上测试多个预处理过滤器组合,并测量每个组合的 OCR 信任度得分。然后,它会选择置信度得分最高的过滤器组合,并将此最佳组合作为可执行的 C# 代码返回。

滤镜向导能否处理低质量或嘈杂的图像?

是的,IronOCR 的滤波向导对具有挑战性的图像特别有效,包括低质量扫描以及具有不同噪声和失真度的图像。它能自动找到最佳的预处理组合,即使在处理困难的源材料时,也能最大限度地提高 OCR 的准确性。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 5,384,824 | 版本: 2026.2 刚刚发布