pythonpoint-cloudskdtreeopen3d

open3D point indexes do not correspond to the order of the points returned


Here is what I am having trouble with. I have the indexes of some points, and at first, I return the first set of 5 points, and afterwards, I retrieve the point of the second index, but the point returned when I used the second index is not the second point in the set of 5 points.

This code is how I create the indexes, by using the open3d.geometry.KDTreeFlann.search_radius_vector_3d method.

pcd = o3d.io.read_point_cloud('/home/antonis/Desktop/1210/cloud_no_color.ply')
pcd_tree = pcd_tree = o3d.geometry.KDTreeFlann(pcd)
[k, idx, _] = pcd_tree.search_radius_vector_3d(np.array([0, 0, 0]), 5)

now that I have the indexes, I select the first 5 of them to do the test and retrieve their corresponding points: (open3d.geometry.PointCloud.select_by_index)

part_of_indexes = idx[0:5]

points = np.asarray(pcd.select_by_index(part_of_indexes).points)
print('Set of point: \n {}'.format(points))

This returns the following set of points:

Set of point: 
 [[-2.55539846 -1.85320044 -0.84582067]
 [-2.64479446 -1.7267524  -0.84633833]
 [-2.64432073 -1.71330798 -0.84426773]
 [-2.74227309 -1.56733859 -0.84633833]
 [-2.75684834 -1.53759551 -0.84582067]]

And when I select the point with the second index of the part_of_indexes

second_point= np.asarray(pcd.select_by_index([part_of_indexes[1]]).points)
print('The point returned by the second index is the following: {}'.format(second_point))

I get back a point that is not the second point of the previous set of points, but it is actually the last one.

The point returned by the second index is the following: [[-2.75684834 -1.53759551 -0.84582067]]

Any thoughts on that?


Solution

  • I will provide 2 answers the first was given to me on github from the user yuecideng so I will pass it here:

    The select_by_index method will not output the points with the order of the given index list, but ouput the original order of points.

    If you want to keep the order of outut points with index, you could use numpy array to do this like

    origin_points = np.asarray(pcd.points)
    points = origin_points[idx]
    

    The answer above works as is but in order to avoid the creation of a huge array, you can sort the indexes returned by the search_radius_vector_3d method.

    [k, idx, _] = pcd_tree.search_radius_vector_3d(pose, 20)
    list_of_indexes = list(idx)
    list_of_indexes.sort()
    points = np.asarray(pcd.select_by_index(list_of_indexes).points)