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向下增長。

在您開始之前,您需要Microsoft Paint(或任何可以顯示光標位置和選擇大小的像素的編輯器),要讀取的圖像,以及將IronOCR加入您的專案中。
解決方案
1. 閱讀X和Y起始點
在Microsoft Paint中打開圖像,然後將游標移動到您要讀取的區域的左上角。 游標位置顯示在視窗的左下角。 2403, 1653px的讀出值給您區域的起始點:

X = 2403
Y = 1653
2. 閱讀寬度和高度
切換到選擇工具,並在目標文字上拖動框。 Paint在左下角報告選擇的大小。 1031 × 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,作為矩形大小。這會選擇整個圖像而不是您的區域。
準備開始了嗎?
Nuget 下載 6,136,090 | 版本: 2026.7 剛剛發布

