使用 IronOCR 電腦視覺指南

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

介紹

IronOCR 使用 OpenCV 利用计算机视觉来检测图像中存在文本的区域。 這對於包含大量噪音的圖像、文字分佈在許多不同位置的圖像,以及文字扭曲的圖像非常有用。 IronOCR 中使用計算機視覺將確定文本區域的位置,然後使用 Tesseract 嘗試讀取這些區域。

開始使用IronOCR

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

第一步:
green arrow pointer


IronOCR.ComputerVision 透過 NuGet 套件安裝

在IronOCR中執行電腦視覺的OpenCV方法可在常規的IronOCR NuGet套件中看到。

這些方法的使用需要將 IronOcr.ComputerVision 的 NuGet 安裝到解決方案中,如果尚未安裝,系統會提示您下載。

  • Windows: IronOcr.ComputerVision.Windows
  • Linux: IronOcr.ComputerVision.Linux
  • macOS:IronOcr.ComputerVision.MacOS
  • macOS ARM: IronOcr.ComputerVision.MacOS.ARM

    使用 NuGet 包管理器安裝或在包管理器控制台中粘貼以下內容:

PM> Install-Package IronOcr.ComputerVision.Windows

這將提供使用我們模型文件所需的組件以使用IronOCR電腦視覺。

功能和 API

本教程後面包括了代碼示例。 以下是目前可用方法的一般概述:

MethodExplanation
FindTextRegionDetect regions which contain text elements and instruct Tesseract to only search for text within the area in which text was detected.
FindMultipleTextRegionsDetect areas which contain text elements and divide the page into separate images based on text regions.
GetTextRegionsScans the image and returns a list of text regions as `List`.

程式碼範例

尋找文本區域

使用FindTextRegion將利用計算機視覺檢測OcrInput物件每個頁面上的文字元素區域。

:path=/static-assets/ocr/content-code-examples/tutorials/csharp-recognize-text-from-image-computer-vision-1.cs
using IronOcr;

var ocr = new IronTesseract();
using var input = new OcrInput();
// Load at least one image
input.LoadImage("/path/file.png");

input.FindTextRegion();
OcrResult result = ocr.Read(input);
string resultText = result.Text;
Imports IronOcr

Private ocr = New IronTesseract()
Private input = New OcrInput()
' Load at least one image
input.LoadImage("/path/file.png")

input.FindTextRegion()
Dim result As OcrResult = ocr.Read(input)
Dim resultText As String = result.Text
$vbLabelText   $csharpLabel

可以選擇使用自訂參數調用:

:path=/static-assets/ocr/content-code-examples/tutorials/csharp-recognize-text-from-image-computer-vision-2.cs
using IronOcr;

var ocr = new IronTesseract();
using var input = new OcrInput();
// Load at least one image
input.LoadImage("/path/file.png");

input.FindTextRegion(Scale: 2.0, DilationAmount: 20, Binarize: true, Invert: true);
OcrResult result = ocr.Read(input);
string resultText = result.Text;
Imports IronOcr

Private ocr = New IronTesseract()
Private input = New OcrInput()
' Load at least one image
input.LoadImage("/path/file.png")

input.FindTextRegion(Scale:= 2.0, DilationAmount:= 20, Binarize:= True, Invert:= True)
Dim result As OcrResult = ocr.Read(input)
Dim resultText As String = result.Text
$vbLabelText   $csharpLabel

重載也可用於將文本區域作為矩形返回:

:path=/static-assets/ocr/content-code-examples/tutorials/csharp-recognize-text-from-image-computer-vision-3.cs
using IronOcr;

using var input = new OcrInput();
// Load at least one image
input.LoadImage("/path/file.png");

input.FindTextRegion(Scale: 2.0, Binarize: true);
Imports IronOcr

Private input = New OcrInput()
' Load at least one image
input.LoadImage("/path/file.png")

input.FindTextRegion(Scale:= 2.0, Binarize:= True)
$vbLabelText   $csharpLabel

查找多個文本區域

使用 FindMultipleTextRegions 會處理 OcrInput 物件的所有頁面,並利用電腦視覺技術來檢測包含文字元素的區域,然後根據文本區域將輸入分成多個單獨的圖像:

:path=/static-assets/ocr/content-code-examples/tutorials/csharp-recognize-text-from-image-computer-vision-4.cs
using IronOcr;

var ocr = new IronTesseract();
using var input = new OcrInput();
// Load at least one image
input.LoadImage("/path/file.png");

input.FindMultipleTextRegions();
OcrResult result = ocr.Read(input);
string resultText = result.Text;
Imports IronOcr

Private ocr = New IronTesseract()
Private input = New OcrInput()
' Load at least one image
input.LoadImage("/path/file.png")

input.FindMultipleTextRegions()
Dim result As OcrResult = ocr.Read(input)
Dim resultText As String = result.Text
$vbLabelText   $csharpLabel

可以選擇使用自訂參數調用:

:path=/static-assets/ocr/content-code-examples/tutorials/csharp-recognize-text-from-image-computer-vision-5.cs
using IronOcr;

var ocr = new IronTesseract();
using var input = new OcrInput();
// Load at least one image
input.LoadImage("/path/file.png");

input.FindMultipleTextRegions(Scale: 2.0, DilationAmount: -1, Binarize: true, Invert: false);
OcrResult result = ocr.Read(input);
string resultText = result.Text;
Imports IronOcr

Private ocr = New IronTesseract()
Private input = New OcrInput()
' Load at least one image
input.LoadImage("/path/file.png")

input.FindMultipleTextRegions(Scale:= 2.0, DilationAmount:= -1, Binarize:= True, Invert:= False)
Dim result As OcrResult = ocr.Read(input)
Dim resultText As String = result.Text
$vbLabelText   $csharpLabel

另一個 FindMultipleTextRegions 的重載方法會接收一個 OCR 頁面並返回一個 OCR 頁面列表,每個文字區域對應一個 OCR 頁面:

:path=/static-assets/ocr/content-code-examples/tutorials/csharp-recognize-text-from-image-computer-vision-6.cs
using IronOcr;
using System.Collections.Generic;
using System.Linq;

int pageIndex = 0;
using var input = new OcrInput();
// Load at least one image
input.LoadImage("/path/file.png");

var selectedPage = input.GetPages().ElementAt(pageIndex);
List<OcrInputPage> textRegionsOnPage = selectedPage.FindMultipleTextRegions();
Imports IronOcr
Imports System.Collections.Generic
Imports System.Linq

Private pageIndex As Integer = 0
Private input = New OcrInput()
' Load at least one image
input.LoadImage("/path/file.png")

Dim selectedPage = input.GetPages().ElementAt(pageIndex)
Dim textRegionsOnPage As List(Of OcrInputPage) = selectedPage.FindMultipleTextRegions()
$vbLabelText   $csharpLabel

獲取文本區域

使用GetTextRegions返回在頁面中檢測到文本的裁剪區域列表:

:path=/static-assets/ocr/content-code-examples/tutorials/csharp-recognize-text-from-image-computer-vision-7.cs
using IronOcr;
using IronSoftware.Drawing;
using System.Collections.Generic;
using System.Linq;


int pageIndex = 0;
using var input = new OcrInput();
// Load at least one image
input.LoadImage("/path/file.png");

var selectedPage = input.GetPages().ElementAt(pageIndex);
var regions = selectedPage.GetTextRegions();
Imports IronOcr
Imports IronSoftware.Drawing
Imports System.Collections.Generic
Imports System.Linq


Private pageIndex As Integer = 0
Private input = New OcrInput()
' Load at least one image
input.LoadImage("/path/file.png")

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