Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding points within a bounding box with numpy

I have millions of xyz-coordinates from multiple point cloud files which I am storing inside a 2-dimensional numpy array: [[x1, y1, z1], [x2, y2, z2],..., [xn, yn, zn]].

I want to filter all the points which are inside a specific bounding box described by 4 coordinates [[x1, y1], [x2, y2]] i.e. the lower left and upper right coordinates of a rectangle.

I have already found the following piece of code to filter coordinates with numpy and it's almost what I want. The only difference is (if I'm getting it right) that my 2-dimensional array also has got z-coordinates.

import random
import numpy as np

points = [(random.random(), random.random()) for i in range(100)]

bx1, bx2 = sorted([random.random(), random.random()])
by1, by2 = sorted([random.random(), random.random()])

pts = np.array(points)
ll = np.array([bx1, by1])  # lower-left
ur = np.array([bx2, by2])  # upper-right

inidx = np.all(np.logical_and(ll <= pts, pts <= ur), axis=1)
inbox = pts[inidx]
outbox = pts[np.logical_not(inidx)]

How would I have to modifiy the code above to make it work with xyz-coordinates to be filtered by a bounding box described with two xy-coordinates?

like image 935
conste Avatar asked Feb 20 '17 19:02

conste


2 Answers

Select the X and Y coordinates of your points:

xy_pts = pts[:,[0,1]]

Now, simply use xy_pts instead of pts in the comparisons:

inidx = np.all((ll <= xy_pts) & (xy_pts <= ur), axis=1)
like image 130
DYZ Avatar answered Sep 25 '22 16:09

DYZ


I'm writing a Python library for working with point clouds and I have this function that I think that should work for you:

def bounding_box(points, min_x=-np.inf, max_x=np.inf, min_y=-np.inf,
                        max_y=np.inf, min_z=-np.inf, max_z=np.inf):
    """ Compute a bounding_box filter on the given points

    Parameters
    ----------                        
    points: (n,3) array
        The array containing all the points's coordinates. Expected format:
            array([
                [x1,y1,z1],
                ...,
                [xn,yn,zn]])

    min_i, max_i: float
        The bounding box limits for each coordinate. If some limits are missing,
        the default values are -infinite for the min_i and infinite for the max_i.

    Returns
    -------
    bb_filter : boolean array
        The boolean mask indicating wherever a point should be keeped or not.
        The size of the boolean mask will be the same as the number of given points.

    """

    bound_x = np.logical_and(points[:, 0] > min_x, points[:, 0] < max_x)
    bound_y = np.logical_and(points[:, 1] > min_y, points[:, 1] < max_y)
    bound_z = np.logical_and(points[:, 2] > min_z, points[:, 2] < max_z)

    bb_filter = np.logical_and(np.logical_and(bound_x, bound_y), bound_z)

    return bb_filter

Here is an example of what you are asking:

10 million points:

points = np.random.rand(10000000, 3)

Rectangle in the format you specify:

rectangle = np.array([[0.2, 0.2],
                     [0.4, 0.4]])

Unpack the rectangle:

min_x = rectangle[:,0].min()
max_x = rectangle[:,0].max()
min_y = rectangle[:,1].min()
max_y = rectangle[:,1].max()

Get boolean array marking points inside the box:

%%timeit
inside_box = bounding_box(points, min_x=min_x, max_x=max_x, min_y=min_y, max_y=max_y)
1 loop, best of 3: 247 ms per loop

This way you can use the array as follows:

points_inside_box = points[inside_box]
points_outside_box = points[~inside_box]
like image 43
David de la Iglesia Avatar answered Sep 24 '22 16:09

David de la Iglesia