使用 IronOCR 篩選器指南

查克尼思·賓
查克尼思·賓
2022年6月9日
已更新 2024年12月10日
分享:
This article was translated from English: Does it need improvement?
Translated
View the article in English

IronOCR 擁有您在預處理形式的過濾器中可能需要用來讀取圖像的工具。 您可以從多種過濾器中選擇,這些過濾器可以操作您的圖像,使其可被處理。

開始使用IronOCR

立即在您的專案中使用IronOCR,並享受免費試用。

第一步:
green arrow pointer


OCR 圖像篩選器列表

以下圖像過濾器可以顯著提升性能:

  • 用於改變圖像方向的濾鏡

    • 旋轉 - 將圖片按順時針方向旋轉指定的度數。如需逆時針旋轉,請使用負數。

    • Deskew - 旋轉圖像,使其正確朝上且成直角。 這對於光學字符識別非常有用,因為Tesseract對斜掃描的容忍度可以低至5度。
  • 縮放 - 按比例縮放OCR輸入頁面。
  • 用於操作影像顏色的濾鏡

    • 二值化 - 此影像濾鏡將每個像素轉變為黑色或白色,無任何中間色。 可改善文字與背景對比非常低的情況下的OCR性能。

    • ToGrayScale - 這個影像濾鏡將每個像素轉變為灰階色調。 不太可能提高OCR精度,但可能提高速度。

    • 反轉 - 翻轉每一種顏色。 例如:白變成黑:黑變成白。
  • ReplaceColor - 以某個閾值將圖像中的一種顏色替換為另一種顏色。
  • 嘗試改善影像對比度的濾鏡

    • 對比度 - 自動增加對比度。 此過濾器通常可以提高低對比度掃描中的OCR速度和準確性。

    • 擴張 - 高級形態學。 擴張會在影像中的物體邊界增加像素。 侵蝕的對立面
  • Erode - 高級形態學。 腐蝕會移除物件邊界上的像素。 縮小
  • 嘗試減少影像噪音的濾鏡

    • Sharpen - 銳化模糊的OCR文檔並將透明通道展平為白色。

    • DeNoise - 去除數位噪音。此濾鏡應僅在預期有噪音的地方使用。

    • DeepCleanBackgroundNoise - 去除嚴重背景噪音。 僅在已知文件背景噪音極端的情況下使用此過濾器,因為這種過濾器也可能降低清晰文件的OCR準確性,且非常耗費CPU資源。

    • EnhanceResolution - 提升低品質圖像的解析度。 此篩選器通常不是必須的,因為_OcrInput.MinimumDPI_和_OcrInput.TargetDPI_會自動捕捉並解決低解析度的輸入。

篩選範例與使用方法

在以下示例中,我們展示了如何在您的代碼中應用過濾器。

:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-1.cs
using 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)
$vbLabelText   $csharpLabel

調試過濾器 / 過濾器在做什麼?

如果您在程式中遇到讀取圖像或條碼的困難。 您的程序中有一種方法可以保存篩選結果的圖像。 這樣您可以進行除錯,並且確切地看到每個濾鏡的作用以及它是如何操作您的影像。

:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-2.cs
using 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)
$vbLabelText   $csharpLabel

篩選使用案例

旋轉

API 參考

過濾器說明

旋轉是一個過濾器,用於手動設置圖像的已知旋轉角度,使其盡可能接近直立。 IronOCR 具有執行 Deskew() 的功能,但其容忍度相對較小,最適合用於幾乎完美平直(約在 15 度內) 的圖像。 對於偏差90度或倒置的輸入圖像,我們應該調用Rotate()

使用案例代碼示例

這是呼叫 Rotate 以修正倒置圖像的一個例子:

:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-3.cs
using 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)
$vbLabelText   $csharpLabel

Input.Rotate(180) 之前 Input.Rotate(180) 之後


Screenshot related to 使用案例代碼示例 Screenshot Rotated related to 使用案例代碼示例

校正歪斜

API 參考

過濾器說明

使用霍夫變換試圖在一定的容忍度內使圖像變直。 這對於圖像不完全直的情況非常重要,因為有時候一個傾斜的文件可能會導致讀取錯誤。

注意:此方法返回一個布林值,如果濾鏡被應用則為真,如果由於無法檢測到圖片方向而應用失敗則為假。 如果頁面沒有內容來確定方向,這將會失敗。

使用案例代碼示例

這是一個調用Deskew來糾正傾斜圖像的例子:

:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-4.cs
using 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
$vbLabelText   $csharpLabel

Deskew() 之前 Deskew() 之後


Paragraph Skewed related to 使用案例代碼示例 Paragraph Deskewed related to 使用案例代碼示例

規模

API 參考

過濾器說明

縮放是一種實用的圖像操作過濾器,它可以幫助您使用現有的像素來調整圖像的大小。 當條碼因為圖像僅寬幾十個像素,每個條紋寬一個像素,或者文字太小且沒有抗鋸齒時,可以使用此方法。

注意:有一個條形碼尺寸的最佳範圍是1000px x 1000px,這樣的條形碼可以被很好地讀取,若您的條形碼未被找到,應考慮這一點。

使用案例代碼示例

這是一個調用Scale來放大條形碼中條間距以便掃描的例子:

:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-5.cs
using 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)
$vbLabelText   $csharpLabel

Scale() 之前 Scale() 之後


C Sharp Ocr Image Filters 1 related to 使用案例代碼示例 C Sharp Ocr Image Filters 2 related to 使用案例代碼示例

二值化

API 參考資料

過濾器說明

二值化過濾器會根據一個自適應算法將圖像中的所有像素分類為黑色或白色,該算法評估圖像並將其認定為背景的顏色。 當您有一張包含許多顏色但文字可以突出的圖像時,二值化過濾器將移除所有顏色,並將背景分割為平坦的白色,任何被識別為文字的部分將被塗成全黑色,以便於閱讀。

使用案例代碼示例

這是一個調用 Binarize 來對齊彩色文本、去除背景顏色和噪音的示例:

:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-6.cs
using 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)
$vbLabelText   $csharpLabel

Binarize() 之前 Binarize() 之後


No Binarize related to 使用案例代碼示例 After Binarize related to 使用案例代碼示例

反轉

API 參考

過濾器說明

當圖像文本是白色背景的黑色文字時,IronOCR 的識別效果最佳。 在執行一系列過濾器時,重要的是在讀取之前嘗試達成這個結果。 Invert 是一種簡單的濾鏡,可以反轉圖像上的所有顏色。 白變成黑,黑變成白,中間的一切都翻轉了。

使用案例代碼示例

這是一個調用 Invert 將黑白顛倒的例子:

:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-7.cs
using 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)
$vbLabelText   $csharpLabel

之前 之後


Before Invert related to 使用案例代碼示例 After Invert related to 使用案例代碼示例

查克尼思·賓
軟體工程師
Chaknith 致力於 IronXL 和 IronBarcode。他在 C# 和 .NET 方面擁有豐富的專業知識,協助改進軟體並支持客戶。他從用戶互動中獲得的洞察力有助於提高產品、文檔和整體體驗。