pythonobject-detectionpoint-cloud-libraryyolov5ply-file-format

How do I get z-axis coordinate from ply file for a given point ( x, y ) in python?


I hope this finds you in good health. I am really really new to working with 3d objects. I have been working with an Object Detection Algorithm (YOLO) recently. As Yolo returns the bounding box coordinates of an object, we can easily get the (x,y) coordinates of the bounding boxes. However recently, I have added a TOF camera to the project that can sense dept(z-axis coordinate) for each pixel. All of this data are stored in a corresponding ".ply". I want to get the z-axis value for each bounding-box coordinates that yolo outputs.

Right now my .ply file shows this output:

array([[-818.5 , -830.75, 1949.25],
       [-748.  , -814.5 , 1918.5 ],
       [-704.  , -806.75, 1905.25],
       ...,
       [ 903.75,  790.  , 1844.25],
       [ 906.75,  789.5 , 1843.  ],
       [ 915.  ,  793.75, 1852.25]], dtype=float32)

I am using python's plyfile library to read the data from my .ply file

This is how far I have gone:

def read_ply(filename):
    """ read XYZ point cloud from filename PLY file """
    plydata = PlyData.read(filename)
    x = np.asarray(plydata.elements[0].data['x'])
    y = np.asarray(plydata.elements[0].data['y'])
    z = np.asarray(plydata.elements[0].data['z'])
    return np.stack([x,y,z], axis=1)

coors =  read_ply("test.ply")
coors

This script reads the given .ply file and outputs the following vertexes (x,y,z) value from the ply file:

array([[-818.5 , -830.75, 1949.25],
       [-748.  , -814.5 , 1918.5 ],
       [-704.  , -806.75, 1905.25],
       ...,
       [ 903.75,  790.  , 1844.25],
       [ 906.75,  789.5 , 1843.  ],
       [ 915.  ,  793.75, 1852.25]], dtype=float32)

Now I want to find the z-axis for the corresponding pixels that are present in the bounding box that YOLO outputs.


Solution

  • Finally figured out what i was doing wrong. This is the correct working code. Cheers!

    #Enter your x & y coors to get corresponding z-axis value
    
    x =  2300          
    y =  1822     
    
    xy = np.array([x, y])
    z = coors[np.all(np.isclose(coors[:, :2], xy), axis=1), 2][0]
    print("x= {}  y= {}  z= {}".format(x, y, z))