相对于中心缩放多边形

Scale polygon relative to the center

我有带多边形坐标的 numpy 数组

array([[726, 462],
       [724, 457],
       [723, 448],
...

我需要相对于多边形中心缩放多边形以便在 OpenCV 中绘制。

要计算多边形的中心,请参考此处的公式:Algorithm for calculating polygon center

>>> def center(x, y):
...     diff = x[:-1] * y[1:] - x[1:] * y[:-1]
...     coef = 1 / (diff.sum() * 3)
...     return coef * ((x[:-1] + x[1:]) * diff).sum(), coef * ((y[:-1] + y[1:]) * diff).sum()
...
>>> a
array([[726, 462],
       [724, 457],
       [723, 448],
       [726, 462]])
>>> # Ensure that the position of the last point is the same as that of the first point.
>>> pos = np.array(center(*a.T))
>>> multiple = 2    # Zoom in twice
>>> new_a = (a - pos) * multiple + pos

本人对OpenCV不熟悉,所以调用了Matplotlib的API进行绘制: