如何在 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 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() - 文字細化操作

有關濾鏡的詳細信息,請參閱這篇關於圖像濾鏡的教程影像校正濾鏡指南中提供了其他預處理技術。

這種詳盡的測試方法需要一定的處理時間。對於大規模操作,請使用多執行緒支援同時處理多個映像。

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

此範例使用具有大量人工雜訊的螢幕截圖來示範篩選精靈的功能。 篩選精靈可以有效地處理各種影像類型,從掃描文件帶有文字的照片

嚴重損壞的測試影像,雜訊圖案導致文字模糊不清,用於演示濾鏡精靈。

選擇測試影像時,請考慮以下因素: -影像解析度: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);
$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}");
    }
}
$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}");
}
$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}");
}
$vbLabelText   $csharpLabel

應用濾鏡後,最終的OCR結果是什麼?

終端顯示 OCR 結果:應用篩選精靈後,擷取的文字為

即使在嚴重失真的情況下, IronOCR也能提取大部分文字。 置信度與篩選嚮導的報告相符。 有關詳細的 OCR 結果處理,請參閱我們的資料輸出指南。

我應該注意哪些高級使用技巧?

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

1.批量處理:對代表性樣本進行測試,然後將濾波器鏈應用於類似的圖像。

2.效能優化:篩選嚮導功能全面,但耗時較長。 如需更快的 OCR 識別,請參閱快速 OCR 設定

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

  1. 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 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 5,556,263 | 版本: 2026.3 剛剛發布
Still Scrolling Icon

還在捲動嗎?

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