尝试这个 -
from mpl_toolkits.mplot3d.axes3d import *
import matplotlib.pyplot as plt
from sklearn import linear_model
fig = plt.figure("Pointcloud")
ax = Axes3D(fig)
ax.grid = True
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
xyz = get_points()# here xyz is a 3d numpy array
if xyz.size > 10:
XY = xyz[:, :2]
Z = xyz[:, 2]
ransac = linear_model.RANSACRegressor(residual_threshold=0.01)
ransac.fit(XY, Z)
inlier_mask = ransac.inlier_mask_
outlier_mask = np.logical_not(inlier_mask)
inliers = np.zeros(shape=(len(inlier_mask), 3))
outliers = np.zeros(shape=(len(outlier_mask), 3))
a, b = ransac.estimator_.coef_
d = ransac.estimator_.intercept_
for i in range(len(inlier_mask)):
if not outlier_mask[i]:
inliers[i] = xyz[i]
else:
outliers[i] = xyz[i]
min_x = np.amin(inliers[:, 0])
max_x = np.amax(inliers[:, 0])
min_y = np.amin(inliers[:, 1])
max_y = np.amax(inliers[:, 1])
x = np.linspace(min_x, max_x)
y = np.linspace(min_y, max_y)
X, Y = np.meshgrid(x, y)
Z = a * X + b * Y + d
AA = ax.plot_surface(X, Y, Z, cmap='binary', rstride=1, cstride=1,
alpha=1.0)
BB = ax.scatter(outliers[:, 0], outliers[:, 1], outliers[:, 2],c='k', s
=1)
CC = ax.scatter(inliers[:, 0], inliers[:, 1], inliers[:, 2], c='green',
s=1)
plt.show()
或者请提供您的数据集。也可以使用 ransac 参数