点云随机采样

import numpy as np


def random_sample(num_points, num_choices):
    # 如果选取的数目大于可选数目,使用抽样方法
    if num_choices > num_points:
        # 随机抽样,确保选取的数字不会重复
        indices = np.random.choice(num_points, size=num_points, replace=False)
        # indices = np.arange(0, num_points)
        # 重复抽样直到满足选取数目
        indices = np.concatenate([indices, np.random.choice(num_points, size=num_choices - num_points, replace=True)])
    else:
        # 如果可选数目大于等于选取的数目,直接进行随机选择
        indices = np.random.choice(num_points, size=num_choices, replace=False)
    return indices

if __name__ == '__main__':
    import pyvista as pv

    file_path = './test2.ply'
    mesh = pv.read(file_path)
    points = mesh.points
    colors = mesh.active_scalars

    choice = random_sample(len(mesh.points), 5000)
    # choice = np.random.choice(len(mesh.points), 4000, replace=True)
    print(np.unique(choice).shape)

    sampled_points = points[choice]
    sampled_colors = colors[choice]

    print(points.shape, sampled_points.shape, sampled_colors.shape)
    # pl = pv.Plotter(shape=(1, 2))
    #
    # pl.subplot(0, 0)
    # pl.add_points(points, scalars=colors, point_size=5, style="points", rgb=True)
    # pl.add_title(str(points.shape[0]), font_size=20, font='times')
    # pl.view_xy()
    #
    # pl.subplot(0, 1)
    # pl.add_points(sampled_points, scalars=sampled_colors, point_size=5, style="points", rgb=True)
    # pl.add_title(str(sampled_points.shape[0]), font_size=20, font='times')
    # pl.view_xy()
    # pl.show()
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值