Char 的边界框顶点顺序

Char's bounding box order of vertices

Google Vision API 文档指出检测到的字符的顶点将始终处于相同的顺序:

// The bounding box for the symbol.
// The vertices are in the order of top-left, top-right, bottom-right,
// bottom-left. When a rotation of the bounding box is detected the rotation
// is represented as around the top-left corner as defined when the text is
// read in the 'natural' orientation.
// For example:
//   * when the text is horizontal it might look like:
//      0----1
//      |    |
//      3----2
//   * when it's rotated 180 degrees around the top-left corner it becomes:
//      2----3
//      |    |
//      1----0
//   and the vertice order will still be (0, 1, 2, 3).

但是有时我会看到不同的顶点顺序。这是来自同一图像的两个字符的示例,它们具有相同的方向:

[x:778 y:316  x:793 y:316  x:793 y:323  x:778 y:323 ]
0----1
|    |
3----2

[x:857 y:295  x:857 y:287  x:874 y:287  x:874 y:295 ]
1----2
|    |
0----3

为什么顶点顺序不一样?而不是在文档中?

这似乎是 Vision 中的一个错误 API。 解决方案是检测图像方向,然后以正确的顺序重新排列顶点。

不幸的是,Vision API 没有在其输出中提供图像方向,所以我不得不编写代码来检测它。

Horizontal/vertical 可以通过比较字符的高度和宽度来检测方向。高度通常大于宽度。

下一步是检测文本的方向。例如,在垂直图像方向的情况下,文本可能从上到下或从下到上。

输出中的大多数字符似乎以自然方式出现。因此,通过查看统计数据,我们可以检测文本方向。例如: 第 1 行的 Y 坐标为 1000 第 2 行的 Y 坐标为 900 第 3 行的 Y 坐标为 950 第 4 行的 Y 坐标为 800 我们可以看到图像被颠倒了。

You must to reorder vertices of four poins(clockwise inverted from A to D):
A-B-C-D that:
A: min X, min Y
B: max X, min Y
C: max X, max Y
D: min X, max Y

And save to your rectangle object.

更新:对于上面的 A-B-C-D 顺序,您可以按距离 O(0,0) 的距离对顶点进行排序。