如何在 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. using 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 Engine 實例。 如需進階引擎控制功能,請參閱我們關於 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() - 文字潤飾操作

如需詳細的濾鏡資訊,請參閱這篇關於影像濾鏡的教學文章。 更多預處理技術請參閱影像校正濾鏡指南。

此全面測試方法需要處理時間。針對大規模操作,請使用多執行緒支援功能以同時處理多張圖片。

測試時應使用哪種類型的圖片?

此範例使用一張帶有大量人工雜訊的螢幕截圖,用以展示濾鏡精靈的功能。 "濾鏡精靈"能有效處理各種圖像類型,從掃描文件含有文字的照片皆可。

用於 Filter Wizard 示範的嚴重損壞測試圖像,圖中雜訊圖案顯示文字品質已劣化

選擇測試圖片時,請考量以下因素:

  • 圖片解析度: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

篩選精靈將返回哪些結果?

Filter Wizard 控制台輸出顯示 65% 的信心分數,以及包含影像處理方法的生成的 C# 程式碼

濾鏡精靈的輸出顯示,針對這張特定圖片,65% 的信心度是可達到的最佳結果。 信心分數是評估 OCR 準確度的關鍵指標。 請參閱我們的專用指南,進一步了解結果可信度

原始圖片含有極度變形與人為雜訊。此案例旨在展示 Filter Wizard 在艱鉅情境下的處理能力。 若用於正式生產環境,請盡可能選用更高品質的原始圖片。

生成的程式碼片段提供:

  • 精確的篩選順序:操作順序對取得最佳結果至關重要
  • 方法鏈接:簡潔、易讀且易於實作的程式碼
  • 無需猜測參數:每個篩選器皆已針對最佳效能進行配置

如何套用建議的篩選器組合?

執行篩選精靈後,請將提供的程式碼片段設定套用至您的輸入圖片,以驗證結果與信心分數。 這可確保在您的文件處理流程中,針對類似的圖像能獲得可重現的結果。

如何實作建議的程式碼?

: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 結果的終端機:應用篩選精靈後,以 65.61% 的信心水準擷取的文字為

IronOCR 即使在嚴重變形的條件下,也能提取大部分文字。 信心等級與篩選精靈的報告一致。 有關 OCR 結果處理的詳細說明,請參閱我們的資料輸出指南。

我應考慮哪些進階使用技巧?

在生產環境中使用"篩選精靈"時,請遵循以下最佳實務:

  1. 批次處理:先在具代表性的樣本上進行測試,然後將濾鏡鏈套用至類似的圖片。

  2. 效能優化:篩選精靈功能雖全面,但耗時較長。 如需更快的 OCR 速度,請參閱快速 OCR 設定

  3. 自訂語言支援:針對非英文文本,請嘗試多種語言以優化辨識效果。

  4. API 整合:請參閱我們的 API 參考文件以獲取完整說明。

  5. 文件類型的特定優化:不同類型的文件需要採用專門的處理方法:

  6. 記憶體管理:請使用 using 語句妥善釋放 OcrInput 物件。

  7. 錯誤復原:針對信心度較低的結果,實作備用策略。 針對重要文件,請考慮進行人工審核。

"濾鏡精靈"提供強大的自動化預處理偵測功能,以獲得最佳的 OCR 結果。 透過自動為您的特定影像找出最佳預處理流程,它能消除影像準備過程中的試錯,並確保在您的應用程式中實現一致且高品質的文字擷取。

常見問題

什麼是 OCR 濾鏡精靈?它如何協助進行影像預處理?

IronOCR 濾鏡精靈是一款自動化工具,可針對您的影像測試所有可能的預處理濾鏡組合,以找出最佳的 OCR 設定。它透過自動評估各種濾鏡組合來最大化 OCR 的信心值與準確度,從而消除手動試錯的過程,並將最佳濾鏡組合以可直接使用的 C# 程式碼片段形式回傳。

如何在我的 C# 應用程式中使用「篩選精靈」?

using IronOCR 的濾鏡精靈非常簡單——只需透過 OcrInputFilterWizard.Run() 方法,傳入您的圖片路徑、用於傳回信心分數的 out 參數,以及一個 IronTesseract 實例。例如:string code = OcrInputFilterWizard.Run("image.png", out double confidence, new IronTesseract());

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

IronOCR 中的 OcrInputFilterWizard.Run 方法接受三個參數:輸入影像(以檔案路徑形式提供)、一個返回結果置信度的輸出參數,以及用於處理的 IronTesseract Engine 實例。

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

手動進行預處理濾鏡測試既耗時又具挑戰性,尤其面對掃描品質不佳或雜訊程度不一的影像時更是如此。IronOCR 的「濾鏡精靈」能透過全面測試各種濾鏡組合,並回傳信心分數最高的結果及所需的精確 C# 程式碼,從而自動化此流程,大幅節省開發時間。

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

IronOCR 的「濾鏡精靈」會對您的影像測試多種預處理濾鏡的組合,並測量每種組合的 OCR 信心分數。接著,它會選取信心分數最高的濾鏡組合,並將此最佳組合以可執行的 C# 程式碼形式回傳。

「濾鏡精靈」能否處理畫質不佳或雜訊較多的圖片?

是的,IronOCR 的「濾鏡精靈」對於處理具挑戰性的圖像特別有效,包括低畫質掃描檔以及噪點和變形程度各異的圖像。即使面對困難的原始素材,它也能自動找出最佳的前處理組合,以最大化 OCR 準確度。

IronOCR 能否整合至現有應用程式中?

IronOCR 設計上可輕鬆透過 C# 整合至現有應用程式中,讓開發人員能以最少的努力,為其軟體增添 OCR 功能。

使用 IronOCR 進行文件管理有哪些好處?

使用 IronOCR 進行文件管理,可將掃描文件轉換為可搜尋且可編輯的文字,從而簡化工作流程,減少人工資料輸入的需求,並提升文件的可存取性。

IronOCR 如何提升資料準確性?

IronOCR 透過其先進的辨識演算法與影像校正功能來提升資料準確性,確保文字擷取過程既可靠又精確。

IronOCR 是否有提供免費試用版?

是的,Iron Software 提供 IronOCR 的免費試用版,讓使用者能在決定購買前測試其功能與效能。

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
Nuget 下載 5,896,332 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronOcr
執行範例 觀看您的圖片轉為可搜尋文字。