def projection_plot(pcpath='', noise=0.05, outlier=0.05, savefig=False):
    f_list = [
        pcpath + '/' + i for i in os.listdir(pcpath)
        if os.path.splitext(i)[1] == '.ply'
    ]
    fig = plt.figure(figsize=(38, 20), dpi=600, facecolor='w')
    colunms = 8
    for i, j in enumerate(f_list):
        for k in range(colunms):
            pc = PointCloud(j)
            pc.down_sample(number_of_downsample=10000)
            pc.add_noise(noise)
            pc.add_outlier(outlier)
            pts_size = 2.5
            if i == 7:
                pts_size = 1
            try:
                mfig = pc.half_by_plane(n=1024,
                                        grid_resolution=(200, 200),
                                        show_result=pts_size)
            except:
                try:
                    mfig = pc.half_by_plane(n=1024,
                                            grid_resolution=(250, 250),
                                            show_result=pts_size)
                except:
                    try:
                        mfig = pc.half_by_plane(n=1024,
                                                grid_resolution=(300, 300),
                                                show_result=pts_size)
                    except:
                        mfig = pc.half_by_plane(n=1024,
                                                grid_resolution=(650, 650),
                                                show_result=pts_size)

            f = mlab.gcf()  # this two line for mlab.screenshot to work
            f.scene._lift()
            if savefig:
                mlab.savefig(str(i) + str(k) + '.png')
            img = mlab.screenshot(figure=mfig)
            mlab.close()
            ax = fig.add_subplot(len(f_list), colunms, i * colunms + k + 1)
            ax.imshow(img)
            ax.set_axis_off()

    plt.subplots_adjust(wspace=0, hspace=0)

    if savefig:
        plt.savefig('projection.png')
        plt.show()
    plt.close()
def knn_plot(pc_path=''):

    f_list = [
        base_path + '/' + i for i in os.listdir(base_path)
        if os.path.splitext(i)[1] == '.ply'
    ]
    for j, i in enumerate(f_list):
        if j < 4:
            pc = PointCloud(i)
            pc.down_sample(number_of_downsample=4096)
            pc.add_noise(factor=0.04)
            pc.add_outlier(factor=0.04)
            fig = pc.compute_key_points(
                percentage=0.02,
                resolution_control=1 / 15,
                rate=0.05,
                use_deficiency=False,
                show_result=True)  # get the key points id

            f = mlab.gcf()  # this two line for mlab.screenshot to work
            f.scene._lift()
            mlab.savefig(filename=str(j) + '_0.png')
            mlab.close()

            colorset = np.random.random((100, 3))
            fig = pc.generate_k_neighbor(k=32,
                                         show_result=True,
                                         colorset=colorset)

            f = mlab.gcf()  # this two line for mlab.screenshot to work
            f.scene._lift()
            mlab.savefig(filename=str(j) + '_1.png')
            mlab.close()

            fig = pc.generate_k_neighbor(k=64,
                                         show_result=True,
                                         colorset=colorset)
            f = mlab.gcf()  # this two line for mlab.screenshot to work
            f.scene._lift()
            mlab.savefig(filename=str(j) + '_2.png')
            mlab.close()

            fig = pc.generate_k_neighbor(k=128,
                                         show_result=True,
                                         colorset=colorset)
            f = mlab.gcf()  # this two line for mlab.screenshot to work
            f.scene._lift()
            mlab.savefig(filename=str(j) + '_3.png')
            mlab.close()
def noise_outliers(pointclous, noise=0.05, outliers=0.05):

    fig = plt.figure(figsize=(38, 20), dpi=600, facecolor='w')
    for j, i in enumerate(pointclous):
        pc = PointCloud(i)
        pc.down_sample(number_of_downsample=1024)

        for k in range(4):
            if k == 3:
                k = 4
            pc.add_noise(factor=k * noise)
            pc.add_outlier(factor=k * outliers)
            m_fig = mlab.figure(bgcolor=(1, 1, 1))

            mlab.points3d(pc.position[:, 0],
                          pc.position[:, 1],
                          pc.position[:, 2],
                          pc.position[:, 2] * 10**-2 + 1,
                          colormap='Spectral',
                          scale_factor=2,
                          figure=m_fig)

            # mlab.gcf().scene.parallel_projection = True  # parallel projection
            f = mlab.gcf()  # this two line for mlab.screenshot to work
            f.scene._lift()
            # mlab.show()  # for testing
            img = mlab.screenshot(figure=m_fig)
            mlab.close()
            if k == 4:
                k = 3
            ax = fig.add_subplot(4, 8, (j + 1) + k * 8)
            ax.imshow(img)
            ax.set_axis_off()

    plt.subplots_adjust(wspace=0, hspace=0)
    plt.show()
コード例 #4
0
def augment_data(base_path='',
                 pc_path='',
                 add_noise=0.04,
                 add_outlier=0.04,
                 n=5000,
                 not_project=False,
                 show_result=False):

    pc = PointCloud(pc_path)
    pc.down_sample()

    if add_noise is not None:
        pc.add_noise(factor=add_noise)
    if add_outlier is not None:
        pc.add_outlier(factor=add_outlier)

    if not_project:
        for i in range(n):
            if i % 10 == 0:
                print('saving number', i + 1,
                      'th lab random sample point clouds')
            temp = deepcopy(pc)
            temp.down_sample(number_of_downsample=1024)
            np.savetxt(base_path + '/random_sample' + str(i) + '.txt',
                       temp.position,
                       delimiter=' ')
    else:
        for i in range(n):
            if i % 10 == 0:
                print('saving number', i + 1, 'th lab_project point clouds')

            #pc.cut_by_plane()  # todo manually
            #pc2 = PointCloud(pc.visible)
            pc2 = pc
            try:
                pc2.half_by_plane(n=1024, grid_resolution=(200, 200))
            except:
                try:
                    pc2.half_by_plane(n=1024, grid_resolution=(250, 250))
                except:
                    try:
                        pc2.half_by_plane(n=1024, grid_resolution=(300, 300))
                    except:
                        pc2.half_by_plane(n=1024, grid_resolution=(650, 650))

            np.savetxt(base_path + '/lab_project' + str(i) + '.txt',
                       pc2.visible,
                       delimiter=' ')  # pc.visible will variant

    if show_result:
        dir_list = [
            base_path + '/' + i for i in os.listdir(base_path)
            if os.path.isdir(i)
        ]
        fig = mlab.figure(size=(1000, 1000), bgcolor=(1, 1, 1))
        for i in dir_list:
            color = np.random.random((1, 3))
            pc = np.loadtxt(i + '/lab_project1')
            mlab.points3d(pc[:, 0],
                          pc[:, 1],
                          pc[:, 2],
                          pc[:, 2] * 10**-9,
                          color=color,
                          figure=fig)
コード例 #5
0
def test_data(h5_path='', rand_trans=False, showinone=False):
    """
    test and show if the h5 point cloud data is generate correctly
    :param h5_path:
    :param rand_trans:
    :param showinone: show the whole batch of point clouds in one picture
    :return:
    """
    h5file = read_data(h5_path)
    trainset = h5file['train_set'][...]
    train_local = h5file['train_set_local'][...]
    print('train_local:', train_local, 'train_local shape:', train_local.shape)
    if rand_trans:
        trainset += -300 + 600 * np.random.random(
            size=(20000, 1, 3))  # 20000 * 1024 * 3

    if showinone:
        ind = np.random.choice(20000, 20)
        points = trainset[ind, :, :]
        points = np.reshape(points, [-1, 3])
        points = PointCloud(points)
        points.show()

    for i in range(1):
        fig = plt.figure()
        for k in range(4):
            a = np.squeeze(trainset[1 + k * 5000, :, :])
            a = PointCloud(a)
            origin = a.show(not_show=True)
            mlab.show(origin)

            mlab.gcf().scene.parallel_projection = True  # parallel projection
            f = mlab.gcf()  # this two line for mlab.screenshot to work
            f.scene._lift()
            img = mlab.screenshot(antialiased=False)

            mlab.close()
            ax = fig.add_subplot(4, 4, 1 + k * 4)
            ax.imshow(img)
            ax.set_axis_off()

            a.add_noise(factor=5 / 100)
            noise = a.show(not_show=True)
            mlab.show(noise)
            mlab.gcf().scene.parallel_projection = True  # parallel projection
            f = mlab.gcf()  # this two line for mlab.screenshot to work
            f.scene._lift()
            img = mlab.screenshot()
            mlab.close()
            ax = fig.add_subplot(4, 4, 2 + k * 4)
            ax.imshow(img)
            ax.set_axis_off()

            a.add_outlier(factor=5 / 100)
            outlier = a.show(not_show=True)
            mlab.show(outlier)
            mlab.gcf().scene.parallel_projection = True  # parallel projection
            f = mlab.gcf()  # this two line for mlab.screenshot to work
            f.scene._lift()
            img = mlab.screenshot()
            mlab.close()
            ax = fig.add_subplot(4, 4, 4 + k * 4)
            ax.imshow(img)
            ax.set_axis_off()

            a = np.squeeze(trainset[1 + k * 5000, :, :])
            a = PointCloud(a)
            a.add_outlier(factor=5 / 100)
            outlier = a.show(not_show=True)
            mlab.show(outlier)
            mlab.gcf().scene.parallel_projection = True  # parallel projection
            f = mlab.gcf()  # this two line for mlab.screenshot to work
            f.scene._lift()
            img = mlab.screenshot()
            mlab.close()
            ax = fig.add_subplot(4, 4, 3 + k * 4)
            ax.imshow(img)
            ax.set_axis_off()

        plt.subplots_adjust(wspace=0, hspace=0)
        plt.show()