<jupyter_start><jupyter_text>Fix distortionOur depth channel corresponds to the distance to the camera, instead of the depth to the camera level. This was causing distortions in the point clouds.<jupyter_code>f = 711.1111
cx = cy = 256

rows, cols = depth.shape
c, r = np.meshgrid(np.arange(cols), np.arange(rows), sparse=True)
c = c - cx
r = r - cy

valid = (depth > 0) & (depth < 1.5)

d_px = np.where(valid, np.sqrt(np.square(c) + np.square(r)), np.nan)  # pixel distance to (cx,cy) (image center)
z_px = np.where(valid, np.sqrt(np.square(d_px) + np.square(f)), np.nan)
z = np.where(valid, depth * f / z_px, np.nan)

x = np.where(valid, z * c / f, 0)
y = np.where(valid, z * r / f, 0)

pcd = np.dstack((x, y, z))
pcd.resize(rows*cols, 3)
pcd
o3d.visualization.draw([
    o3d.geometry.PointCloud(o3d.utility.Vector3dVector(
        np.array([p for p in pcd if not np.isnan(p[-1])])
    ))
])<jupyter_output>[Open3D INFO] Window window_12 created.
[Open3D INFO] DataChannelObserver::OnStateChange label: ServerDataChannel, state: closing, peerid: 0.33968204479977904
[Open3D INFO] DataChannelObserver::OnStateChange label: ServerDataChannel, state: closed, peerid: 0.33968204479977904
[Open3D INFO] DataChannelObserver::OnStateChange label: ClientDataChannel, state: closing, peerid: 0.33968204479977904
[Open3D INFO] DataChannelObserver::OnStateChange label: ClientDataChannel, state: closed, peerid: 0.33968204479977904
[Open3D INFO] DataChannelObserver::OnStateChange label: ServerDataChannel, state: closing, peerid: 0.3555980353198389
[Open3D INFO] DataChannelObserver::OnStateChange label: ServerDataChannel, state: closed, peerid: 0.3555980353198389
[Open3D INFO] DataChannelObserver::OnStateChange label: ClientDataChannel, state: closing, peerid: 0.3555980353198389
[Open3D INFO] DataChannelObserver::OnStateChange label: ClientDataChannel, state: closed, peerid: 0.3555980353198389
[Open3D INFO] Da[...]<jupyter_text>Conversion from (fixed) RGBD image<jupyter_code>rgbd_img = load_rgbd(img_fpath, (256,256), 711.1111)

fig, axs = plt.subplots(1,2)
fig.set_size_inches(10,10)

axs[0].imshow(rgbd_img.color, cmap='gray')
axs[1].imshow(rgbd_img.depth, cmap='gray')

axs[0].set_axis_off()
axs[1].set_axis_off()

fig.show()
f = 711.1111
t = 512

camera_params = o3d.camera.PinholeCameraIntrinsic(t,t,f,f,t/2,t/2)

pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_img, camera_params)
np.asarray(pcd.points)
o3d.visualization.draw([pcd])<jupyter_output>[Open3D INFO] Window window_1 created.
[Open3D INFO] [Called HTTP API] /api/hangup
[Open3D INFO] [Called HTTP API] /api/getMediaList
[Open3D INFO] [Called HTTP API] /api/getIceServers
[Open3D INFO] [Called HTTP API] /api/getIceServers
[Open3D INFO] [Called HTTP API] /api/call
[Open3D INFO] [Called HTTP API] /api/call<jupyter_text>Refactored<jupyter_code>pcd = load_pcd(img_fpath)

o3d.visualization.draw([pcd])<jupyter_output>[Open3D INFO] Window window_0 created.
[Open3D INFO] EGL headless mode enabled.
[Open3D INFO] ICE servers: {"stun:stun.l.google.com:19302", "turn:user:password@172.16.17.32:3478", "turn:user:password@172.16.17.32:3478?transport=tcp"}
[Open3D INFO] Set WEBRTC_STUN_SERVER environment variable add a customized WebRTC STUN server.
[Open3D INFO] WebRTC HTTP server handshake mode enabled.
[Open3D INFO] Open3D WebVisualizer is serving at http://192.168.3.11:8889.
[Open3D INFO] Set WEBRTC_IP and WEBRTC_PORT environment variable to customize the HTTP server address.
[Open3D INFO] [Called HTTP API] /api/getMediaList
[Open3D INFO] [Called HTTP API] /api/getIceServers
[Open3D INFO] [Called HTTP API] /api/call
{'fraction_non_alphanumeric': 0.12805555555555556, 'fraction_numerical': 0.07666666666666666, 'mean_word_length': 6.102564102564102, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 3, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '506740', 'license': 'CNRI-Python', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1362, 'n_words': 329, 'path': 'notebooks/00.02-bmp-rgbd-to-point-cloud.ipynb', 'repo_name': 'brunompacheco/part-counting', 'size': '3601'}