如何使用電腦視覺尋找文本

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

介紹

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

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

本教程後面包括了代碼示例。 以下是目前可用方法的一般概述:
<table class="table table__configuration-variables">
    <tr>
        <th scope="col">Method</th>
        <th scope="col">Explanation</th>
    </tr>
    <tr>
        <td><a href="#anchor-findtextregion">FindTextRegion</a></td>
        <td class="word-break--break-word">Detect regions which contain text elements and instruct Tesseract to only search for text within the area in which text was detected.</td>
    </tr>
     <tr>
        <td><a href="#anchor-findmultipletextregions">FindMultipleTextRegions</a></td>
        <td class="word-break--break-word">Detect areas which contain text elements and divide the page into separate images based on text regions.</td>
    </tr>
    <tr>
        <td><a href="#anchor-gettextregions">GetTextRegions</a></td>
        <td class="word-break--break-word">Scans the image and returns a list of text regions as `List<CropRectangle>`.</td>
    </tr>
</table>

## 尋找文本區域

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

```cs
:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findtextregion-1.cs

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

:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findtextregion-2.cs
using IronOcr;

var ocr = new IronTesseract();
using var input = new OcrInput();
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()
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

在此示例中,我將使用以下圖像編寫一個方法,該方法需要裁剪包含文本的區域,但輸入圖像中的文本位置可能會有所不同。 在這種情況下,我可以使用FindTextRegion來縮小掃描範圍,僅針對計算機視覺偵測到的文字區域。 這是一個範例圖片:

帶有文字的圖片
:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findtextregion-3.cs
using IronOcr;
using IronSoftware.Drawing;
using System;
using System.Linq;

var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("wh-words-sign.jpg");

// Find the text region using Computer Vision
Rectangle textCropArea = input.GetPages().First().FindTextRegion();

// For debugging and demonstration purposes, lets see what region it found:
input.StampCropRectangleAndSaveAs(textCropArea, Color.Red, "image_text_area", AnyBitmap.ImageFormat.Png);

// Looks good, so let us apply this region to hasten the read:
var ocrResult = ocr.Read("wh-words-sign.jpg", textCropArea);
Console.WriteLine(ocrResult.Text);
Imports IronOcr
Imports IronSoftware.Drawing
Imports System
Imports System.Linq

Private ocr = New IronTesseract()
Private input = New OcrInput()
input.LoadImage("wh-words-sign.jpg")

' Find the text region using Computer Vision
Dim textCropArea As Rectangle = input.GetPages().First().FindTextRegion()

' For debugging and demonstration purposes, lets see what region it found:
input.StampCropRectangleAndSaveAs(textCropArea, Color.Red, "image_text_area", AnyBitmap.ImageFormat.Png)

' Looks good, so let us apply this region to hasten the read:
Dim ocrResult = ocr.Read("wh-words-sign.jpg", textCropArea)
Console.WriteLine(ocrResult.Text)
$vbLabelText   $csharpLabel

現在此代碼有兩個輸出,第一個是由StampCropRectangleAndSaveAs保存的.png文件,用於調試。 我們可以看到 IronCV(計算機視覺)認為文字的位置:

突出顯示文本區域的圖像

看起來相當不錯。 現在第二個輸出是文字本身,即:

IRONSOFTWARE

50,000+

Developers in our active community

10,777,061 19,313
NuGet downloads Support tickets resolved
50%+ 80%+
Engineering Team growth Support Team growth
$25,000+

Raised with #TEAMSEAS to clean our beaches & waterways

查找多個文本區域

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

:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findmultipletextregions-1.cs
using IronOcr;

var ocr = new IronTesseract();
using var input = new OcrInput();
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()
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/how-to/computer-vision-findmultipletextregions-2.cs
using IronOcr;

var ocr = new IronTesseract();
using var input = new OcrInput();
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()
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/how-to/computer-vision-findmultipletextregions-3.cs
using IronOcr;
using System.Collections.Generic;
using System.Linq;

int pageIndex = 0;
using var input = new OcrInput();
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()
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/how-to/computer-vision-gettextregions.cs
using IronOcr;
using IronSoftware.Drawing;
using System.Collections.Generic;
using System.Linq;

int pageIndex = 0;
using var input = new OcrInput();
input.LoadImage("/path/file.png");

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

Private pageIndex As Integer = 0
Private input = New OcrInput()
input.LoadImage("/path/file.png")

Dim selectedPage = input.GetPages().ElementAt(pageIndex)
' List<Rectangle> regions = selectedPage.GetTextRegions();
$vbLabelText   $csharpLabel

特定使用案例指南

透過正確的設定和輸入檔案,OCR 可以是一個非常強大的工具。 它幾乎可以完美地模仿人類的閱讀能力。

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