如何在OCR中使用C#設定DPI
在IronOCR的OcrInput中設置TargetDPI屬性,以提升低解析度圖像,從而提高OCR準確度。 即使解析度低至 100 DPI,此配置也能大幅改善模糊或像素化文件的文字辨識能力。
每英吋點數 (DPI) 量測影像品質,並決定掃描文件或數位照片的詳細程度。雖然掃描文件既快速又有效率,但掃描過程通常會產生低解析度的檔案,尤其是使用預設或快速掃描設定時。 由於缺乏細節,文字顯得模糊或像素化,造成資料擷取的障礙。
OCR 效能取決於影像品質。 OCR 引擎會分析字形和模式,將其轉換為機器可讀的文字。 當圖像的 DPI 偏低時,就沒有足夠的像素來清楚定義每個字母,造成細節遺失,導致結果不準確。 對於處理掃描文件或舊的數位化系統的開發者,了解DPI優化對於可靠的文字擷取至關重要。
IronOCR 有效地處理了這些挑戰。 它能在解析度低至 225 DPI 的掃描上達到高準確度。 程式庫的影像預處理能力可自動偵測並補償各種影像品質問題,適合用於處理多樣來源的文件。 無論是在單行代碼中實現簡單的OCR,還是構建複雜的文件處理管道,DPI優化仍然是關鍵因子。
快速開始:設置TargetDPI以獲得更清晰的OCR結果配置IronOCR以一行程式碼升級低解析度影像,從而以最小的努力改善文字清晰度和識別度。
-
1使用 IronOCR 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
var input = new IronOcr.OcrInput { TargetDPI = 300 }; input.LoadImage("low-res.png"); var result = new IronOcr.IronTesseract().Read(input);Dim input = New IronOcr.OcrInput With {.TargetDPI = 300} input.LoadImage("low-res.png") Dim result = (New IronOcr.IronTesseract()).Read(input)Dim input = New IronOcr.OcrInput With {.TargetDPI = 300} input.LoadImage("low-res.png") Dim result = (New IronOcr.IronTesseract()).Read(input) -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronOCR,並使用 免費試用
最小工作流程(5 個步驟)
- 下載C#程式庫以設定OCR的DPI設置
- 實例化一個新的
OcrInput - 根據您的需求設定
TargetDPI - 載入所需的圖像
- 使用
Read從影像中讀取和提取資料。
如何設定DPI以改善OCR結果?
此示例使用約100 DPI的低解析度樣本圖像並加入人工噪音,以演示TargetDPI功能的效果。 了解如何配置DPI設置是使用OcrInput類時必不可少的,該類提供對影像預處理的全面控制。
圖片中的實際文字是:「測試 測試 測試 模糊文字範例 範例 範例」。

我需要什麼程式碼來升級影像DPI?
在此示例中,我們將TargetDPI設置為300以提高圖像解析度。 然後,我們將載入輸入圖像,並使用Text打印提取的文本,使用Confidence打印置信水平。 最佳 DPI 設定通常介於 250-400 DPI 之間,視您的使用情況和原始素材品質而定。 對於涉及多個影像濾鏡的高階方案,請參閱我們的OCR影像優化濾鏡指南。
using IronOcr;
using System;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
// Set the target DPI to 300 for better OCR accuracy
ocrInput.TargetDPI = 300;
ocrInput.LoadImage(@"images\image.png");
// Perform OCR on the image with the specified DPI
var ocrResult = ocrTesseract.Read(ocrInput);
// Display the text extracted from the image
Console.WriteLine(ocrResult.Text);
// Display the confidence level of the OCR result
Console.WriteLine(ocrResult.Confidence);using IronOcr;
using System;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
// Set the target DPI to 300 for better OCR accuracy
ocrInput.TargetDPI = 300;
ocrInput.LoadImage(@"images\image.png");
// Perform OCR on the image with the specified DPI
var ocrResult = ocrTesseract.Read(ocrInput);
// Display the text extracted from the image
Console.WriteLine(ocrResult.Text);
// Display the confidence level of the OCR result
Console.WriteLine(ocrResult.Confidence);Imports IronOcr
Imports System
Dim ocrTesseract As New IronTesseract()
Using ocrInput As New OcrInput()
' Set the target DPI to 300 for better OCR accuracy
ocrInput.TargetDPI = 300
ocrInput.LoadImage("images\image.png")
' Perform OCR on the image with the specified DPI
Dim ocrResult = ocrTesseract.Read(ocrInput)
' Display the text extracted from the image
Console.WriteLine(ocrResult.Text)
' Display the confidence level of the OCR result
Console.WriteLine(ocrResult.Confidence)
End UsingImports IronOcr
Imports System
Dim ocrTesseract As New IronTesseract()
Using ocrInput As New OcrInput()
' Set the target DPI to 300 for better OCR accuracy
ocrInput.TargetDPI = 300
ocrInput.LoadImage("images\image.png")
' Perform OCR on the image with the specified DPI
Dim ocrResult = ocrTesseract.Read(ocrInput)
' Display the text extracted from the image
Console.WriteLine(ocrResult.Text)
' Display the confidence level of the OCR result
Console.WriteLine(ocrResult.Confidence)
End Using升級DPI後我能期待什麼結果?

輸出結果顯示 IronOCR 達到 85% 的置信度。 儘管有顯著的雜訊,而且來源影像的初始 DPI 很低,結果還是很精確,證明了升頻功能的有效性。 對於需要更高準確度的應用,考慮實施進度追蹤以即時監控信心指數並動態調整設置。
IronOCR 包括內建功能,可在單一程式庫中透過升頻自動增強低解析度影像。 此方法消除了對外部影像處理工具的需求,簡化了您的工作流程並減少了依賴性。 對於在Windows上開始使用IronOCR的開發者,該功能不用額外配置即可立即工作。
DPI設置如何影響OCR準確性?
為說明差異,這是未設置TargetDPI屬性的低解析度圖像處理結果。 此比較說明了為何正確的 DPI 設定對於可靠的文字擷取至關重要。
找到正確的 DPI 需要平衡。 高 DPI 可提供更高的精確度,但處理速度較慢; 低DPI速度較快,但穩定性較差。 有疑問時,讓IronTesseract的自動預處理決定最佳設定。 您可以透過將TargetDPI屬性設定為0來禁用自動放大。對於專業應用,請參閱我們關於Tesseract圖像DPI優化的指南。
不進行DPI升級會發生什麼?

如果不進行放大,置信度分數會下降到 79%,擷取的文字準確度也會明顯降低。 此比較強調設定TargetDPI如何改善低品質圖像的OCR結果。 在處理包含表格、表單或多列版面的複雜文件時,差異會變得更加明顯。
如何為PDF文件設置DPI?
在處理 PDF 時,IronOCR 會將整個文件升頻至目標 DPI,而不只是其中的影像。 這種全面的方法確保文件始終如一的品質,這對於商務應用中的PDF OCR文字擷取至關重要。 對於高階PDF處理,例如操作可搜尋PDF,正確的DPI配置變得更加重要。
雖然較高的 DPI 通常會帶來較好的 OCR 結果,但不同 PDF 的最佳設定會有所不同。 如果您不確定該使用哪個值,請將 DPI 設定為預設值,讓 IronOCR 自動決定最佳配置。 該函式庫的演算法可分析字型大小、影像品質和內容密度等文件特性,以選擇適當的預處理參數。
// Example: Processing a PDF with custom DPI settings
using IronOcr;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
// Configure DPI specifically for PDF processing
ocrInput.TargetDPI = 250; // Lower DPI often works well for PDFs
// Load a multi-page PDF document
ocrInput.LoadPdf(@"documents\scanned-report.pdf");
// Optional: Process only specific pages
// ocrInput.LoadPdf(@"documents\scanned-report.pdf", PageSelection: new int[] {1, 3, 5});
// Apply additional filters if needed
ocrInput.DeNoise(); // Remove digital noise
ocrInput.Sharpen(); // Enhance text edges
// Perform OCR with confidence tracking
var ocrResult = ocrTesseract.Read(ocrInput);
// Process results page by page
foreach (var page in ocrResult.Pages)
{
Console.WriteLine($"Page {page.PageNumber}: {page.Confidence}% confidence");
Console.WriteLine(page.Text);
}// Example: Processing a PDF with custom DPI settings
using IronOcr;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
// Configure DPI specifically for PDF processing
ocrInput.TargetDPI = 250; // Lower DPI often works well for PDFs
// Load a multi-page PDF document
ocrInput.LoadPdf(@"documents\scanned-report.pdf");
// Optional: Process only specific pages
// ocrInput.LoadPdf(@"documents\scanned-report.pdf", PageSelection: new int[] {1, 3, 5});
// Apply additional filters if needed
ocrInput.DeNoise(); // Remove digital noise
ocrInput.Sharpen(); // Enhance text edges
// Perform OCR with confidence tracking
var ocrResult = ocrTesseract.Read(ocrInput);
// Process results page by page
foreach (var page in ocrResult.Pages)
{
Console.WriteLine($"Page {page.PageNumber}: {page.Confidence}% confidence");
Console.WriteLine(page.Text);
}Imports IronOcr
' Example: Processing a PDF with custom DPI settings
Dim ocrTesseract As New IronTesseract()
Using ocrInput As New OcrInput()
' Configure DPI specifically for PDF processing
ocrInput.TargetDPI = 250 ' Lower DPI often works well for PDFs
' Load a multi-page PDF document
ocrInput.LoadPdf("documents\scanned-report.pdf")
' Optional: Process only specific pages
' ocrInput.LoadPdf("documents\scanned-report.pdf", PageSelection:=New Integer() {1, 3, 5})
' Apply additional filters if needed
ocrInput.DeNoise() ' Remove digital noise
ocrInput.Sharpen() ' Enhance text edges
' Perform OCR with confidence tracking
Dim ocrResult = ocrTesseract.Read(ocrInput)
' Process results page by page
For Each page In ocrResult.Pages
Console.WriteLine($"Page {page.PageNumber}: {page.Confidence}% confidence")
Console.WriteLine(page.Text)
Next
End UsingImports IronOcr
' Example: Processing a PDF with custom DPI settings
Dim ocrTesseract As New IronTesseract()
Using ocrInput As New OcrInput()
' Configure DPI specifically for PDF processing
ocrInput.TargetDPI = 250 ' Lower DPI often works well for PDFs
' Load a multi-page PDF document
ocrInput.LoadPdf("documents\scanned-report.pdf")
' Optional: Process only specific pages
' ocrInput.LoadPdf("documents\scanned-report.pdf", PageSelection:=New Integer() {1, 3, 5})
' Apply additional filters if needed
ocrInput.DeNoise() ' Remove digital noise
ocrInput.Sharpen() ' Enhance text edges
' Perform OCR with confidence tracking
Dim ocrResult = ocrTesseract.Read(ocrInput)
' Process results page by page
For Each page In ocrResult.Pages
Console.WriteLine($"Page {page.PageNumber}: {page.Confidence}% confidence")
Console.WriteLine(page.Text)
Next
End UsingTargetDPI的最大值為32,766。設定高於此限制的DPI將導致例外。 這是因為產生的影像尺寸將超過 Tesseract 支援的最大尺寸 32,767 x 32,767 像素。 如果超過限制,您將收到以下錯誤訊息:「TargetDPI過高,會導致圖像對於Tesseract而言過大({new_width} x {new_height})。 Tesseract的最大影像大小是32767 x 32767'對於處理大量文件的企業應用程式,可考慮實施動態 DPI 調整策略。 開始時請使用中等設定 (300 DPI),然後根據信心分數進行調整。 此方法可平衡處理速度與精準度,確保不同文件類型的最佳效能。將 DPI 最佳化與其他預處理技術結合,可以為具有挑戰性的文件帶來更好的結果。
在處理特定文件類型時,DPI 要求可能會有所不同。 例如,讀取車牌通常需要更高的DPI設置,因為字元較小,且攝影角度可能會導致影像失真。 同樣,處理MICR支票受益於特定的DPI配置,以準確擷取專業化的MICR字型字元。
對於將 OCR 整合到 Web 應用程式或雲端服務的開發人員而言,瞭解 DPI 最佳化對於管理處理時間和伺服器資源至關重要。 微調 DPI 設定的功能可讓您根據特定的應用程式需求和基礎架構限制,最佳化精確度與效能之間的平衡。
常見問題
什麼是DPI,以及它對OCR有什麼影響?
DPI,或每英寸點數,測量掃描文件或數位影像的細節。較高的DPI設置提供更多細節,這對準確的OCR至關重要。IronOCR使用DPI設置來改善文字識別,特別是針對低解析度的影像。
IronOCR如何處理低解析度影像?
IronOCR利用TargetDPI屬性來提升低解析度影像的品質,提高OCR準確性。其影像預處理功能會自動檢測並解決影像品質問題,以改善文字識別。
使用IronOCR進行OCR的最佳DPI設置是多少?
使用IronOCR進行OCR的最佳DPI設置通常範圍在250至400 DPI之間,具體取決於影像品質和使用情況。您可以調整IronOCR的TargetDPI屬性來獲得不同文件類型的最佳結果。
IronOCR可以處理具DPI優化的PDF文件嗎?
是的,IronOCR可以處理PDF文件並提升至TargetDPI設置,確保文件整體文字擷取品質的一致性。這種方法對於企業和商業應用中的可靠PDF OCR至關重要。
DPI設置如何影響OCR性能?
較高的DPI設置可能導致更好的OCR準確性,但也可能會降低處理速度。相反,較低的DPI設置可能更快但準確性較差。IronOCR會平衡DPI設置以優化性能和準確性。
如果將`TargetDPI`設置得太高會發生什麼?
將TargetDPI設置得太高可能會導致例外,因為它可能會創建超出Tesseract最大支援尺寸32,767 x 32,767像素的影像大小。IronOCR會在超出此限制時通知您。
IronOCR可以自動調整DPI設置嗎?
是的,IronOCR可以自動通過分析文件特徵如字體大小和影像品質來確定最佳的DPI設置,確保在無需手動輸入的情況下達到最佳設定。
如何使用IronOCR為影像設置DPI?
要使用IronOCR為影像設置DPI,請在OcrInput類中配置TargetDPI屬性。這有助於透過將影像解析度提升到期望的DPI來改善OCR準確性。
使用IronOCR進行DPI放大後,我可以預期獲得什麼結果?
透過IronOCR進行DPI升級後,您可以預期文字識別的改善和更高的信心得分。例如,OCR信心得分可以從79%提升到85%,顯示在困難影像上的準確性提高。
IronOCR如何在調整DPI的同時保持性能?
IronOCR使用複雜的演算法來平衡DPI設置和影像預處理任務,確保準確性和處理速度最佳化。這種能力對於高效管理大量文件至關重要。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。