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这些值以像素为单位。

坐标从图像的左上角开始:x 向右增长,y 向下增长。

坐标系统显示原点在左上角,X轴向右增长,Y轴向下增长

在开始之前,您需要Microsoft Paint(或任何显示光标位置和选择区域大小的像素编辑器)、您想要读取的图像以及添加到项目中的IronOCR。

解决方案

1. 读取X和Y起点

在Microsoft Paint中打开图像,然后将光标移动到您希望读取区域的左上角。 光标位置显示在窗口的左下角。 读取2403, 1653px 可为您提供区域的起始点:

Microsoft Paint状态栏显示在OCR区域左上角光标坐标为2403,1653px

X = 2403
Y = 1653

2. 读取宽度和高度

切换至选择工具并在目标文本上拖动一个框。 Paint在左下角报告选择的大小。 读取1031 × 214px 是您区域的尺寸:

Microsoft Paint对Testing123文本的选择,状态栏显示选择大小为1031 x 214像素

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 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 6,136,090 | 版本: 2026.7 刚刚发布
Still Scrolling Icon

还在滚动吗?

想快速获得证据? PM > Install-Package IronOcr
运行示例 观看您的图像变成可搜索文本。