简体   繁体   English

使用 RANSAC 将平面拟合到 3D 点云

[英]Fit a plane to 3D point cloud using RANSAC

I am trying to fit a plane to a point cloud using RANSAC in scikit.我正在尝试使用 scikit 中的 RANSAC 将飞机安装到点云上。

I am not able to understand how to do it, how to plot the plane which I obtain from ransac.predict .我无法理解如何去做,如何绘制我从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))

I am getting error message我收到错误信息

ValueError: shape mismatch: objects cannot be broadcast to a single shape

In this example, you only use 2 features to the fit is not a PLANE but a line.在此示例中,您仅使用 2 个特征来拟合不是 PLANE 而是一条线。

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.包含您拥有的 2 个特征中的每一个的权重。


Let's make a real 3D case:让我们制作一个真正的 3D 案例:

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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM