Search code examples
pythonperformancenumpyroslidar

Trouble vectoring a NumPy


I am trying to write some efficient code for a school project using LIDAR. The goal is to filter out anything in the point cloud past 10 meters in the point cloud and pass it out. I can write a for loop to do this but it's not very optimized. My goal is to this efficiently with NumPy.

def get_distance(input_array):
        return np.sqrt(np.sum(np.square(input_array)))

def filter_by_distance(cloud, distance=10):
     cloud1 = [item for item in cloud if get_distance(item) <= distance]
     return np.asarray(cloud1)

cloud is a mulidimensional np.array containing [X,Y,Z,I]

[[23.157  0.032  0.992  0.34 ]
 [23.219  0.105  0.994  0.29 ]
 [23.282  0.179  0.996  0.26 ]
 ...
 [ 1.548 -1.101 -0.77   0.   ]
 [ 1.388 -0.978 -0.676  0.   ]
 [ 1.42  -0.964 -0.684  0.   ]]

I'm using get_distance(cloud[:,0:2]) the get the x,y distance, but I can't seem to find a way to use this trim the original point cloud without a for loop. I'd appreciate any help!


Solution

  • Calculate sum of square inner rows and no need to sqrt, directly compare with square distance

    def filter_by_distance(cloud, distance=10):
        # np.sum is implemented c and very fast
        # axis = 1 for summing row
        # no need to sqaure root (save computation)
        # : stand for row, 0:3 stand for column 0, 1, 2
        umask = np.sum(cloud[:, 0:3]**2, axis=1) < distance**2
        # umask is boolean array, whereever it is false, that cloud point will not be shown
        return cloud[umask]