0

I want to use the ransac algorithm to segment the ground plane lidar rings, I use the python-pcl to do that, but I got the false results as the pictures showed below.

As we known, the lidar data has many rings of the ground plane, it can't find the right plane of the ground, but it found the plane above the ground. The reason I can guess is that maybe the ground of lidar is very sparse, and the plane above the ground has the points numbers more than the ground, so the algorithm find the false results. The code can be list as below:


seg = point_cloud.make_segmenter()

seg.set_optimize_coefficients(True)

seg.set_model_type(pcl.SACMODEL_PLANE)

seg.set_method_type(pcl.SAC_RANSAC)

seg.set_distance_threshold(0.1)

indices, model = seg.segment()

It's uncertain whether is the problem I guessed, so if anyone met the problem before, please tell me. And I don't know how to solve the problem, there is few information about the lidar rings segmentation, does anyone know how to solve it?

And is there other methods to do the lidar ground segmentation which I can get the code?

enter image description here

Jaiy
  • 1
  • 2
  • 1
    Welcome! Please edit your post to add formatting and some new lines to help readers. It is currently too compact and not easily readable. Also ask a question and do not ask for suggestions. And add code examples to illustrate your question. – ChrisR Sep 11 '18 at 12:45
  • @ChrisR: Thank you for your comments, I edit the question as you says, the code I used the ransac algorithm can be list as above. – Jaiy Sep 12 '18 at 01:50
  • @scopchanov: Hi, I just don't know why the ransac failed to seg the ground rings of the lidar data as the picture showed, I guessed it was a programming problem, but I don't know how to solve it, and the code of ransac has list above, thank you for your commemt. – Jaiy Sep 12 '18 at 01:53
  • Thanks for help, I have solved my problem by modifying my code. – Jaiy Sep 17 '18 at 09:43

1 Answers1

2

Try this -

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

Or please provide your dataset.Also play around with the ransac parameters

Samm Flynn
  • 314
  • 2
  • 13