使用IronOCR過濾器指南

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronOCR提供了一些工具,您可以使用這些工具來讀取可能需要以過濾器形式進行預處理的圖像。您可以從多種過濾器中選擇,這些過濾器可以操控圖像,使其可處理。

C# NuGet 程式庫用于 OCR

安裝與 NuGet

Install-Package IronOcr
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

C# NuGet 程式庫用于 OCR

安裝與 NuGet

Install-Package IronOcr
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

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

第一步:
green arrow pointer

查看 IronOCRNuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變OCR。

C# NuGet 程式庫用于 OCR nuget.org/packages/IronOcr/
Install-Package IronOcr

請考慮安裝 IronOCR DLL 直接下載並手動安裝到您的專案或GAC表單: IronOcr.zip

手動安裝到您的項目中

下載DLL

OCR 圖片篩選器列表

以下的圖片篩選器可以顯著提高性能:

  • 更改圖片方向的篩選器

    • 旋轉 - 將圖片順時針旋轉指定的度數。若需逆時針旋轉,使用負數。

    • 傾斜校正 - 旋轉圖片,使其為正確的方向和垂直。對於 OCR 來說,這非常有用,因為 Tesseract 對傾斜掃描的容忍度可能僅為 5 度。
  • 縮放 - 按比例縮放 OCR 輸入頁面。
  • 操縱圖片顏色的篩選器

    • 二值化 - 將每個像素轉變成黑色或白色,無中間值。可以在文字與背景對比度非常低的情況下提高 OCR 效果。

    • 灰階 - 將每個像素變為灰階。雖不太可能提高 OCR 的準確度,但可能提高速度。

    • 顛倒顏色 - 顛倒所有顏色。例如,白色變黑色,黑色變白色。
  • 顏色替換 - 以一定閾值替換圖片中的一種顏色為另一種顏色。
  • 嘗試提高圖片對比度的篩選器

    • 對比度 - 自動增加對比度。此篩選器通常在低對比度掃描中提高 OCR 的速度和準確度。

    • 膨脹 - 高級形態學處理。膨脹 在圖片中對象的邊界添加像素。與侵蝕相反。
  • 侵蝕 - 高級形態學處理。侵蝕 移除對象邊界上的像素。與膨脹相反。
  • 嘗試減少圖片噪聲的篩選器

    • 銳化 - 銳化模糊的 OCR 文件,並將透明通道轉換為白色。

    • 降噪 - 移除數字噪聲。僅在預期有噪聲時使用此篩選器。

    • 深度去除背景噪聲 - 強效背景噪聲移除。僅在已知文件背景噪聲極大時使用此篩選器,因為此篩選器也會降低乾淨文件的 OCR 準確性,且非常耗費 CPU。

    • 增強解析度 - 增強低質量圖片的解析度。通常不需要此篩選器,因為 OcrInput.MinimumDPIOcrInput.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)
VB   C#

除錯過濾器 / 過濾器在做什麼?

如果你在程式中讀取圖像或條碼時遇到困難,有一種方法可以在程式內保存過濾結果的圖像。這樣你就可以進行除錯,確切地查看每個過濾器的作用以及它是如何處理你的圖像的。

: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)
VB   C#

篩選使用案例

旋轉

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)
VB   C#

之前 `Input.Rotate(180) After Input.Rotate(180)

--

校正歪斜

API 參考文獻

篩選器說明

使用霍夫變換嘗試在一定度數的容差內將圖像變直。這對於不完全平直的圖像非常重要,因為有時傾斜的文件可能會導致誤讀。

注意:此方法返回布林值,如果篩選器應用成功則返回 true,如果由於無法檢測圖像方向而應用失敗則返回 false。如果頁面沒有任何內容來確定方向,這將會失敗。

用例代碼範例

這是一個使用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
VB   C#

Deskew 之前() After Deskew()

--

規模

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)
VB   C#

未縮放前() Scale()

--

二值化

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)
VB   C#

Binarize 之前()``` Binarize ``()

--

反轉

API 參考文獻

過濾器說明

IronOCR 在閱讀時,圖片的閱讀效果最好是黑色文字在白色背景上。在應用一系列過濾器時,重要的是在閱讀前儘量達到這個效果。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)
VB   C#

之前 之後

--