本文介绍了使用RANSAC将飞机拟合到3D点云的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用scikit中的RANSAC使飞机适应点云.
I am trying to fit a plane to a point cloud using RANSAC in scikit.
我不知道该怎么做,如何绘制从ransac.predict
获得的平面.
I am not able to understand how to do it, how to plot the plane which I obtain from ransac.predict
.
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from sklearn import datasets, linear_model
diabetes = datasets.load_diabetes()
X_train = diabetes.data[:-20, (0,1)]
y_train = diabetes.target[:-20]
ransac = linear_model.RANSACRegressor(
linear_model.LinearRegression()
)
ransac.fit(X_train, y_train)
fig = plt.figure()
plt.clf()
ax = Axes3D(fig)
ax.plot_surface([-5,5],[-5,5], ransac.predict(X_train))
我收到错误消息
ValueError: shape mismatch: objects cannot be broadcast to a single shape
推荐答案
在此示例中,您仅使用2个适合的特征不是PLANE而是一条线.
In this example, you only use 2 features to the fit is not a PLANE but a line.
这也可以从以下位置看到:
This can also be seen from:
ransac.estimator_.coef_
array([266.63361536, -48.86064441])
包含您拥有的两个功能中每个功能的权重.
that contains a weight for each of the 2 features that you have.
让我们制作一个真实的3D案例:
Let's make a real 3D case:
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from sklearn import datasets, linear_model
diabetes = datasets.load_diabetes()
X_train = diabetes.data[:-20, (0,1,2)]
y_train = diabetes.target[:-20]
ransac = linear_model.RANSACRegressor(linear_model.LinearRegression())
ransac.fit(X_train, y_train)
# the plane equation
z = lambda x,y: (-ransac.estimator_.intercept_ - ransac.estimator_.coef_[0]*x - ransac.estimator_.coef_[1]*y) / ransac.estimator_.coef_[2]
tmp = np.linspace(-5,5,50)
x,y = np.meshgrid(tmp,tmp)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot3D(X_train[:,0], X_train[:,1], X_train[:,2], 'or')
ax.plot_surface(x, y, z(x,y))
ax.view_init(10, 60)
plt.show()
这篇关于使用RANSAC将飞机拟合到3D点云的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!