zivid / zivid-python-samples

Python code samples for Zivid
https://zivid.com
BSD 3-Clause "New" or "Revised" License
38 stars 14 forks source link

nan_to_num #143

Closed christinaionescu closed 2 years ago

christinaionescu commented 2 years ago

Hi everyone,

when extracting the pointcloud I often see formulations where the np.nan_to_num function is applied to the xyz values. For example here https://github.com/zivid/zivid-python-samples/blob/master/source/sample_utils/display.py#L54-L93 in line 59. xyz = np.nan_to_num(xyz).reshape(-1, 3)

Why is this approach chosen? Would it not make sense to simply delete those nan values?

In my code I went for this version:

    frame = zivid.Frame(path)
    point_cloud = frame.point_cloud()
    xyz = pointcloud.copy_data("xyz") # Get point coordinates as [Height,Width,3]
    rgba = pointcloud.copy_data("rgba") # Get point colors as [Height,Width,4]
    rgb = rgba[:, :, 0:3]
    xyz = np.nan_to_num(xyz).reshape(-1, 3)
    zero_index = np.where((xyz[:,0]!=0 ) & (xyz[:,1]!=0 ) & (xyz[:,2]!=0 ) == True ) #remove zero values
    xyz = xyz[zero_index]
    rgb = rgb.reshape(-1, 3)
    rgb = rgb[zero_index]

Is there a reason for your approach to keep them as zero values or do you have a better idea than mine?

Thank you!

torbsorb commented 2 years ago

Hi @christinaionescu, thanks for taking an interest in our code! This code is intended for use with our Zivid 3D cameras which produce an organized point cloud, including color. We usually want to maintain the organization to simplify lookup between the 2D image and 3D-coordinates. The reason we use nan_to_num is that some external tools, such as Open3D visualization, don't support nan-values.

Zero is a useful value as it's impossible to get a point inside the camera (in the camera origin). For visualization I agree that it's a bit silly to keep these values, and indeed we remove the organization when we reshape to a list of coordinates.

christinaionescu commented 2 years ago

Hi,

yes I completely understand why you do it this way if the organization is relevant. In my case I want to perform pointcloud registration so I reshape it anyways to transform the data to a pointcloud. Here, it can influence the pointcloud registration algorithm if I have many points set to zero instead of just deleting them. This is why it was relevant to me :)

Thank you for your answer and best regards

torbsorb commented 2 years ago

That makes perfect sense! Thanks :)