如何使用 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 檔案,或直接處理動態影像來源的 streams

針對批次處理情境,請考慮此擴充範例:

/* :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 Engine 實例。

為何要使用篩選精靈而非手動測試篩選器?

手動預處理濾鏡測試既耗時又具挑戰性,尤其是低品質掃描或具有不同雜訊等級的影像。IronOCR 的篩選精靈可將此流程自動化,透過徹底測試篩選器組合,並返回最高置信度分數與所需的精確 C# 程式碼,大幅節省開發時間。

篩選精靈如何決定最佳的篩選組合?

IronOCR 的篩選精靈會在您的圖像上測試多種預處理篩選器組合,並測量每種組合的 OCR 信賴度分數。然後,它會選擇達到最高置信度分數的篩選器組合,並將此最佳組合以可執行 C# 程式碼的方式傳回。

篩選精靈可以處理低品質或有雜訊的影像嗎?

是的,IronOCR 的篩選精靈對於具有挑戰性的影像特別有效,包括低品質掃描以及具有不同雜訊和失真程度的影像。它可以自動找到最佳的預處理組合,即使是對於困難的原始資料,也能最大限度地提高 OCR 精確度。

Curtis Chau
技術撰稿人

Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。

準備好開始了嗎?
Nuget 下載 5,384,824 | 版本: 2026.2 剛剛發布