C# 指南:使用 IronOCR 影像濾鏡提升 OCR 辨識效果
IronOCR 提供讀取影像所需的工具,這些影像可能需要以濾鏡的形式進行預處理。 您可以從各種各樣的濾鏡中進行選擇,這些濾鏡可以處理您的影像,使其可以進行後續處理。
快速入門:應用濾鏡清理 OCR 影像
只需一連串簡單的呼叫,您就可以套用 DeNoise, Binarize, 和 Deskew 過濾器,在 OCR 之前改善掃描的清晰度。 本範例展示了使用 IronOCR 的內建濾鏡增強影像是多麼容易,並且可以立即上手。
立即開始使用 NuGet 建立 PDF 檔案:
使用 NuGet 套件管理器安裝 IronOCR
複製並運行這段程式碼。
using var input = new IronOcr.OcrInput("scan.jpg"); input.DeNoise(true).Binarize().Deskew(45); var result = new IronOcr.IronTesseract().Read(input);部署到您的生產環境進行測試
OCR影像濾鏡列表
以下影像濾鏡可顯著提升性能:
改變影像方向的濾鏡Rotate- 將影像順時針旋轉指定角度。逆時針旋轉請使用負數。Deskew- 將影像旋轉成正確的方向和正交角度。 這對於 OCR 非常有用,因為 Tesseract 對傾斜掃描的容忍度可以低至 5 度。Scale- 按比例縮放 OCR 輸入頁。Filters to manipulate Image ColorsBinarize- 此影像濾鏡將每個像素變為黑色或白色,沒有中間色。 這可以提高文字與背景對比度非常低時的 OCR 效能。ToGrayScale- 此影像濾鏡將每個像素轉換為灰階。 不太可能提高OCR識別準確率,但可能會提高速度。Invert- 反轉所有顏色。 例如,白色變成黑色,反之亦然。ReplaceColor- 將影像中的一種顏色替換為在一定閾值範圍內的另一種顏色。
改善影像對比的濾鏡Contrast- 自動增加對比。 此濾鏡通常可以提高低對比度掃描影像的 OCR 速度和準確性。Dilate- 高階形態學。 膨脹操作是指在影像中物件的邊界上新增像素。 與侵蝕相反。Erode- 高階形態學。 腐蝕會從物體邊界移除像素。 與擴張相反。
減少影像雜訊的濾鏡Sharpen- 銳利化模糊的 OCR 文檔,並將 alpha 通道展平為白色。DeNoise- 去除數位雜訊。此濾波器僅套用於預期會出現雜訊的場景。DeepCleanBackgroundNoise- 消除嚴重的背景噪音。 只有在已知文件背景噪音極大時才使用此過濾器,因為它可能會降低乾淨文件的 OCR 準確率,並且會佔用大量 CPU 資源。EnhanceResolution- 提高低品質影像的解析度。 這個過濾器並不經常需要,因為OcrInput.MinimumDPI和OcrInput.TargetDPI會自動擷取並解決低解析度輸入。
過濾器範例及用法
下面的範例將示範如何在程式碼中套用篩選器。
:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-1.csusing IronOcr;
using System;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("my_image.png");
input.Deskew();
var result = ocr.Read(input);
Console.WriteLine(result.Text);Imports IronOcr
Imports System
Private ocr = New IronTesseract()
Private input = New OcrInput()
input.LoadImage("my_image.png")
input.Deskew()
Dim result = ocr.Read(input)
Console.WriteLine(result.Text)調試過濾器/過濾器正在執行什麼操作?
如果您在程式中讀取影像或條碼時遇到困難,可以儲存篩選結果的影像。 這樣,你就可以進行調試,並準確地看到每個濾鏡的作用以及它是如何處理圖像的。
:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-2.csusing IronOcr;
using System;
var file = "skewed_image.tiff";
var ocr = new IronTesseract();
using var input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(file, pageindices);
// Here we apply the filter: Deskew
input.Deskew();
// Save the input with filter(s) applied
input.SaveAsImages("my_deskewed");
// We read, then print the text to the console
var result = ocr.Read(input);
Console.WriteLine(result.Text);Imports IronOcr
Imports System
Private file = "skewed_image.tiff"
Private ocr = New IronTesseract()
Private input = New OcrInput()
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames(file, pageindices)
' Here we apply the filter: Deskew
input.Deskew()
' Save the input with filter(s) applied
input.SaveAsImages("my_deskewed")
' We read, then print the text to the console
Dim result = ocr.Read(input)
Console.WriteLine(result.Text)過濾器使用案例
旋轉
篩選說明
旋轉濾鏡用於手動設定影像的已知旋轉角度,使其盡可能接近直線。 IronOCR 具有運行Deskew()功能,但是,其容差範圍相當窄,最適合用於幾乎完全筆直的影像(偏差在 15 度左右)。 對於偏離 90 度或上下顛倒的輸入影像,我們應該呼叫Rotate() 。
用例程式碼範例
這是呼叫 Rotate() 以修正倒置圖片的範例:
:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-3.csusing IronOcr;
using System;
var image = "screenshot.png";
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);
// Rotate 180 degrees because image is upside-down
input.Rotate(180);
// Read image into variable: result
var result = ocr.Read(input);
// Example print to console
Console.WriteLine(result.Text);Imports IronOcr
Imports System
Private image = "screenshot.png"
Private ocr = New IronTesseract()
Private input = New OcrInput()
' Load at least one image
input.LoadImage(image)
' Rotate 180 degrees because image is upside-down
input.Rotate(180)
' Read image into variable: result
Dim result = ocr.Read(input)
' Example print to console
Console.WriteLine(result.Text)在輸入Before旋轉(180)`` | 輸入After旋轉(180)`` |
|---|---|
IronOCR | IronOCR |
桌子
篩選說明
使用霍夫變換來嘗試在一定容差範圍內矯正影像。 對於不完全水平的影像來說,這一點很重要,因為傾斜的文件可能會導致誤讀。
[{i:(此方法傳回一個布林值,如果應用了過濾器,則傳回 true;如果由於無法偵測影像方向而未能套用,則傳回 false。 如果頁面沒有內容來定義方向,此操作將會失敗。
用例程式碼範例
這是呼叫 Deskew() 以修正歪斜影像的範例:
:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-4.csusing IronOcr;
using System;
var image = @"paragraph_skewed.png";
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);
// Apply deskew with 15 degree snap
bool didDeskew = input.Deskew(15);
if (didDeskew)
{
// Read image into variable: result
var result = ocr.Read(input);
Console.WriteLine(result.Text);
}
else
{
Console.WriteLine("Deskew not applied because Image Orientation could not be determined.");
}Imports IronOcr
Imports System
Private image = "paragraph_skewed.png"
Private ocr = New IronTesseract()
Private input = New OcrInput()
' Load at least one image
input.LoadImage(image)
' Apply deskew with 15 degree snap
Dim didDeskew As Boolean = input.Deskew(15)
If didDeskew Then
' Read image into variable: result
Dim result = ocr.Read(input)
Console.WriteLine(result.Text)
Else
Console.WriteLine("Deskew not applied because Image Orientation could not be determined.")
End If規模
篩選說明
縮放是一個有用的影像處理濾鏡,它可以利用影像已有的像素來調整影像大小。 當條碼無法掃描時(因為圖像只有幾十個像素寬,每個條形佔一個像素),或者文字太小而沒有抗鋸齒,可以使用這種方法。
對於1000px x 1000px的條碼尺寸來說,存在一個最佳尺寸範圍,在這個範圍內條碼可以很好地被讀取。如果您的條碼無法被找到,則應考慮此尺寸範圍。
用例程式碼範例
這是一個呼叫 Scale() 來放大條碼中的條與條之間的間隙以便掃描的範例:
:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-5.csusing IronOcr;
using System;
var image = @"small_barcode.png";
var ocr = new IronTesseract();
// Optional: This example uses a barcode
ocr.Configuration.ReadBarCodes = true;
using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);
// Apply scale
input.Scale(400); // 400% is 4 times larger
// Read image into variable: result
var result = ocr.Read(input);
// Example print to console
Console.WriteLine(result.Text);Imports IronOcr
Imports System
Private image = "small_barcode.png"
Private ocr = New IronTesseract()
' Optional: This example uses a barcode
ocr.Configuration.ReadBarCodes = True
Dim input = New OcrInput()
' Load at least one image
input.LoadImage(image)
' Apply scale
input.Scale(400) ' 400% is 4 times larger
' Read image into variable: result
Dim result = ocr.Read(input)
' Example print to console
Console.WriteLine(result.Text)二值化
篩選說明
二值化濾波器根據自適應演算法將影像中的所有像素分類為黑色或白色。 這會移除所有顏色,並將背景變成純白色,所有被識別為文字的部分都會變成全黑色,以便於閱讀。
用例程式碼範例
這是呼叫 Binarize() 以對齊彩色文字並移除背景顏色和雜訊的範例:
:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-6.csusing IronOcr;
using System;
var image = @"no-binarize.jpg";
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);
// Apply Binarize
input.Binarize();
// Read image into variable: result
var result = ocr.Read(input);
// Example print to console
Console.WriteLine(result.Text);Imports IronOcr
Imports System
Private image = "no-binarize.jpg"
Private ocr = New IronTesseract()
Private input = New OcrInput()
' Load at least one image
input.LoadImage(image)
' Apply Binarize
input.Binarize()
' Read image into variable: result
Dim result = ocr.Read(input)
' Example print to console
Console.WriteLine(result.Text)在二值化之前()`` | 二進位化之後()`` |
|---|---|
IronOCR | IronOCR |
倒置
篩選說明
IronOCR 辨識black text on a white background影像效果最佳。 Invert 篩選器是透過反轉影像上的所有顏色來達到此目的。
用例程式碼範例
這是呼叫 Invert() 將黑底白字轉為白底黑字的範例:
:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-7.csusing IronOcr;
using System;
var image = @"before-invert.png";
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);
// Apply Invert
input.Invert(true);
// Read image into variable: result
var result = ocr.Read(input);
// Example print to console
Console.WriteLine(result.Text);Imports IronOcr
Imports System
Private image = "before-invert.png"
Private ocr = New IronTesseract()
Private input = New OcrInput()
' Load at least one image
input.LoadImage(image)
' Apply Invert
input.Invert(True)
' Read image into variable: result
Dim result = ocr.Read(input)
' Example print to console
Console.WriteLine(result.Text)之前 | 之後 |
|---|---|
IronOCR | IronOCR |
常見問題解答
圖像過濾器如何在 C# 中提高 OCR 準確性?
IronOCR 中的圖像過濾器可以對圖像進行預處理來提升其質量,從而提高 OCR 準確性。二值化和對比度等過濾器通過調整顏色和對比度提高可讀性,而旋轉和去斜則校正圖像方向。
更正圖像方向有什麼過濾器可用?
IronOCR 提供旋轉和去斜過濾器來糾正圖像方向問題。旋轉允許手動調整圖像角度,而去斜則自動校正輕微傾斜的圖像。
二值化過濾器如何影響圖像預處理?
IronOCR 中的二值化過濾器將圖像像素轉換為黑白,從而去除背景顏色並增強文本可見性,特別是在低對比度條件下提高 OCR 準確性。
什麼時候適合使用噪音降低過濾器?
當圖像中存在數字噪音時,應使用如銳化和去噪等噪音降低過濾器。這些過濾器清理圖像,使文本更清晰,以便在 IronOCR 中獲得更好的 OCR 結果。
增強圖像解析度會影響 OCR 性能嗎?
是的,使用增強解析度過濾器可以通過增加低質量圖像的解析度來提高 OCR 性能。儘管 IronOCR 的默認 MinimumDPI 和 TargetDPI 設置通常已經足夠,但如有需要,該過濾器可以提供額外的解析度增強。
顏色操作過濾器在 OCR 中扮演什麼角色?
IronOCR 中的顏色操作過濾器如反轉、灰度化和二值化調整圖像顏色以提升文本可讀性。反轉更改色彩方案,灰度化將圖像轉化為灰階,而二值化將圖像簡化為黑白。
對比度和銳化過濾器有什麼區別?
IronOCR 中的對比度過濾器增強亮暗區域間的差異,提高文本清晰度,而銳化過濾器增強邊緣,使文本更鮮明,兩者皆助於更好的OCR識別。
如何在 IronOCR 中保存和調試過濾后的圖像?
要保存和調試 IronOCR 中的過濾圖像,請在應用過濾器後使用 SaveAsImages 函數。這有助於可視化過濾器效果,確保預處理步驟提高了圖像品質以進行 OCR。
IronOCR 中有哪些高階形態學過濾器?
IronOCR 提供高階形態學過濾器如膨脹和腐蝕。膨脹在物體邊界增加像素以增強特徵,而腐蝕則去除像素,兩者皆用於明確圖像細節以提高 OCR 準確性。
IronOCR
IronOCR
IronOCR
IronOCR
IronOCR
IronOCR






