MCPcopy Create free account
hub / github.com/thygate/stable-diffusion-webui-depthmap-script / save_point_cloud

Function save_point_cloud

lib/test_utils.py:163–210  ·  view source on GitHub ↗

Save an RGB point cloud as a PLY file. :paras @pcd: Nx3 matrix, the XYZ coordinates @rgb: NX3 matrix, the rgb colors for each 3D point

(pcd, rgb, filename, binary=True)

Source from the content-addressed store, hash-verified

161 return pcd
162
163def save_point_cloud(pcd, rgb, filename, binary=True):
164 """Save an RGB point cloud as a PLY file.
165
166 :paras
167 @pcd: Nx3 matrix, the XYZ coordinates
168 @rgb: NX3 matrix, the rgb colors for each 3D point
169 """
170 assert pcd.shape[0] == rgb.shape[0]
171
172 if rgb is None:
173 gray_concat = np.tile(np.array([128], dtype=np.uint8), (pcd.shape[0], 3))
174 points_3d = np.hstack((pcd, gray_concat))
175 else:
176 points_3d = np.hstack((pcd, rgb))
177 python_types = (float, float, float, int, int, int)
178 npy_types = [('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('red', 'u1'), ('green', 'u1'),
179 ('blue', 'u1')]
180 if binary is True:
181 # Format into NumPy structured array
182 vertices = []
183 for row_idx in range(points_3d.shape[0]):
184 cur_point = points_3d[row_idx]
185 vertices.append(tuple(dtype(point) for dtype, point in zip(python_types, cur_point)))
186 vertices_array = np.array(vertices, dtype=npy_types)
187 el = PlyElement.describe(vertices_array, 'vertex')
188
189 # Write
190 PlyData([el]).write(filename)
191 else:
192 x = np.squeeze(points_3d[:, 0])
193 y = np.squeeze(points_3d[:, 1])
194 z = np.squeeze(points_3d[:, 2])
195 r = np.squeeze(points_3d[:, 3])
196 g = np.squeeze(points_3d[:, 4])
197 b = np.squeeze(points_3d[:, 5])
198
199 ply_head = 'ply\n' \
200 'format ascii 1.0\n' \
201 'element vertex %d\n' \
202 'property float x\n' \
203 'property float y\n' \
204 'property float z\n' \
205 'property uchar red\n' \
206 'property uchar green\n' \
207 'property uchar blue\n' \
208 'end_header' % r.shape[0]
209 # ---- Save ply data to disk
210 np.savetxt(filename, np.column_stack((x, y, z, r, g, b)), fmt="%d %d %d %d %d %d", header=ply_head, comments='')
211
212def reconstruct_depth(depth, rgb, dir, pcd_name, focal):
213 """

Callers 1

reconstruct_depthFunction · 0.85

Calls 1

appendMethod · 0.80

Tested by

no test coverage detected