【发布时间】:2017-03-16 07:41:12
【问题描述】:
我试图在图像上绘制文本边界框。图像 用给定的一组系数进行透视变换。变换前的文本坐标是已知的,我想计算变换后的文本坐标。
据我了解,如果我将透视变换与图像变换中使用的系数应用于文本坐标,我将得到变换后文本的结果坐标。但是,文本没有出现在它应该出现的位置。
较小的白框很好地界定了文本,因为我知道文本的坐标。
由于坐标转换过程中的一些错误,较小的白框没有包围文本。
我关注文档 reference for coefficients of perspective transformation 并使用以下代码找到图像变换的系数:origin of the code is from this answer
def find_coeffs(pa, pb):
'''
find the coefficients for perspective transform.
parameters:
pa : verticies in the resulting plane
pb : verticies in the current plane
retrun:
coeffs : 8- tuple
coefficents for PIL perspective transform
'''
matrix = []
for p1, p2 in zip(pa, pb):
matrix.append([p1[0], p1[1], 1, 0, 0, 0, -p2[0]*p1[0], -p2[0]*p1[1]])
matrix.append([0, 0, 0, p1[0], p1[1], 1, -p2[1]*p1[0], -p2[1]*p1[1]])
A = np.matrix(matrix, dtype=np.float)
B = np.array(pb).reshape(8)
res = np.dot(np.linalg.inv(A.T * A) * A.T, B)
return np.array(res).reshape(8)
我的文本边界框转换代码:
# perspective transformation
a, b, c, d, e, f, g, h = coeffs
# return two vertices defining the bounding box
new_x0 = float(a * new_x0 - b * new_y0 + c) / float(g * new_x0 + h * new_y0 + 1)
new_y0 = float(d * new_x0 + e * new_y0 + f) / float(g * new_x0 + h * new_y0 + 1)
new_x1 = float(a * new_x1 - b * new_y1 + c) / float(g * new_x1 + h * new_y1 + 1)
new_y1 = float(d * new_x1 + e * new_y1 + f) / float(g * new_x1 + h * new_y1 + 1)
我也去了Pillow Github,但是找不到定义透视变换的源代码。
更多关于透视变换数学的信息。 The Geometry of Perspective Drawing on the Computer
谢谢。
【问题讨论】:
-
我不确定你的意思。您是要转换第二张图片还是要转换您绘制的白框以适合图片/标志?
-
我想转换第二个图中较小的白框,使其成为文本的边界。
标签: python image transform pillow