如何在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#篩選器鏈。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/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-3.cs
/* :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-5.cs
/* :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結果:應用篩選器精靈後以65.61%信心提取的文字'測試:測試中'

IronOCR即使在嚴重失真的情況下也可提取大部分文字。 信心層級符合篩選器精靈的報告。 欲了解OCR結果處理的詳細資訊,請參閱我們的資料輸出指南

我應該考慮哪些進階使用提示?

在生產中使用篩選器精靈時,請考慮以下最佳實踐:

  1. 批量處理:在代表樣本上測試,然後將篩選器鏈應用於類似圖像。

  2. 性能優化:篩選器精靈徹底但耗時。 快速OCR,請參見快速OCR配置

  3. 自訂語言支援:對於非英語文字,探索多語言方案以優化識別。

  4. API整合:存取我們的API參考以獲取完整文件。

  5. 針對文件的優化:不同的文件型別會得益於專門的處理方法:

  6. 記憶體管理:使用OcrInput物件。

  7. 錯誤恢復:針對低信心結果實施備援策略。 考慮手動審核重要文件。

篩選器精靈提供強大的自動預處理發現以獲得最佳OCR結果。 通過自動為您的特定影像找到最佳的預處理流程,它排除了影像準備中的猜測確保在您的應用中一致性和高品質的文字提取。

常見問題

OCR 濾鏡精靈是什麼以及它如何幫助影像預處理?

IronOCR 濾鏡精靈是一個自動化工具,可以測試您影像上所有可能的預處理濾鏡組合,以尋找最佳的 OCR 設置。它通過自動評估各種濾鏡組合以達到最大化的 OCR 信心和準確性,從而消除了手動試錯過程,然後將最佳濾鏡組合返回為即用的 C# 程式碼片段。

如何在我的 C# 應用程式中使用濾鏡精靈?

using IronOCR 的濾鏡精靈非常簡單 - 只需調用 OcrInputFilterWizard.Run() 並輸入圖像路徑、輸出參數以獲取信心分數,以及一個 IronTesseract 實例。例如:string code = OcrInputFilterWizard.Run("image.png", out double confidence, new IronTesseract());

OcrInputFilterWizard.Run 方法接受哪些參數?

IronOCR 中的 OcrInputFilterWizard.Run 方法需要三個參數:輸入影像(作為檔案路徑)、返回結果信心水平的輸出參數,以及用於處理的 IronTesseract 引擎實例。

為什麼我應該使用濾鏡精靈而不是手動測試濾鏡?

手動預處理濾鏡測試既耗時又具有挑戰性,尤其是在低質量掃描或具有不同噪聲水平的影像中。IronOCR 的濾鏡精靈通過徹底測試濾鏡組合並返回最高信心分數以及所需的精確 C# 程式碼,大大節省了開發時間。

濾鏡精靈如何確定最佳濾鏡組合?

IronOCR 的濾鏡精靈會在您的影像上測試多種預處理濾鏡組合,並測量每種組合的 OCR 信心分數。然後,它選擇達到最高信心分數的濾鏡組合,並將這個最佳組合返回為可執行的 C# 程式碼。

濾鏡精靈可以處理低質量或噪聲的影像嗎?

是的,IronOCR 的濾鏡精靈特別有效於包括低質量掃描和具有不同噪聲和失真水平的影像自動尋找最佳預處理組合,以最大化 OCR 的準確性,即使面對困難的原始資料。

IronOCR能整合到現有的應用程式中嗎?

IronOCR被設計成可以輕鬆地整合到現有應用程式中,使用C#允許開發人員以最小的努力為其軟體新增OCR功能。

使用IronOCR進行文件管理的好處是什麼?

使用IronOCR進行文件管理通過將掃描的文件轉換為可搜索和可編輯的文字來簡化工作流程,減少手動資料輸入的需求並提高文件的可存取性。

IronOCR如何提高資料精確性?

IronOCR通過其先進的識別算法和影像校正功能提高資料精確性,確保文字提取過程既可靠又精確。

IronOCR有免費試用版嗎?

有的,Iron Software提供IronOCR的免費試用版,允許使用者在做出購買決定前測試其功能和能力。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備開始了嗎?
Nuget 下載 6,151,372 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在滾動?

想要快速證明? PM > Install-Package IronOcr
執行範例 觀看您的圖像轉變為可搜尋文字。