2017-06-14 17 views
0

我正在做一些車道檢測作爲一個有趣的項目,我試圖創建一個三次樣條來表示車道。但是,當我使用scipy CubicSpline函數時,我得到了一些荒謬的大值。具有非常大的y值的scipy立方樣條

這裏是我的代碼:

from scipy import interpolate 
from scipy.interpolate import CubicSpline 
from scipy.interpolate import InterpolatedUnivariateSpline 
from scipy.interpolate import interp1d 


rows = img_size[0] # width 
height = left_lane.shape[0] 
y_values = [0, height/2, height] 

plt.figure() 
plt.imshow(left_lane, cmap='gray') 
splines = [] 

particle = particles[0] 
cx = [particle[0], particle[1], particle[2]] 
cy = [y_values[0], y_values[1], y_values[2]] 

points = zip(cx, cy) 
points = sorted(points, key=lambda point: point[0]) 
x1, y1 = zip(*points) 

x1 = np.asarray(x1) 
y1 = np.asarray(y1) 
s = CubicSpline(x1, y1) 

new_x = np.arange(min(x1), max(x1), 0.1) 
new_y = s(new_x) 
plt.plot(new_x, new_y) 

plt.show() 

這裏是輸出:

plotted cubic spline

這裏是與控制點的原始圖像畫在:

original image with control points

我不明白爲什麼樣條算法給了我這樣一個簡單樣條的這麼大的值。那麼,這裏有什麼問題?值不好?立方樣條的使用不正確?

謝謝你的幫助!

回答

0

問題在於樣條線在這個圖像中是垂直的,這意味着x值並不總是遞增的順序(例如第二個控制>第一個控制點)。所以,當我整理我的列表時,可能會發生這樣的事情(2,1,3)。然後,scipy必須使用一些巨大的係數來適應這個樣條。

解決的辦法是翻轉軸,使y現在是x軸。現在排序不會導致控制點的任何奇怪的排序。

cx = [particle[0], particle[1], particle[2]] 
cy = [y_values[0], y_values[1], y_values[2]] 

# Sort particles in increasing x order 
points = zip(cx, cy) 
points = sorted(points, key=lambda point: point[1]) 
x1, y1 = zip(*points) 

x1 = np.asarray(x1) 
y1 = np.asarray(y1) 
s = CubicSpline(y1, x1) 

new_x = np.arange(min(y1), max(y1), 0.1) 
new_y = s(new_x) 
plt.plot(new_y, new_x) 
plt.plot(cx, cy, '.')