Как построить поверхность через указанные c точек в matplotlib - PullRequest
0 голосов
/ 14 января 2020

У меня есть данные облака точек из трехмерного массива, и я использую ransa c, чтобы найти наземную плоскость из всех точек в облаке точек. Используя коэффициент из ransa c, я также могу построить поверхность. enter image description here

, но проблема в том, что когда я пытаюсь построить поверхность, поверхность становится прямоугольной angular в форме -

enter image description here

Как можно нарисовать поверхность только через зеленые точки?

Кстати - plt.plot_trisurf не работает для меня. enter image description here

вот код -

    while 1:
        #try:
        xyz = get_points()
        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]
    #############
            db = DBSCAN(eps=.1, min_samples=50).fit(outliers)
            cluster_labels = db.labels_
            num_clusters = len(set(cluster_labels))
            clusters = pd.Series([outliers[cluster_labels == n] for n in range(num_clusters)])
            clusters = np.array(clusters)
            n_clusters_ = len(set(cluster_labels)) - (1 if -1 in cluster_labels else 0)
    ##########
            C = np.empty(shape=(len(clusters), 3), dtype=np.float32)
            rects = np.empty(shape=(len(clusters), 6), dtype=np.float32)
            #print("Clusters",n_clusters_)
            for i in range(n_clusters_):
                mini = np.nanmin(clusters[i], axis=0)
                maxi = np.nanmax(clusters[i], axis=0)
                sp = maxi - mini
                rects[i] = [mini[0], mini[1], mini[2], sp[0], sp[1], sp[2]]
                C[i] = np.nanmean(clusters[i], axis=0, dtype=np.float32)
            unique_labels = set(cluster_labels)
            if animation:
                colors = [plt.cm.Spectral(each)
                          for each in np.linspace(0, 1, len(unique_labels))]
                for k, col in zip(unique_labels, colors):
                    if k == -1:
                        # Black used for noise.
                        col = [0, 0, 0, 1]
                rectangles = []
                if box:
                    for i in range(len(C)):

                        xx, yy, zz, dx, dy, dz = rects[i]
                        rectangles.append(ax.bar3d(xx, yy, zz, dx, dy, dz, color=colors[i], alpha=0.8))#############


                min_x = np.amin(inliers[:, 0])
                max_x = np.amax(inliers[:, 0])
                min_y = np.amin(inliers[:, 1])
                max_y = np.amax(inliers[:, 1])
                #print(min_x, min_y, max_y, max_x)
                x = np.arange(min_x, max_x)
                y = np.arange(min_y, max_y)
                #X, Y = np.meshgrid(x, y)
                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)
                #DD = ax.scatter(outliers[:, 0], outliers[:, 1], outliers[:, 2], s=1, c=db.labels_)
                #EE = ax.scatter(xyz[:, 0], xyz[:, 1], xyz[:, 2], c='k', s=1)

                CC = ax.scatter(inliers[:, 0], inliers[:, 1], inliers[:, 2], c='green', s=)


                plt.pause(0.001)
                AA.remove()
                #BB.remove()
                CC.remove()
...