如何使用IronOCR獲取C# OCR讀取信心水準
IronOCR的讀取信心水準表示OCR系統對已識別文字準確性的確定程度,範圍從0到100,分數越高表示可靠性越高——可以通過任何OcrResult屬性存取。
在OCR(光學字元識別)中,讀取信心水準指的是OCR系統對圖像或文件中已識別文字的準確性所賦予的確定性或可靠性水平。 這是衡量OCR系統對已識別文字是否正確的信心水準。 這個指標在處理掃描文件、照片或任何文字質量可能不同的圖像時顯得尤為重要。
高信心得分表示識別的準確性很高,而低信心得分則表明識別可能不太可靠。 了解這些信心水準有助於開發者在應用程式中實現適當的驗證邏輯和錯誤處理。
快速入門:在一行中獲取OCR讀取信心水準使用Confidence屬性,以查看IronOCR對其文字識別的確定性。 這是一個簡單可靠的方式來開始評估OCR輸出準確性。
-
1Install IronOCR with NuGet Package Manager
-
2複製並運行這段程式碼片段。
double confidence = new IronOcr.IronTesseract().Read("input.png").Confidence;C# -
3部署以在您的實時環境中測試
今天就開始在您的專案中使用IronOCR,透過免費試用
最小工作流程(5步)
- 下載C#程式庫以存取讀取信心水準
- 準備目標圖像和PDF文件
- 存取OCR結果的
Confidence屬性 - 檢索頁面、段落、行、單詞和字元的信心水準
- 檢查
Choices屬性以獲取替代單詞選擇
如何在C#中獲取讀取信心水準?
在對輸入圖像進行OCR後,文字的信心水準被儲存在Confidence屬性中。 使用'using'語句來自動釋放用過的物件。 分別使用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;Imports IronOcr
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Add image
Private imageInput = New OcrImageInput("sample.tiff")
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Get confidence level
Private confidence As Double = 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;' Get page confidence level
Dim pageConfidence As Double = ocrResult.Pages(0).Confidence
' Get paragraph confidence level
Dim paragraphConfidence As Double = ocrResult.Paragraphs(0).Confidence
' Get line confidence level
Dim lineConfidence As Double = ocrResult.Lines(0).Confidence
' Get word confidence level
Dim wordConfidence As Double = ocrResult.Words(0).Confidence
' Get character confidence level
Dim characterConfidence As Double = ocrResult.Characters(0).Confidence
' Get block confidence level
Dim blockConfidence As Double = 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}%)");
}Imports IronOcr
Imports System.Linq
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Configure for better accuracy
ocrTesseract.Configuration.ReadBarCodes = False
ocrTesseract.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd
' Add image
Using imageInput As New OcrImageInput("invoice.png")
' Apply filters to improve quality
imageInput.Deskew()
imageInput.DeNoise()
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Filter words with confidence above 85%
Dim highConfidenceWords = ocrResult.Words _
.Where(Function(word) word.Confidence >= 85) _
.Select(Function(word) word.Text) _
.ToList()
' Process only high-confidence text
Dim reliableText As String = String.Join(" ", highConfidenceWords)
Console.WriteLine($"High confidence text: {reliableText}")
' Flag low-confidence words for manual review
Dim lowConfidenceWords = ocrResult.Words _
.Where(Function(word) word.Confidence < 85) _
.Select(Function(word) New With {Key .Text = word.Text, Key .Confidence = word.Confidence}) _
.ToList()
For Each word In lowConfidenceWords
Console.WriteLine($"Review needed: '{word.Text}' (Confidence: {word.Confidence:F2}%)")
Next
End UsingOCR中的字元選擇是什麼?
除了信心水準外,還有另一個有趣的屬性叫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;Imports IronOcr
Imports IronOcr.OcrResult
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Add image
Private imageInput = New OcrImageInput("Potter.tiff")
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Get choices
Private choices() As Choice = ocrResult.Characters(0).Choices替代字元選擇如何提供幫助?
替代字元選擇提供多項好處:
- 疑義解決:當'O'和'0'、'l'和'1'混淆時
- 字體變化:對於樣式化或裝飾性字體的不同解釋
- 品質問題:處理劣化文字時的多個可能性
- 語言上下文:基於語言規則的替代解釋

使用字元選擇
這是一個全面的範例,展示如何利用字元選擇來提高準確性:
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}%");
}
}
}
}Imports IronOcr
Imports System
Imports System.Linq
Imports IronOcr.OcrResult
' Configure IronTesseract for detailed results
Dim ocrTesseract As New IronTesseract()
' Process image with potential ambiguities
Using imageInput As New OcrImageInput("ambiguous_text.png")
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Analyze character choices for each word
For Each word In ocrResult.Words
Console.WriteLine(vbCrLf & $"Word: '{word.Text}' (Confidence: {word.Confidence:F2}%)")
' Check each character in the word
For Each character In word.Characters
If character.Choices IsNot Nothing AndAlso character.Choices.Length > 1 Then
Console.WriteLine($" Character '{character.Text}' has alternatives:")
' Display all choices sorted by confidence
For Each choice In character.Choices.OrderByDescending(Function(c) c.Confidence)
Console.WriteLine($" - '{choice.Text}': {choice.Confidence:F2}%")
Next
End If
Next
Next
End Using高級信心策略
在處理專用文件(如護照、車牌或MICR支票)時,信心得分對於驗證尤為重要:
using IronOcr;
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}$");
}
}Imports IronOcr
Public Class DocumentValidator
Private ReadOnly ocr As New IronTesseract()
Public Function ValidatePassportNumber(imagePath As String, Optional minConfidence As Double = 95.0) As Boolean
Using input As 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
Dim result = ocr.Read(input)
' Find passport number pattern
Dim passportLine = result.Lines _
.Where(Function(line) line.Text.Contains("P<") OrElse IsPassportNumberFormat(line.Text)) _
.FirstOrDefault()
If passportLine IsNot Nothing Then
Console.WriteLine($"Passport line found: {passportLine.Text}")
Console.WriteLine($"Confidence: {passportLine.Confidence:F2}%")
' Only accept if confidence meets threshold
Return passportLine.Confidence >= minConfidence
End If
Return False
End Using
End Function
Private Function IsPassportNumberFormat(text As String) As Boolean
' Simple passport number validation
Return System.Text.RegularExpressions.Regex.IsMatch(text, "^[A-Z]\d{7,9}$")
End Function
End Class優化以獲得更好的信心
為了達到更高的信心水準,考慮使用圖像過濾和預處理技術:
using IronOcr;
// 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}");
}Imports IronOcr
' Create an optimized OCR workflow
Dim ocr As New IronTesseract()
Using input As 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
Dim result = ocr.Read(input)
Console.WriteLine($"Document confidence: {result.Confidence:F2}%")
' Generate confidence report
Dim confidenceReport = result.Pages _
.Select(Function(page, index) New With {
.PageNumber = index + 1,
.Confidence = page.Confidence,
.WordCount = page.Words.Length,
.LowConfidenceWords = page.Words.Count(Function(w) w.Confidence < 80)
})
For Each 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}")
Next
End Using總結
理解並利用OCR信心水準是構建強健的文件處理應用程式的關鍵。 通過利用IronOCR的信心屬性和字元選擇,開發者可以在OCR工作流程中實現智能驗證、錯誤處理和品質保證機制。 無論您是否在處理截圖、表格或專用文件,信心得分提供了必要的指標來確保準確的文字提取。
常見問題
什麼是 OCR 信心及其重要性?
OCR 信心是一種從 0 到 100 的衡量標準,表示 OCR 系統對文字識別準確性的確定程度。IronOCR 透過 OcrResult 物件上的 Confidence 屬性提供此指標,幫助開發人員評估識別文字的可靠性,特別是在處理掃描文件、照片或文字品質不均的圖像時。
如何在 C# 中快速檢查 OCR 信心?
using IronOCR,您只需一行程式碼即可獲取 OCR 信心:double confidence = new IronOcr.IronTesseract().Read("input.png").Confidence; 這會返回一個 0-100 的信心評分,指示 IronOCR 對其文字識別的確信程度。
不同的信心評分範圍代表什麼意思?
IronOCR 信心評分表示:90-100(極好) 表示文字高度可靠;80-89(良好)表示文字一般準確但有小的不確定性;70-79(中等)表示文字可能包含一些錯誤;低於 70(低)表示文字應進行檢查或重新處理。
如何存取不同文字元素的信心水平?
IronOCR 允許您以多種精細度檢索信心水平——頁面、段落、行、單詞和個別字元。在執行 OCR 後,您可以透過 OcrResult 物件結構存取每個層級的 Confidence 屬性。
我可以獲得帶有信心評分的替代單詞建議嗎?
是的,IronOCR 提供一個 Choices 屬性,提供替代單詞選擇及其信心評分。當 OCR 引擎識別相同文字的多個可能解釋時,此功能有助於實現智能驗證邏輯。
如何在應用程式中實施基於信心的驗證?
在使用 IronOCR 的 Read 方法後,檢查 OcrResult 的 Confidence 屬性。基於信心水準設定條件邏輯——例如,自動接受高於 90 的結果,標記 70-90 之間的結果供審查,而對低於 70 的結果進行重新處理或手動驗證。
What are character choices in IronOCR?
Character choices in IronOCR refer to alternative word choices and their relevance metrics provided during OCR. This feature is useful for handling ambiguities and understanding potential character variations in the text.
How can character choices aid in text recognition?
Character choices help resolve ambiguities by offering alternative interpretations for similar-looking characters and words. This is particularly beneficial when dealing with font variations, degraded text, and multiple languages.
What preprocessing techniques improve IronOCR text accuracy?
Preprocessing techniques such as deskewing, denoising, sharpening, and scaling can significantly enhance text accuracy by improving image quality before executing OCR processes with IronOCR.
How does IronOCR handle low confidence recognition results?
IronOCR allows you to filter and flag low-confidence recognition results for review. You can identify words with confidence scores below a certain threshold and address uncertainties by using validation or reprocessing strategies.

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