def get_empty_anchor_filter_2d(anchors, voxel_grid_2d, density_threshold=1): """ Returns a filter for empty anchors from the given 2D anchor list Args: anchors: list of 3d anchors in the format N x [x, y, z, dim_x, dim_y, dim_z] voxel_grid_2d: a VoxelGrid object containing a 2D voxel grid of point cloud used to filter the anchors density_threshold: minimum number of points in voxel to keep the anchor Returns: anchor filter: N Boolean mask """ format_checker.check_anchor_format(anchors) # Remove y dimensions from anchors to project into BEV anchors_2d = anchors[:, [0, 2, 3, 5]] # Get Integral image of the voxel, add 1 since filled = 0, empty is -1 leaf_layout = voxel_grid_2d.leaf_layout_2d + 1 leaf_layout = np.squeeze(leaf_layout) integral_image = IntegralImage2D(leaf_layout) # Make anchor container anchor_container = np.zeros([len(anchors_2d), 4]).astype(np.uint32) num_anchors = len(anchors_2d) # Set up objects containing corners of anchors top_left_up = np.zeros([num_anchors, 2]).astype(np.float32) bot_right_down = np.zeros([num_anchors, 2]).astype(np.float32) # Calculate minimum corner top_left_up[:, 0] = anchors_2d[:, 0] - (anchors_2d[:, 2] / 2.) top_left_up[:, 1] = anchors_2d[:, 1] - (anchors_2d[:, 3] / 2.) # Calculate maximum corner bot_right_down[:, 0] = anchors_2d[:, 0] + (anchors_2d[:, 2] / 2.) bot_right_down[:, 1] = anchors_2d[:, 1] + (anchors_2d[:, 3] / 2.) # map_to_index() expects N x 2 points anchor_container[:, :2] = voxel_grid_2d.map_to_index( top_left_up) anchor_container[:, 2:] = voxel_grid_2d.map_to_index( bot_right_down) # Transpose to pass into query() anchor_container = anchor_container.T # Get point density score for each anchor point_density_score = integral_image.query(anchor_container) # Create the filter anchor_filter = point_density_score >= density_threshold return anchor_filter
def test_integral_image_2d(self): # Generate integral image integral_image = IntegralImage2D(self.test_image) boxes = np.array([[0, 0, 1, 1], [0, 0, 2, 2], [0, 0, 3, 3]]).T.astype(np.uint32) occupancy_count = integral_image.query(boxes) # First box case = should be 1*1*1 = 1 self.assertTrue(occupancy_count[0] == 1) # Second box case = should be 2*2*2 = 8 self.assertTrue(occupancy_count[1] == 4) # Third box case = should be 3*3*3 = 27 self.assertTrue(occupancy_count[2] == 9) boxes = np.array([[1, 1, 2, 2], [1, 1, 3, 3]]).T.astype(np.uint32) occupancy_count = integral_image.query(boxes) # First box case = should be 1*1 = 1 self.assertTrue(occupancy_count[0] == 1) # Second box case = should be 2*2 = 4 self.assertTrue(occupancy_count[1] == 4) boxes = np.array([[0, 0, 3, 1]]).T.astype(np.uint32) occupancy_count = integral_image.query(boxes) # Flat Surface case = should be 1*3 = 3 self.assertTrue(occupancy_count[0] == 3) # Test outside the boundary boxes = np.array([[0, 0, 2312, 162]]).T.astype(np.uint32) occupancy_count = integral_image.query(boxes) self.assertTrue(occupancy_count[0] == 9)