실제로이 경우 scipy.interpolate.inter1d
을 사용할 수 있습니다. 다각형의 x 및 y 구성 요소를 별도의 시리즈로 처리해야합니다. 사각형과 빠른 예를 들어
: 당신이 볼 수
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
x = [0, 1, 1, 0, 0]
y = [0, 0, 1, 1, 0]
t = np.arange(len(x))
ti = np.linspace(0, t.max(), 10 * t.size)
xi = interp1d(t, x, kind='cubic')(ti)
yi = interp1d(t, y, kind='cubic')(ti)
fig, ax = plt.subplots()
ax.plot(xi, yi)
ax.plot(x, y)
ax.margins(0.05)
plt.show()
그러나, 이것은 0,0에서 몇 가지 문제가 발생합니다.
스플라인 세그먼트가 두 개 이상의 점에 의존하기 때문에 이러한 현상이 발생합니다. 첫 번째와 마지막 점은 보간 된 방식으로 "연결"되지 않습니다. 끝점의 스플라인에 대한 "랩 어라운드 (wrap-around)"경계 조건이 있도록 x 및 y 시퀀스를 두 번째 및 마지막 점과 두 번째 점으로 "패딩"하여이 문제를 해결할 수 있습니다.
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
x = [0, 1, 1, 0, 0]
y = [0, 0, 1, 1, 0]
# Pad the x and y series so it "wraps around".
# Note that if x and y are numpy arrays, you'll need to
# use np.r_ or np.concatenate instead of addition!
orig_len = len(x)
x = x[-3:-1] + x + x[1:3]
y = y[-3:-1] + y + y[1:3]
t = np.arange(len(x))
ti = np.linspace(2, orig_len + 1, 10 * orig_len)
xi = interp1d(t, x, kind='cubic')(ti)
yi = interp1d(t, y, kind='cubic')(ti)
fig, ax = plt.subplots()
ax.plot(xi, yi)
ax.plot(x, y)
ax.margins(0.05)
plt.show()
그리고 그냥 데이터와 모양을 보여주기 :
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
x = [-0.25, -0.625, -0.125, -1.25, -1.125, -1.25,
0.875, 1.0, 1.0, 0.5, 1.0, 0.625, -0.25]
y = [1.25, 1.375, 1.5, 1.625, 1.75, 1.875, 1.875,
1.75, 1.625, 1.5, 1.375, 1.25, 1.25]
# Pad the x and y series so it "wraps around".
# Note that if x and y are numpy arrays, you'll need to
# use np.r_ or np.concatenate instead of addition!
orig_len = len(x)
x = x[-3:-1] + x + x[1:3]
y = y[-3:-1] + y + y[1:3]
t = np.arange(len(x))
ti = np.linspace(2, orig_len + 1, 10 * orig_len)
xi = interp1d(t, x, kind='cubic')(ti)
yi = interp1d(t, y, kind='cubic')(ti)
fig, ax = plt.subplots()
ax.plot(xi, yi)
ax.plot(x, y)
ax.margins(0.05)
plt.show()
참고이 방법으로 "오버 슈트"꽤 얻을 . 이는 3 차 스플라인 보간 때문입니다. @ pythonstarter의 제안은 그것을 처리하는 또 다른 좋은 방법이지만, 베 지어 곡선은 동일한 문제 (기본적으로는 수학적으로 동일합니다. 제어점이 정의되는 방식의 문제 일뿐입니다)로 인해 어려움을 겪을 것입니다. 폴리곤을 다듬는 데 특화된 방법 (예 : 다항식 근사 식 지수 커널 (PAEK))을 비롯하여 다듬기를 처리 할 수있는 여러 가지 방법이 있습니다. 필자는 PAEK를 구현하려 한 적이 없으므로 어떻게 참여했는지 확신 할 수 없습니다. 보다 견고하게 수행해야하는 경우 PAEK 또는 다른 유사한 방법을 조사해보십시오.
다음은이 알고리즘에 대한 Python 2.7.X 구현입니다. https://stackoverflow.com/questions/47068504/ find-python-chaikins-corner-cutting-algorithm의 어디에서 찾을 것인가? –