Skip to content

Commit b32f89f

Browse files
committed
update write_pcd to save point cloud as dense image
1 parent fe924a5 commit b32f89f

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

freenect2/__init__.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -520,9 +520,9 @@ def write_pcd(self, file_object, undistorted, registered=None):
520520
rgb (:py:class:`Frame`): if not-None, the RGB data corresponding to
521521
the depth frame.
522522
"""
523-
undistorted_array = undistorted.to_array()
524-
rows, cols = np.nonzero(undistorted_array)
523+
cols, rows = np.meshgrid(np.arange(undistorted.width), np.arange(undistorted.height))
525524
xs, ys, zs = self.get_points_xyz(undistorted, rows, cols)
525+
n_points = int(np.product(xs.shape))
526526

527527
print('VERSION .7', file=file_object)
528528

@@ -531,23 +531,23 @@ def write_pcd(self, file_object, undistorted, registered=None):
531531
print('SIZE 4 4 4', file=file_object)
532532
print('TYPE F F F', file=file_object)
533533
print('COUNT 1 1 1', file=file_object)
534-
data = np.vstack((-xs, -ys, -zs)).T
534+
data = np.vstack((-xs.flatten(), -ys.flatten(), -zs.flatten())).T
535535
else:
536536
print('FIELDS x y z rgb', file=file_object)
537537
print('SIZE 4 4 4 4', file=file_object)
538538
print('TYPE F F F I', file=file_object)
539539
print('COUNT 1 1 1 1', file=file_object)
540-
bgrx = (registered.to_array()[rows, cols, ...]).astype(np.uint32)
541-
rgbs = bgrx[:, 0] + (bgrx[:, 1] << 8) + (bgrx[:, 2] << 16)
542-
data = np.vstack((-xs, -ys, -zs, rgbs)).T
540+
bgrx = registered.to_array().astype(np.uint32)
541+
rgbs = bgrx[..., 0] + (bgrx[..., 1] << 8) + (bgrx[..., 2] << 16)
542+
data = np.vstack((-xs.flatten(), -ys.flatten(), -zs.flatten(), rgbs.flatten())).T
543543

544-
print('WIDTH {}'.format(xs.shape[0]), file=file_object)
545-
print('HEIGHT 1', file=file_object)
544+
print('WIDTH {}'.format(undistorted.width), file=file_object)
545+
print('HEIGHT {}'.format(undistorted.height), file=file_object)
546546
print('VIEWPOINT 0 0 0 1 0 0 0', file=file_object)
547-
print('POINTS {}'.format(xs.shape[0]), file=file_object)
547+
print('POINTS {}'.format(n_points), file=file_object)
548548
print('DATA ascii', file=file_object)
549549

550-
assert data.shape[0] == xs.shape[0]
550+
assert data.shape[0] == n_points
551551

552552
for row in data:
553553
print(' '.join([str(f) for f in row]), file=file_object)

0 commit comments

Comments
 (0)