OCR区域检测的矩形坐标
一些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)
每个参数对应于一个测量值:
x: 从图像左边缘到您区域起始的距离。y: 从图像顶部边缘到您区域起始的距离。width: 您想要读取的区域宽度。height: 您想要读取的区域高度。MeasurementUnits.Pixels: 告诉IronOCR这些值以像素为单位。
坐标从图像的左上角开始:x 向右增长,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)
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)
调试提示
- 状态栏中的
1031 × 214px值是选择的宽度和高度,而不是坐标。 2403, 1653px光标读取是X和Y起点,即区域的左上角。- Paint 以像素显示,因此传递
MeasurementUnits.Pixels以匹配。 - 用眼睛读取值会使它们偏差几像素。 如果裁剪剪掉了部分文字,稍微加宽
y。

