如何使用 IronOCR 取得 C# OCR 讀取信心度

Curtis Chau
Curtis Chau
已更新: 2026年8月2日

IronOCR 的讀取信心度表示 OCR 系統對文字辨識結果的把握程度。分數介於 0 和 100 之間,分數越高,結果越可靠。您可以透過任何 OcrResult 物件的 Confidence 屬性取得此值。

OCR(光學字元辨識)的讀取信心度衡量系統對影像或文件中文字辨識結果的把握程度。處理掃描文件、照片或其他文字品質可能不一的影像時,這項指標尤其重要。

信心度分數高,表示系統對辨識結果較有把握。分數低則表示結果可能較不可靠。了解這些分數有助於開發人員在應用程式中實作適當的驗證邏輯和錯誤處理。

快速入門:用一行程式碼取得 OCR 讀取信心度

將影像檔案路徑傳給 IronTesseract 的 Read 方法,再從回傳的 OcrResult 讀取 Confidence 屬性,即可得知 IronOCR 對文字辨識結果的把握程度。這是初步評估 OCR 辨識準確度的簡單可靠方法。

  1. 1使用 IronOCR 安裝 NuGet Package Manager

    PM > Install-Package IronOcr

  2. 2複製並運行這段程式碼。

    double confidence = new IronOcr.IronTesseract().Read("input.png").Confidence;
  3. 3部署到您的生產環境進行測試

    立即在您的項目中開始使用 IronOCR,並使用 免費試用
    arrow pointer

如何在 C# 中取得讀取信心度?

對輸入影像執行 OCR 後,文字辨識信心度會儲存在 Confidence 屬性中。使用「using」陳述式可在物件用完後自動釋放資源。請分別使用 OcrImageInput 和 OcrPdfInput 類別加入影像與 PDF 文件。Read 方法會回傳 OcrResult物件,您可從中存取 Confidence 屬性。

using IronOcr;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Add image
using var imageInput = new OcrImageInput("sample.tiff");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Get confidence level
double confidence = ocrResult.Confidence;

回傳的信心度分數介於 0 和 100 之間:

  • 90-100: 信心度極高,文字辨識結果非常可靠
  • 80-89: 信心度高,文字大致正確,僅少數部分有疑慮
  • 70-79: 信心度中等,文字可能有些錯誤
  • 低於 70: 信心度低,應檢查或重新處理文字

如何取得不同層級的信心度?

您可以取得整份文件、每個頁面、段落、行、單字和字元的信心度,也可以取得區塊的信心度。每個區塊由一個或多個位置相近的段落組成。

// Get page confidence level
double pageConfidence = ocrResult.Pages[0].Confidence;

// Get paragraph confidence level
double paragraphConfidence = ocrResult.Paragraphs[0].Confidence;

// Get line confidence level
double lineConfidence = ocrResult.Lines[0].Confidence;

// Get word confidence level
double wordConfidence = ocrResult.Words[0].Confidence;

// Get character confidence level
double characterConfidence = ocrResult.Characters[0].Confidence;

// Get block confidence level
double blockConfidence = ocrResult.Blocks[0].Confidence;

實用範例:根據信心度篩選

當處理品質不同的文件時,例如低品質掃描,您可以使用信心度分數來篩選結果:

using IronOcr;
using System.Linq;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Configure for better accuracy
ocrTesseract.Configuration.ReadBarCodes = false;
ocrTesseract.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd;

// Add image
using var imageInput = new OcrImageInput("invoice.png");
// Apply filters to improve quality
imageInput.Deskew();
imageInput.DeNoise();

// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Filter words with confidence above 85%
var highConfidenceWords = ocrResult.Words
    .Where(word => word.Confidence >= 85)
    .Select(word => word.Text)
    .ToList();

// Process only high-confidence text
string reliableText = string.Join(" ", highConfidenceWords);
Console.WriteLine($"High confidence text: {reliableText}");

// Flag low-confidence words for manual review
var lowConfidenceWords = ocrResult.Words
    .Where(word => word.Confidence < 85)
    .Select(word => new { word.Text, word.Confidence })
    .ToList();

foreach (var word in lowConfidenceWords)
{
    Console.WriteLine($"Review needed: '{word.Text}' (Confidence: {word.Confidence:F2}%)");
}

OCR中的字元選擇是什麼?

除了信心度,Choices 屬性還提供候選字詞清單及其統計相關性,讓您查看其他可能的字元。處理多種語言或特殊字型時,這項功能尤其實用。

using IronOcr;
using static IronOcr.OcrResult;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Add image
using var imageInput = new OcrImageInput("Potter.tiff");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Get choices
Choice[] choices = ocrResult.Characters[0].Choices;

候選字元有何幫助?

候選字元可用於下列情境:

  1. 釐清易混淆的字元: 例如「O」和「0」,或「l」和「1」
  2. 辨識不同字型: 提供風格化或裝飾性字型的其他辨識結果
  3. 處理品質不佳的文字: 提供多種可能的辨識結果
  4. 語言脈絡: 依語言規則提供其他可能的辨識結果
OCR 候選字元的除錯畫面,顯示「Chapter Eight」的信心度分數及文字辨識結果

使用候選字元

以下是一個綜合範例,示範如何使用候選字元提高準確度:

using IronOcr;
using System;
using System.Linq;
using static IronOcr.OcrResult;

// Configure IronTesseract for detailed results
IronTesseract ocrTesseract = new IronTesseract();

// Process image with potential ambiguities
using var imageInput = new OcrImageInput("ambiguous_text.png");
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Analyze character choices for each word
foreach (var word in ocrResult.Words)
{
    Console.WriteLine($"\nWord: '{word.Text}' (Confidence: {word.Confidence:F2}%)");
    
    // Check each character in the word
    foreach (var character in word.Characters)
    {
        if (character.Choices != null && character.Choices.Length > 1)
        {
            Console.WriteLine($"  Character '{character.Text}' has alternatives:");
            
            // Display all choices sorted by confidence
            foreach (var choice in character.Choices.OrderByDescending(c => c.Confidence))
            {
                Console.WriteLine($"    - '{choice.Text}': {choice.Confidence:F2}%");
            }
        }
    }
}

信心度的進階用法

處理特定類型的文件時,例如護照、車牌或 MICR 支票,信心度分數是重要的驗證依據:

using IronOcr;
using System;
using System.Linq;

public class DocumentValidator
{
    private readonly IronTesseract ocr = new IronTesseract();
    
    public bool ValidatePassportNumber(string imagePath, double minConfidence = 95.0)
    {
        using var input = new OcrImageInput(imagePath);
        
        // Configure for passport reading
        ocr.Configuration.ReadBarCodes = true;
        ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.SingleLine;
        
        // Apply preprocessing
        input.Deskew();
        input.Scale(200); // Upscale for better accuracy
        
        var result = ocr.Read(input);
        
        // Find passport number pattern
        var passportLine = result.Lines
            .Where(line => line.Text.Contains("P<") || IsPassportNumberFormat(line.Text))
            .FirstOrDefault();
        
        if (passportLine != null)
        {
            Console.WriteLine($"Passport line found: {passportLine.Text}");
            Console.WriteLine($"Confidence: {passportLine.Confidence:F2}%");
            
            // Only accept if confidence meets threshold
            return passportLine.Confidence >= minConfidence;
        }
        
        return false;
    }
    
    private bool IsPassportNumberFormat(string text)
    {
        // Simple passport number validation
        return System.Text.RegularExpressions.Regex.IsMatch(text, @"^[A-Z]\d{7,9}$");
    }
}

如何提高信心度

若要提高信心度分數,可以使用影像濾鏡和前處理技術:

using IronOcr;
using System;
using System.Linq;

// Create an optimized OCR workflow
IronTesseract ocr = new IronTesseract();

using var input = new OcrImageInput("low_quality_scan.jpg");

// Apply multiple filters to improve confidence
input.Deskew();           // Correct rotation
input.DeNoise();          // Remove noise
input.Sharpen();          // Enhance edges
input.Dilate();           // Thicken text
input.Scale(150);         // Upscale for clarity

// Configure for accuracy over speed
ocr.Configuration.TesseractVersion = TesseractVersion.Tesseract5;
ocr.Configuration.EngineMode = TesseractEngineMode.TesseractOnly;

var result = ocr.Read(input);

Console.WriteLine($"Document confidence: {result.Confidence:F2}%");

// Generate confidence report
var confidenceReport = result.Pages
    .Select((page, index) => new
    {
        PageNumber = index + 1,
        Confidence = page.Confidence,
        WordCount = page.Words.Length,
        LowConfidenceWords = page.Words.Count(w => w.Confidence < 80)
    });

foreach (var page in confidenceReport)
{
    Console.WriteLine($"Page {page.PageNumber}: {page.Confidence:F2}% confidence");
    Console.WriteLine($"  Total words: {page.WordCount}");
    Console.WriteLine($"  Low confidence words: {page.LowConfidenceWords}");
}

總結

了解並運用 OCR 信心度分數,有助於建立可靠的文件處理應用程式。開發人員可運用 IronOCR 的信心度屬性和候選字元,在 OCR 流程中實作智慧型驗證、錯誤處理及品質保證機制。無論是處理截圖、表格或特定類型的文件,信心度分數都可作為確認文字擷取結果是否準確的依據。

常見問題

什麼是IronOCR中的OCR閱讀信心分數?

IronOCR中的OCR閱讀信心分數表示文字識別過程的確定性。它使用從0到100的信心分數,其中較高的分數表示識別文字的準確性較高。

我該如何在IronOCR中取得信心分數?

您可以透過使用IronTesseract.Read方法返回的OcrResult物件的Confidence屬性來取得IronOCR的信心分數。

為什麼信心分數在OCR中很重要?

信心分數很重要,因為它有助於評估識別文字的可靠性。理解這些分數可以讓開發者實施適當的驗證和錯誤處理,以提高OCR應用中的準確性。

高的OCR信心分數代表什麼意義?

高的OCR信心分數代表在文字識別準確性上有高度的確定性,意味著提取的文字可能是正確且可靠的。

IronOCR能為不同層級的文字提供信心分數嗎?

是的,IronOCR可以為多個層級提供信心分數,包括整份文件、頁面、段落、行、單詞及字符,允許進行詳細的準確性評估。

如何利用IronOCR提高OCR信心分數?

要提高IronOCR的OCR信心分數,您可以在執行OCR過程前應用如傾斜校正、去噪和影像放大等預處理技術。此外,配置IronTesseract以獲得最佳設定也可以提高準確性。

什麼是IronOCR中的字符選擇?

IronOCR中的字符選擇指的是在OCR過程中提供的替代單詞選擇及其相關性度量。此功能對於處理不確定性和理解文字中可能的字符變化很有幫助。

字符選擇如何有助於文字識別?

字符選擇透過為相似外觀的字符和單詞提供替代解釋來解決不確定性。這在處理字體變化、降級文本和多種語言時尤其有益。

哪些預處理技術可以提高IronOCR的文字準確性?

像是傾斜校正、去噪、銳化和縮放的預處理技術可以通過在使用IronOCR執行OCR過程前提高影像品質來顯著提升文字準確性。

IronOCR如何處理低信心識別結果?

IronOCR允許您篩選和標記低信心識別結果以供檢查。您可以識別信心分數低於某個臨界值的單詞,並通過使用驗證或重新處理策略來解決不確定性。

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

閱讀更多

準備好開始了嗎?

NuGet 下載次數 6,411,693版本:2026.11剛剛發布