Skip to content

Commit

Permalink
Replace pptk visualizer with open3D
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisasc committed Feb 12, 2021
1 parent bd89683 commit ccfa83a
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 122 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ There are two main categories of samples: **camera** and **applications**. The s
- **visualization**
- [**read_zdf_vis_3d**][read_zdf_vis_3d-url] - Read point cloud data from a ZDF file and visualize it.
- **Dependencies:**
- [pptk](https://github.com/heremaps/pptk) version 0.1.0 or newer
- [Open3D](http://www.open3d.org/) version 0.12.0 or newer
- [numpy](https://numpy.org/) version 1.19.2 or newer
- [matplotlib](https://matplotlib.org/) version 3.3.2 or newer
- **file_formats**
Expand Down Expand Up @@ -71,7 +71,7 @@ There are two main categories of samples: **camera** and **applications**. The s
- [**downsample**][downsample-url] - Downsample point cloud from a ZDF file.
- **Dependencies:**
- [numpy](https://numpy.org/) version 1.19.2 or newer
- [pptk](https://github.com/heremaps/pptk) version 0.1.0 or newer
- [Open3D](http://www.open3d.org/) version 0.12.0 or newer
- [**create_depth_map**][create_depth_map-url] - Read point cloud data from a ZDF file, convert it to OpenCV format, then extract and visualize depth map.
- **Dependencies:**
- [numpy](https://numpy.org/) version 1.19.2 or newer
Expand All @@ -80,7 +80,7 @@ There are two main categories of samples: **camera** and **applications**. The s
- **Dependencies:**
- [numpy](https://numpy.org/) version 1.19.2 or newer
- [matplotlib](https://matplotlib.org/) version 3.3.2 or newer
- [pptk](https://github.com/heremaps/pptk) version 0.1.0 or newer
- [Open3D](http://www.open3d.org/) version 0.12.0 or newer
- [**gamma_correction**][gamma_correction-url] - Capture 2D image with gamma correction.
- **Dependencies:**
- [numpy](https://numpy.org/) version 1.19.2 or newer
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ matplotlib
vtk
vtk_visualizer
opencv-python
pptk
open3d
pyyaml
scipy
65 changes: 25 additions & 40 deletions source/applications/advanced/downsample.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,41 @@
The ZDF files for this sample can be found under the main instructions for Zivid samples.
"""

import math
from pathlib import Path
import numpy as np
import pptk
import open3d as o3d
import zivid
from sample_utils.paths import get_sample_data_path


def _get_mid_point(xyz):
"""Calculate mid point from average of the 100 centermost points.
def _display_pointcloud(xyz, rgb):
"""Display point cloud.
Args:
rgb: RGB image
xyz: X, Y and Z images (point cloud co-ordinates)
Returns:
mid_point: Calculated mid point
Returns None
"""
offset = 5
xyz_center_cube = xyz[
int(xyz.shape[0] / 2 - offset) : int(xyz.shape[0] / 2 + offset),
int(xyz.shape[1] / 2 - offset) : int(xyz.shape[1] / 2 + offset),
:,
]
return (
np.nanmedian(xyz_center_cube[:, :, 0]),
np.nanmedian(xyz_center_cube[:, :, 1]),
np.nanmedian(xyz_center_cube[:, :, 2]),
)
xyz = np.nan_to_num(xyz).reshape(-1, 3)
rgb = rgb.reshape(-1, 3)

point_cloud_open3d = o3d.geometry.PointCloud(o3d.utility.Vector3dVector(xyz))
point_cloud_open3d.colors = o3d.utility.Vector3dVector(rgb / 255)

def _visualize_point_cloud(point_cloud):
"""Visualize point cloud.
visualizer = o3d.visualization.Visualizer() # pylint: disable=no-member
visualizer.create_window()
visualizer.add_geometry(point_cloud_open3d)

Visualize the provided point cloud `xyz`, and color it with `rgb`.
visualizer.get_render_option().background_color = (0, 0, 0)
visualizer.get_render_option().point_size = 1
visualizer.get_render_option().show_coordinate_frame = True
visualizer.get_view_control().set_front([0, 0, -1])
visualizer.get_view_control().set_up([0, -1, 0])

We take the centermost co-ordinate as 'lookat' point. We assume that camera location is at azimuth -pi/2 and
elevation -pi/2 relative to the 'lookat' point.
Args:
point_cloud: Zivid point cloud
"""
xyz = point_cloud.copy_data("xyz")
rgb = point_cloud.copy_data("rgba")[:, :, 0:3]

mid_point = _get_mid_point(xyz)

# Setting nans to zeros
xyz[np.isnan(xyz[:, :, 2])] = 0

viewer = pptk.viewer(xyz)
viewer.attributes(rgb.reshape(-1, 3) / 255.0)
viewer.set(lookat=mid_point)
viewer.set(phi=-math.pi / 2, theta=-math.pi / 2, r=mid_point[2])
visualizer.run()
visualizer.destroy_window()


def _main():
Expand All @@ -69,17 +50,21 @@ def _main():
frame = zivid.Frame(data_file)

point_cloud = frame.point_cloud()
xyz = point_cloud.copy_data("xyz")
rgba = point_cloud.copy_data("rgba")

print(f"Before downsampling: {point_cloud.width * point_cloud.height} point cloud")

_visualize_point_cloud(point_cloud)
_display_pointcloud(xyz, rgba[:, :, 0:3])

print("Downsampling point cloud")
point_cloud.downsample(zivid.PointCloud.Downsampling.by2x2)
xyz_donwsampled = point_cloud.copy_data("xyz")
rgba_downsampled = point_cloud.copy_data("rgba")

print(f"After downsampling: {point_cloud.width * point_cloud.height} point cloud")

_visualize_point_cloud(point_cloud)
_display_pointcloud(xyz_donwsampled, rgba_downsampled[:, :, 0:3])

input("Press Enter to close...")

Expand Down
61 changes: 21 additions & 40 deletions source/applications/advanced/mask_point_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
The ZDF file for this sample can be found under the main instructions for Zivid samples.
"""

import math
from pathlib import Path
import numpy as np
import matplotlib.pyplot as plt
import pptk
import open3d as o3d
import zivid

from sample_utils.paths import get_sample_data_path
Expand Down Expand Up @@ -51,52 +50,34 @@ def _display_depthmap(xyz):
plt.show(block=False)


def _get_mid_point(xyz):
"""Calculate mid point from average of the 100 centermost points.
def _display_pointcloud(xyz, rgb):
"""Display point cloud provided from 'xyz' with colors from 'rgb'.
Args:
rgb: RGB image
xyz: X, Y and Z images (point cloud co-ordinates)
Returns:
mid_point: Calculated mid point
Returns None
"""
offset = 5
xyz_center_cube = xyz[
int(xyz.shape[0] / 2 - offset) : int(xyz.shape[0] / 2 + offset),
int(xyz.shape[1] / 2 - offset) : int(xyz.shape[1] / 2 + offset),
:,
]
return (
np.nanmedian(xyz_center_cube[:, :, 0]),
np.nanmedian(xyz_center_cube[:, :, 1]),
np.nanmedian(xyz_center_cube[:, :, 2]),
)
xyz = np.nan_to_num(xyz).reshape(-1, 3)
rgb = rgb.reshape(-1, 3)

point_cloud_open3d = o3d.geometry.PointCloud(o3d.utility.Vector3dVector(xyz))
point_cloud_open3d.colors = o3d.utility.Vector3dVector(rgb / 255)

def _display_pointcloud(rgb, xyz):
"""Display point cloud.
visualizer = o3d.visualization.Visualizer() # pylint: disable=no-member
visualizer.create_window()
visualizer.add_geometry(point_cloud_open3d)

Display the provided point cloud `xyz`, and color it with `rgb`.
visualizer.get_render_option().background_color = (0, 0, 0)
visualizer.get_render_option().point_size = 1
visualizer.get_render_option().show_coordinate_frame = True
visualizer.get_view_control().set_front([0, 0, -1])
visualizer.get_view_control().set_up([0, -1, 0])

We take the centermost co-ordinate as 'lookat' point. We assume that
camera location is at azimuth -pi/2 and elevation -pi/2 relative to
the 'lookat' point.
Args:
rgb: RGB image
xyz: X, Y and Z images (point cloud co-ordinates)
Returns None
"""
mid_point = _get_mid_point(xyz)
point_cloud_to_view = xyz
point_cloud_to_view[np.isnan(xyz[:, :, 2])] = 0
viewer = pptk.viewer(point_cloud_to_view)
viewer.attributes(rgb.reshape(-1, 3) / 255.0)
viewer.set(lookat=mid_point)
viewer.set(phi=-math.pi / 2, theta=-math.pi / 2, r=mid_point[2])
visualizer.run()
visualizer.destroy_window()


def _main():
Expand Down Expand Up @@ -125,15 +106,15 @@ def _main():
_display_rgb(rgba[:, :, 0:3], "RGB image")

_display_depthmap(xyz)
_display_pointcloud(rgba[:, :, 0:3], xyz)
_display_pointcloud(xyz, rgba[:, :, 0:3])
input("Press Enter to continue...")

print("Masking point cloud")
xyz_masked = xyz.copy()
xyz_masked[mask == 0] = np.nan

_display_depthmap(xyz_masked)
_display_pointcloud(rgba[:, :, 0:3], xyz_masked)
_display_pointcloud(xyz_masked, rgba[:, :, 0:3])
input("Press Enter to close...")


Expand Down
58 changes: 20 additions & 38 deletions source/applications/basic/visualization/read_zdf_vis_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
The ZDF file for this sample can be found under the main instructions for Zivid samples.
"""

import math
from pathlib import Path
import numpy as np
import matplotlib.pyplot as plt
import pptk
import zivid
import open3d as o3d

from sample_utils.paths import get_sample_data_path

Expand Down Expand Up @@ -50,51 +49,34 @@ def _display_depthmap(xyz):
plt.show(block=False)


def _get_mid_point(xyz):
"""Calculate mid point from average of the 100 centermost points.
def _display_pointcloud(xyz, rgb):
"""Display point cloud provided from 'xyz' with colors from 'rgb'.
Args:
rgb: RGB image
xyz: X, Y and Z images (point cloud co-ordinates)
Returns:
mid_point: Calculated mid point
Returns None
"""
xyz_center_cube = xyz[
int(xyz.shape[0] / 2 - 5) : int(xyz.shape[0] / 2 + 5),
int(xyz.shape[1] / 2 - 5) : int(xyz.shape[1] / 2 + 5),
:,
]
return (
np.nanmedian(xyz_center_cube[:, :, 0]),
np.nanmedian(xyz_center_cube[:, :, 1]),
np.nanmedian(xyz_center_cube[:, :, 2]),
)
xyz = np.nan_to_num(xyz).reshape(-1, 3)
rgb = rgb.reshape(-1, 3)

point_cloud_open3d = o3d.geometry.PointCloud(o3d.utility.Vector3dVector(xyz))
point_cloud_open3d.colors = o3d.utility.Vector3dVector(rgb / 255)

def _display_pointcloud(rgb, xyz):
"""Display point cloud.
visualizer = o3d.visualization.Visualizer() # pylint: disable=no-member
visualizer.create_window()
visualizer.add_geometry(point_cloud_open3d)

Display the provided point cloud `xyz`, and color it with `rgb`.
visualizer.get_render_option().background_color = (0, 0, 0)
visualizer.get_render_option().point_size = 1
visualizer.get_render_option().show_coordinate_frame = True
visualizer.get_view_control().set_front([0, 0, -1])
visualizer.get_view_control().set_up([0, -1, 0])

We take the centermost co-ordinate as 'lookat' point. We assume that
camera location is at azimuth -pi/2 and elevation -pi/2 relative to
the 'lookat' point.
Args:
rgb: RGB image
xyz: X, Y and Z images (point cloud co-ordinates)
Returns None
"""
mid_point = _get_mid_point(xyz)
point_cloud_to_view = xyz
point_cloud_to_view[np.isnan(xyz[:, :, 2])] = 0
viewer = pptk.viewer(point_cloud_to_view)
viewer.attributes(rgb.reshape(-1, 3) / 255.0)
viewer.set(lookat=mid_point)
viewer.set(phi=-math.pi / 2, theta=-math.pi / 2, r=mid_point[2])
visualizer.run()
visualizer.destroy_window()


def _main():
Expand All @@ -114,7 +96,7 @@ def _main():

_display_depthmap(xyz)

_display_pointcloud(rgba[:, :, 0:3], xyz)
_display_pointcloud(xyz, rgba[:, :, 0:3])

input("Press Enter to close...")

Expand Down

0 comments on commit ccfa83a

Please sign in to comment.