Skip to content

Geometry Utils

NumPy polygon geometry helpers for rotated text regions.

expand_quad(quad, ratio)

Grow an ordered rectangle outward by area * ratio / perimeter on every side.

For a rectangle this is exactly the minimum-area rectangle of PaddleOCR's Vatti-clipping unclip result: a w x h rectangle becomes (w + 2D) x (h + 2D).

Parameters:

Name Type Description Default
quad ndarray

(4, 2) rectangle ordered [top_left, top_right, bottom_right, bottom_left].

required
ratio float

Non-negative expansion ratio. 0 leaves the quad unchanged.

required

Returns:

Type Description
ndarray

np.ndarray: (4, 2) expanded rectangle, in the same point order as quad.

min_area_rect(points)

Compute the minimum-area rotated rectangle enclosing a point set.

One rectangle is aligned to each convex-hull edge of SciPy's Qhull hull and the smallest one is kept. Point sets without area, such as a single point or collinear points, get their zero-area axis-aligned bounding box.

Parameters:

Name Type Description Default
points ndarray

(N, 2) array of (x, y) points, N >= 1.

required

Returns:

Type Description
ndarray

np.ndarray: (4, 2) rectangle corners, unordered but consistently wound.

Raises:

Type Description
ImportError

If SciPy, provided by the ocr extra, is not installed.

order_quad_clockwise(quad)

Order four points as top-left, top-right, bottom-right, bottom-left.

Matches PaddleOCR's order_points_clockwise [1]: points are split by x-coordinate into a left pair and a right pair, then each pair is ordered by y-coordinate.

Parameters:

Name Type Description Default
quad ndarray

(4, 2) array of (x, y) points, in any order.

required

Returns:

Type Description
ndarray

np.ndarray: (4, 2) array ordered [top_left, top_right, bottom_right, bottom_left].

reading_order(boxes)

Order boxes into rows top to bottom, then left to right within each row.

Boxes are visited by their top edge. The first box of a row anchors it, and a later box joins only when its vertical overlap with the anchor covers at least half of the taller of the two heights, so a tall sidebar or heading cannot swallow unrelated lines beside it.

Parameters:

Name Type Description Default
boxes Sequence[tuple[float, float, float, float]]

(x_min, y_min, x_max, y_max) boxes, in any order.

required

Returns:

Type Description
list[int]

list[int]: Indices into boxes, permuted into reading order.