【发布时间】:2017-09-04 02:17:13
【问题描述】:
我正在将样条线拉直作为我更大项目的一个组成部分,以拉直弯曲的文本。
将样条曲线拟合到我的数据点后,我使用 scipy 的 splev 来获得曲线沿曲线每个点的样条曲线的导数。由于导数为我提供了曲线在给定点的切线斜率(除非我很困惑),我通过将导数与斜率为 0 的线进行比较来确定生成直线所需的旋转。
在每个点建立了拉直样条曲线所需的旋转后,我循环遍历每个点并将校正旋转应用于当前点和每个前一个点。
相关代码如下:
import numpy as np
from numpy import arange
from scipy import interpolate
import matplotlib.pyplot as plt
import math
import random
def rotate(origin, point, angle):
ox, oy = origin
px, py = point
qx = ox + math.cos(angle) * (px - ox) - math.sin(angle) * (py - oy)
qy = oy + math.sin(angle) * (px - ox) + math.cos(angle) * (py - oy)
return qx, qy
xxx = [0,2,4,4,2,0]
yyy = [0,2,4,6,8,10]
tckp, u = interpolate.splprep([xxx, yyy], s=3, k=2, nest=-1)
xpointsnew, ypointsnew = interpolate.splev(u, tckp)
dx, dy = interpolate.splev(u, tckp, der=1)
fullder = dy/dx
rotating_x = xxx
rotating_y = yyy
index = -1
for i in fullder:
index += 1
corrective_rotation = -(math.degrees(math.atan(0)-math.atan(fullder[index])))
print(corrective_rotation)
rotation_center = [rotating_x[index], rotating_y[index]]
target_indices = np.arange(0,index,1)
for i in target_indices:
rotation_target = [rotating_x[i], rotating_y[i]]
qx, qy = rotate(rotation_target,rotation_center,math.radians(corrective_rotation))
rotating_x[i] = qx
rotating_y[i] = qy
print(rotating_x)
print(rotating_y)
plt.plot(xpointsnew, ypointsnew, 'r-')
plt.plot(rotating_x, rotating_y, 'b-')
plt.show()
我正在做的事情不起作用,但我不知道为什么。结果线不仅不直,而且比原始曲线短得多。上述方法在某种程度上是否存在根本性缺陷?我在我的代码中做一些愚蠢的事情吗?我真的很感激第二双眼睛。
【问题讨论】:
-
你定义了旋转方法,你根本不使用它。
-
@MishaVacic 我不知道吗?我很确定我在这里:
qx, qy = rotate(rotation_target,rotation_center,math.radians(corrective_rotation))