Local maxima in a point cloud

前端 未结 3 1427
遇见更好的自我
遇见更好的自我 2021-02-06 19:04

I have a point cloud C, where each point has an associated value. Lets say the points are in 2-d space, so each point can be represented with the triplet (x, y, v).

I

3条回答
  •  隐瞒了意图╮
    2021-02-06 19:24

    Following up on Yves' suggestion, here's an answer, which uses scipy's KDTree:

    from scipy.spatial.kdtree import KDTree
    import numpy as np
    
    def locally_extreme_points(coords, data, neighbourhood, lookfor = 'max', p_norm = 2.):
        '''
        Find local maxima of points in a pointcloud.  Ties result in both points passing through the filter.
    
        Not to be used for high-dimensional data.  It will be slow.
    
        coords: A shape (n_points, n_dims) array of point locations
        data: A shape (n_points, ) vector of point values
        neighbourhood: The (scalar) size of the neighbourhood in which to search.
        lookfor: Either 'max', or 'min', depending on whether you want local maxima or minima
        p_norm: The p-norm to use for measuring distance (e.g. 1=Manhattan, 2=Euclidian)
    
        returns
            filtered_coords: The coordinates of locally extreme points
            filtered_data: The values of these points
        '''
        assert coords.shape[0] == data.shape[0], 'You must have one coordinate per data point'
        extreme_fcn = {'min': np.min, 'max': np.max}[lookfor]
        kdtree = KDTree(coords)
        neighbours = kdtree.query_ball_tree(kdtree, r=neighbourhood, p = p_norm)
        i_am_extreme = [data[i]==extreme_fcn(data[n]) for i, n in enumerate(neighbours)]
        extrema, = np.nonzero(i_am_extreme)  # This line just saves time on indexing
        return coords[extrema], data[extrema]
    

提交回复
热议问题