顶点的夏亚边界框顺序



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 在其输出中不提供图像方向,所以我不得不编写代码来检测它。

水平/垂直方向可以通过比较字符高度和宽度来检测。高度通常大于宽度。

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

输出中的大多数字符似乎都以自然的方式出现。因此,通过查看统计数据,我们可以检测文本方向。所以例如:第 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( 对顶点进行排序。

最新更新