OCR區域檢測的矩形座標

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

IronOCR的幾個方法接受Rectangle,因此您可以在圖片的一部分而不是整個圖片上運行OCR。 它需要的四個值,height,可以直接從Microsoft Paint中讀取。

構造方法如下:

Rectangle(int x, int y, int width, int height, MeasurementUnits units = MeasurementUnits.Pixels)
Rectangle(int x, int y, int width, int height, MeasurementUnits units = MeasurementUnits.Pixels)
Rectangle(x As Integer, y As Integer, width As Integer, height As Integer, Optional units As MeasurementUnits = MeasurementUnits.Pixels)
$vbLabelText   $csharpLabel

每個參數映射到一個測量值:

  • x從圖像左邊緣到區域起始位置的距離。
  • y從圖像頂部邊緣到區域起始位置的距離。
  • width您要讀取的區域的寬度。
  • height您要讀取的區域的高度。
  • MeasurementUnits.Pixels告訴IronOCR這些值是以像素為單位。

座標從圖像的左上角開始:y向下增長。

座標系統顯示起點在左上角,X向右增長,Y向下增長

在您開始之前,您需要Microsoft Paint(或任何可以顯示光標位置和選擇大小的像素的編輯器),要讀取的圖像,以及將IronOCR加入您的專案中。

解決方案

1. 閱讀X和Y起始點

在Microsoft Paint中打開圖像,然後將游標移動到您要讀取的區域的左上角。 游標位置顯示在視窗的左下角。 2403, 1653px的讀出值給您區域的起始點:

Microsoft Paint狀態欄顯示光標座標2403、1653px在OCR區域的左上角

X = 2403
Y = 1653

2. 閱讀寬度和高度

切換到選擇工具,並在目標文字上拖動框。 Paint在左下角報告選擇的大小。 1031 × 214px的讀出值是您區域的尺寸:

Microsoft Paint在Testing123文字周圍的選擇,狀態欄顯示選擇大小1031 x 214px

Width = 1031
Height = 214

3. 建立矩形

手持四個值,構建Rectangle。 對於上述的Testing123區域:

var rectangle = new Rectangle(2403, 1653, 1031, 214, MeasurementUnits.Pixels);
var rectangle = new Rectangle(2403, 1653, 1031, 214, MeasurementUnits.Pixels);
Dim rectangle = New Rectangle(2403, 1653, 1031, 214, MeasurementUnits.Pixels)
$vbLabelText   $csharpLabel

4. 傳遞給IronOCR

將矩形交給LoadImage以將輸入裁剪到該區域,再運行OCR:

var rectangle = new Rectangle(2403, 1653, 1031, 214, MeasurementUnits.Pixels);
ocrInput.LoadImage(frame, rectangle);
var rectangle = new Rectangle(2403, 1653, 1031, 214, MeasurementUnits.Pixels);
ocrInput.LoadImage(frame, rectangle);
Dim rectangle = New Rectangle(2403, 1653, 1031, 214, MeasurementUnits.Pixels)
ocrInput.LoadImage(frame, rectangle)
$vbLabelText   $csharpLabel

除錯提示

  • 狀態欄中的1031 × 214px值是選擇的寬度和高度,而不是座標。
  • 2403, 1653px光標讀出是X和Y起始點,意味著區域的左上角。
  • Paint報告的是像素,因此傳遞MeasurementUnits.Pixels以匹配。
  • 透過肉眼閱讀的值,可能會使它們偏離幾個像素。 如果裁剪部分的文字,稍微擴大y

小心請勿使用完整圖像尺寸,如4960 × 7014px,作為矩形大小。這會選擇整個圖像而不是您的區域。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備開始了嗎?
Nuget 下載 6,136,090 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在滾動?

想要快速證明? PM > Install-Package IronOcr
執行範例 觀看您的圖像轉變為可搜尋文字。