Point Cloud
point_cloud_bounding_box.py
1# ----------------------------------------------------------------------------
2# - Open3D: www.open3d.org -
3# ----------------------------------------------------------------------------
4# The MIT License (MIT)
5#
6# Copyright (c) 2018-2021 www.open3d.org
7#
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25# ----------------------------------------------------------------------------
26
27import open3d as o3d
28
29if __name__ == "__main__":
30 sample_ply_data = o3d.data.PLYPointCloud()
31 pcd = o3d.io.read_point_cloud(sample_ply_data.path)
32 # Flip it, otherwise the pointcloud will be upside down.
33 pcd.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
34 print(pcd)
35 axis_aligned_bounding_box = pcd.get_axis_aligned_bounding_box()
36 axis_aligned_bounding_box.color = (1, 0, 0)
37 oriented_bounding_box = pcd.get_oriented_bounding_box()
38 oriented_bounding_box.color = (0, 1, 0)
39 print(
40 "Displaying axis_aligned_bounding_box in red and oriented bounding box in green ..."
41 )
42 o3d.visualization.draw(
43 [pcd, axis_aligned_bounding_box, oriented_bounding_box])
point_cloud_convex_hull.py
1# ----------------------------------------------------------------------------
2# - Open3D: www.open3d.org -
3# ----------------------------------------------------------------------------
4# The MIT License (MIT)
5#
6# Copyright (c) 2018-2021 www.open3d.org
7#
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25# ----------------------------------------------------------------------------
26
27import open3d as o3d
28
29if __name__ == "__main__":
30
31 print("Displaying pointcloud with convex hull ...")
32 bunny = o3d.data.BunnyMesh()
33 mesh = o3d.io.read_triangle_mesh(bunny.path)
34 mesh.compute_vertex_normals()
35
36 pcl = mesh.sample_points_poisson_disk(number_of_points=10000)
37 hull, _ = pcl.compute_convex_hull()
38 hull_ls = o3d.geometry.LineSet.create_from_triangle_mesh(hull)
39 hull_ls.paint_uniform_color((1, 0, 0))
40 o3d.visualization.draw([pcl, hull_ls])
point_cloud_crop.py
1# ----------------------------------------------------------------------------
2# - Open3D: www.open3d.org -
3# ----------------------------------------------------------------------------
4# The MIT License (MIT)
5#
6# Copyright (c) 2018-2021 www.open3d.org
7#
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25# ----------------------------------------------------------------------------
26
27import open3d as o3d
28
29if __name__ == "__main__":
30 print("Load a ply point cloud, crop it, and render it")
31 sample_ply_data = o3d.data.DemoCropPointCloud()
32 pcd = o3d.io.read_point_cloud(sample_ply_data.point_cloud_path)
33 vol = o3d.visualization.read_selection_polygon_volume(
34 sample_ply_data.cropped_json_path)
35 chair = vol.crop_point_cloud(pcd)
36 # Flip the pointclouds, otherwise they will be upside down.
37 pcd.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
38 chair.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
39
40 print("Displaying original pointcloud ...")
41 o3d.visualization.draw([pcd])
42 print("Diplaying cropped pointcloud")
43 o3d.visualization.draw([chair])
point_cloud_dbscan_clustering.py
1# ----------------------------------------------------------------------------
2# - Open3D: www.open3d.org -
3# ----------------------------------------------------------------------------
4# The MIT License (MIT)
5#
6# Copyright (c) 2018-2021 www.open3d.org
7#
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25# ----------------------------------------------------------------------------
26
27import open3d as o3d
28import numpy as np
29import matplotlib.pyplot as plt
30
31if __name__ == "__main__":
32 sample_ply_data = o3d.data.PLYPointCloud()
33 pcd = o3d.io.read_point_cloud(sample_ply_data.path)
34 # Flip it, otherwise the pointcloud will be upside down.
35 pcd.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
36
37 with o3d.utility.VerbosityContextManager(
38 o3d.utility.VerbosityLevel.Debug) as cm:
39 labels = np.array(
40 pcd.cluster_dbscan(eps=0.02, min_points=10, print_progress=True))
41
42 max_label = labels.max()
43 print(f"point cloud has {max_label + 1} clusters")
44 colors = plt.get_cmap("tab20")(labels / (max_label if max_label > 0 else 1))
45 colors[labels < 0] = 0
46 pcd.colors = o3d.utility.Vector3dVector(colors[:, :3])
47 o3d.visualization.draw([pcd])
point_cloud_distance.py
1# ----------------------------------------------------------------------------
2# - Open3D: www.open3d.org -
3# ----------------------------------------------------------------------------
4# The MIT License (MIT)
5#
6# Copyright (c) 2018-2021 www.open3d.org
7#
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25# ----------------------------------------------------------------------------
26
27import open3d as o3d
28import numpy as np
29
30if __name__ == "__main__":
31 sample_ply_data = o3d.data.DemoCropPointCloud()
32 pcd = o3d.io.read_point_cloud(sample_ply_data.point_cloud_path)
33 vol = o3d.visualization.read_selection_polygon_volume(
34 sample_ply_data.cropped_json_path)
35 chair = vol.crop_point_cloud(pcd)
36
37 chair.paint_uniform_color([0, 0, 1])
38 pcd.paint_uniform_color([1, 0, 0])
39 print("Displaying the two point clouds used for calculating distance ...")
40 o3d.visualization.draw([pcd, chair])
41
42 dists = pcd.compute_point_cloud_distance(chair)
43 dists = np.asarray(dists)
44 print("Printing average distance between the two point clouds ...")
45 print(dists)
point_cloud_iss_keypoint_detector.py
1# ----------------------------------------------------------------------------
2# - Open3D: www.open3d.org -
3# ----------------------------------------------------------------------------
4# The MIT License (MIT)
5#
6# Copyright (c) 2018-2021 www.open3d.org
7#
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25# ----------------------------------------------------------------------------
26
27import open3d as o3d
28import time
29
30if __name__ == "__main__":
31 # Compute ISS Keypoints on armadillo pointcloud.
32 armadillo_data = o3d.data.ArmadilloMesh()
33 mesh = o3d.io.read_triangle_mesh(armadillo_data.path)
34 pcd = o3d.geometry.PointCloud()
35 pcd.points = mesh.vertices
36
37 tic = time.time()
38 keypoints = o3d.geometry.keypoint.compute_iss_keypoints(pcd)
39 toc = 1000 * (time.time() - tic)
40 print("ISS Computation took {:.0f} [ms]".format(toc))
41
42 mesh.compute_vertex_normals()
43 mesh.paint_uniform_color([0.5, 0.5, 0.5])
44 keypoints.paint_uniform_color([1.0, 0.0, 0.0])
45 o3d.visualization.draw([keypoints, mesh], point_size=5)
point_cloud_normal_estimation.py
1# ----------------------------------------------------------------------------
2# - Open3D: www.open3d.org -
3# ----------------------------------------------------------------------------
4# The MIT License (MIT)
5#
6# Copyright (c) 2018-2021 www.open3d.org
7#
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25# ----------------------------------------------------------------------------
26
27import open3d as o3d
28import numpy as np
29
30if __name__ == "__main__":
31 bunny = o3d.data.BunnyMesh()
32 gt_mesh = o3d.io.read_triangle_mesh(bunny.path)
33 gt_mesh.compute_vertex_normals()
34
35 pcd = gt_mesh.sample_points_poisson_disk(5000)
36 # Invalidate existing normals.
37 pcd.normals = o3d.utility.Vector3dVector(np.zeros((1, 3)))
38
39 print("Displaying input pointcloud ...")
40 o3d.visualization.draw_geometries([pcd], point_show_normal=True)
41
42 pcd.estimate_normals()
43 print("Displaying pointcloud with normals ...")
44 o3d.visualization.draw_geometries([pcd], point_show_normal=True)
45
46 print("Printing the normal vectors ...")
47 print(np.asarray(pcd.normals))
point_cloud_outlier_removal_radius.py
1# ----------------------------------------------------------------------------
2# - Open3D: www.open3d.org -
3# ----------------------------------------------------------------------------
4# The MIT License (MIT)
5#
6# Copyright (c) 2018-2021 www.open3d.org
7#
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25# ----------------------------------------------------------------------------
26
27import open3d as o3d
28import numpy as np
29
30
31def display_inlier_outlier(cloud, ind):
32 inlier_cloud = cloud.select_by_index(ind)
33 outlier_cloud = cloud.select_by_index(ind, invert=True)
34
35 print("Showing outliers (red) and inliers (gray): ")
36 outlier_cloud.paint_uniform_color([1, 0, 0])
37 inlier_cloud.paint_uniform_color([0.8, 0.8, 0.8])
38 o3d.visualization.draw([inlier_cloud, outlier_cloud])
39
40
41if __name__ == "__main__":
42 ptcloud_data = o3d.data.PLYPointCloud()
43
44 print("Load a ply point cloud, print it, and render it")
45 pcd = o3d.io.read_point_cloud(ptcloud_data.path)
46 R = pcd.get_rotation_matrix_from_xyz((np.pi, 0, 0))
47 pcd.rotate(R, center=(0, 0, 0))
48 o3d.visualization.draw([pcd])
49
50 print("Downsample the point cloud with a voxel of 0.02")
51 voxel_down_pcd = pcd.voxel_down_sample(voxel_size=0.02)
52 o3d.visualization.draw([voxel_down_pcd])
53
54 print("Radius oulier removal")
55 cl, ind = voxel_down_pcd.remove_radius_outlier(nb_points=16, radius=0.05)
56 display_inlier_outlier(voxel_down_pcd, ind)
point_cloud_outlier_removal_statistical.py
1# ----------------------------------------------------------------------------
2# - Open3D: www.open3d.org -
3# ----------------------------------------------------------------------------
4# The MIT License (MIT)
5#
6# Copyright (c) 2018-2021 www.open3d.org
7#
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25# ----------------------------------------------------------------------------
26
27import open3d as o3d
28import numpy as np
29
30
31def display_inlier_outlier(cloud, ind):
32 inlier_cloud = cloud.select_by_index(ind)
33 outlier_cloud = cloud.select_by_index(ind, invert=True)
34
35 print("Showing outliers (red) and inliers (gray): ")
36 outlier_cloud.paint_uniform_color([1, 0, 0])
37 inlier_cloud.paint_uniform_color([0.8, 0.8, 0.8])
38 o3d.visualization.draw([inlier_cloud, outlier_cloud])
39
40
41if __name__ == "__main__":
42 ptcloud_data = o3d.data.PLYPointCloud()
43
44 print("Load a ply point cloud, print it, and render it")
45 pcd = o3d.io.read_point_cloud(ptcloud_data.path)
46 R = pcd.get_rotation_matrix_from_xyz((np.pi, 0, 0))
47 pcd.rotate(R, center=(0, 0, 0))
48 o3d.visualization.draw([pcd])
49
50 print("Downsample the point cloud with a voxel of 0.02")
51 voxel_down_pcd = pcd.voxel_down_sample(voxel_size=0.02)
52 o3d.visualization.draw([voxel_down_pcd])
53
54 print("Statistical oulier removal")
55 cl, ind = voxel_down_pcd.remove_statistical_outlier(nb_neighbors=20,
56 std_ratio=2.0)
57 display_inlier_outlier(voxel_down_pcd, ind)
point_cloud_paint.py
1# ----------------------------------------------------------------------------
2# - Open3D: www.open3d.org -
3# ----------------------------------------------------------------------------
4# The MIT License (MIT)
5#
6# Copyright (c) 2018-2021 www.open3d.org
7#
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25# ----------------------------------------------------------------------------
26
27import open3d as o3d
28
29if __name__ == "__main__":
30 print("Load a ply point cloud, print it, and render it")
31 sample_ply_data = o3d.data.PLYPointCloud()
32 pcd = o3d.io.read_point_cloud(sample_ply_data.path)
33 # Flip it, otherwise the pointcloud will be upside down.
34 pcd.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
35 print(pcd)
36 o3d.visualization.draw([pcd])
37 print("Paint pointcloud")
38 pcd.paint_uniform_color([1, 0.706, 0])
39 o3d.visualization.draw([pcd])
point_cloud_plane_segmentation.py
1# ----------------------------------------------------------------------------
2# - Open3D: www.open3d.org -
3# ----------------------------------------------------------------------------
4# The MIT License (MIT)
5#
6# Copyright (c) 2018-2021 www.open3d.org
7#
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25# ----------------------------------------------------------------------------
26
27import open3d as o3d
28
29if __name__ == "__main__":
30
31 sample_pcd_data = o3d.data.PCDPointCloud()
32 pcd = o3d.io.read_point_cloud(sample_pcd_data.path)
33 # Flip it, otherwise the pointcloud will be upside down.
34 pcd.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
35 plane_model, inliers = pcd.segment_plane(distance_threshold=0.01,
36 ransac_n=3,
37 num_iterations=1000)
38 [a, b, c, d] = plane_model
39 print(f"Plane equation: {a:.2f}x + {b:.2f}y + {c:.2f}z + {d:.2f} = 0")
40 print("Displaying pointcloud with planar points in red ...")
41 inlier_cloud = pcd.select_by_index(inliers)
42 inlier_cloud.paint_uniform_color([1.0, 0, 0])
43 outlier_cloud = pcd.select_by_index(inliers, invert=True)
44 o3d.visualization.draw([inlier_cloud, outlier_cloud])
point_cloud_to_depth.py
1# ----------------------------------------------------------------------------
2# - Open3D: www.open3d.org -
3# ----------------------------------------------------------------------------
4# The MIT License (MIT)
5#
6# Copyright (c) 2018-2021 www.open3d.org
7#
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25# ----------------------------------------------------------------------------
26
27import open3d as o3d
28import numpy as np
29import matplotlib.pyplot as plt
30
31if __name__ == '__main__':
32 tum_data = o3d.data.SampleTUMRGBDImage()
33 depth = o3d.t.io.read_image(tum_data.depth_path)
34 intrinsic = o3d.core.Tensor([[535.4, 0, 320.1], [0, 539.2, 247.6],
35 [0, 0, 1]])
36
37 pcd = o3d.t.geometry.PointCloud.create_from_depth_image(depth,
38 intrinsic,
39 depth_scale=5000.0,
40 depth_max=10.0)
41 o3d.visualization.draw([pcd])
42 depth_reproj = pcd.project_to_depth_image(640,
43 480,
44 intrinsic,
45 depth_scale=5000.0,
46 depth_max=10.0)
47
48 fig, axs = plt.subplots(1, 2)
49 axs[0].imshow(np.asarray(depth.to_legacy()))
50 axs[1].imshow(np.asarray(depth_reproj.to_legacy()))
51 plt.show()
point_cloud_to_rgbd.py
1# ----------------------------------------------------------------------------
2# - Open3D: www.open3d.org -
3# ----------------------------------------------------------------------------
4# The MIT License (MIT)
5#
6# Copyright (c) 2018-2021 www.open3d.org
7#
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25# ----------------------------------------------------------------------------
26
27import open3d as o3d
28import numpy as np
29import matplotlib.pyplot as plt
30
31if __name__ == '__main__':
32 device = o3d.core.Device('CPU:0')
33 tum_data = o3d.data.SampleTUMRGBDImage()
34 depth = o3d.t.io.read_image(tum_data.depth_path).to(device)
35 color = o3d.t.io.read_image(tum_data.color_path).to(device)
36
37 intrinsic = o3d.core.Tensor([[535.4, 0, 320.1], [0, 539.2, 247.6],
38 [0, 0, 1]])
39 rgbd = o3d.t.geometry.RGBDImage(color, depth)
40
41 pcd = o3d.t.geometry.PointCloud.create_from_rgbd_image(rgbd,
42 intrinsic,
43 depth_scale=5000.0,
44 depth_max=10.0)
45 o3d.visualization.draw([pcd])
46 rgbd_reproj = pcd.project_to_rgbd_image(640,
47 480,
48 intrinsic,
49 depth_scale=5000.0,
50 depth_max=10.0)
51
52 fig, axs = plt.subplots(1, 2)
53 axs[0].imshow(np.asarray(rgbd_reproj.color.to_legacy()))
54 axs[1].imshow(np.asarray(rgbd_reproj.depth.to_legacy()))
55 plt.show()
point_cloud_transformation.py
1# ----------------------------------------------------------------------------
2# - Open3D: www.open3d.org -
3# ----------------------------------------------------------------------------
4# The MIT License (MIT)
5#
6# Copyright (c) 2018-2021 www.open3d.org
7#
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25# ----------------------------------------------------------------------------
26
27import open3d as o3d
28import numpy as np
29import copy
30
31
32def translate():
33 armadillo_data = o3d.data.ArmadilloMesh()
34 pcd = o3d.io.read_triangle_mesh(
35 armadillo_data.path).sample_points_poisson_disk(5000)
36 pcd_tx = copy.deepcopy(pcd).translate((150, 0, 0))
37 pcd_ty = copy.deepcopy(pcd).translate((0, 200, 0))
38 print('Displaying original and translated geometries ...')
39 o3d.visualization.draw([{
40 "name": "Original Geometry",
41 "geometry": pcd
42 }, {
43 "name": "Translated (in X) Geometry",
44 "geometry": pcd_tx
45 }, {
46 "name": "Translated (in Y) Geometry",
47 "geometry": pcd_ty
48 }],
49 show_ui=True)
50
51
52def rotate():
53 armadillo_data = o3d.data.ArmadilloMesh()
54 pcd = o3d.io.read_triangle_mesh(
55 armadillo_data.path).sample_points_poisson_disk(5000)
56 pcd_r = copy.deepcopy(pcd).translate((200, 0, 0))
57 R = pcd.get_rotation_matrix_from_xyz((np.pi / 2, 0, np.pi / 4))
58 pcd_r.rotate(R)
59 print('Displaying original and rotated geometries ...')
60 o3d.visualization.draw([{
61 "name": "Original Geometry",
62 "geometry": pcd
63 }, {
64 "name": "Rotated Geometry",
65 "geometry": pcd_r
66 }],
67 show_ui=True)
68
69
70def scale():
71 armadillo_data = o3d.data.ArmadilloMesh()
72 pcd = o3d.io.read_triangle_mesh(
73 armadillo_data.path).sample_points_poisson_disk(5000)
74 pcd_s = copy.deepcopy(pcd).translate((200, 0, 0))
75 pcd_s.scale(0.5, center=pcd_s.get_center())
76 print('Displaying original and scaled geometries ...')
77 o3d.visualization.draw([{
78 "name": "Original Geometry",
79 "geometry": pcd
80 }, {
81 "name": "Scaled Geometry",
82 "geometry": pcd_s
83 }],
84 show_ui=True)
85
86
87def transform():
88 armadillo_data = o3d.data.ArmadilloMesh()
89 pcd = o3d.io.read_triangle_mesh(
90 armadillo_data.path).sample_points_poisson_disk(5000)
91 T = np.eye(4)
92 T[:3, :3] = pcd.get_rotation_matrix_from_xyz((0, np.pi / 3, np.pi / 2))
93 T[0, 3] = 150
94 T[1, 3] = 200
95 print(T)
96 pcd_t = copy.deepcopy(pcd).transform(T)
97 print('Displaying original and transformed geometries ...')
98 o3d.visualization.draw([{
99 "name": "Original Geometry",
100 "geometry": pcd
101 }, {
102 "name": "Transformed Geometry",
103 "geometry": pcd_t
104 }],
105 show_ui=True)
106
107
108if __name__ == "__main__":
109
110 translate()
111 rotate()
112 scale()
113 transform()
point_cloud_voxel_downsampling.py
1# ----------------------------------------------------------------------------
2# - Open3D: www.open3d.org -
3# ----------------------------------------------------------------------------
4# The MIT License (MIT)
5#
6# Copyright (c) 2018-2021 www.open3d.org
7#
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25# ----------------------------------------------------------------------------
26
27import open3d as o3d
28
29if __name__ == "__main__":
30 print("Load a ply point cloud, print it, and render it")
31 sample_ply_data = o3d.data.PLYPointCloud()
32 pcd = o3d.io.read_point_cloud(sample_ply_data.path)
33 # Flip it, otherwise the pointcloud will be upside down.
34 pcd.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
35 print(pcd)
36 o3d.visualization.draw([pcd])
37 print("Downsample the point cloud with a voxel of 0.05")
38 downpcd = pcd.voxel_down_sample(voxel_size=0.05)
39 o3d.visualization.draw([downpcd])
point_cloud_with_numpy.py
1# ----------------------------------------------------------------------------
2# - Open3D: www.open3d.org -
3# ----------------------------------------------------------------------------
4# The MIT License (MIT)
5#
6# Copyright (c) 2018-2021 www.open3d.org
7#
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25# ----------------------------------------------------------------------------
26
27import open3d as o3d
28import numpy as np
29
30if __name__ == "__main__":
31 # Generate some n x 3 matrix using a variant of sync function.
32 x = np.linspace(-3, 3, 201)
33 mesh_x, mesh_y = np.meshgrid(x, x)
34 z = np.sinc((np.power(mesh_x, 2) + np.power(mesh_y, 2)))
35 z_norm = (z - z.min()) / (z.max() - z.min())
36 xyz = np.zeros((np.size(mesh_x), 3))
37 xyz[:, 0] = np.reshape(mesh_x, -1)
38 xyz[:, 1] = np.reshape(mesh_y, -1)
39 xyz[:, 2] = np.reshape(z_norm, -1)
40 print("Printing numpy array used to make Open3D pointcloud ...")
41 print(xyz)
42
43 # Pass xyz to Open3D.o3d.geometry.PointCloud and visualize.
44 pcd = o3d.geometry.PointCloud()
45 pcd.points = o3d.utility.Vector3dVector(xyz)
46 # Add color and estimate normals for better visualization.
47 pcd.paint_uniform_color([0.5, 0.5, 0.5])
48 pcd.estimate_normals()
49 pcd.orient_normals_consistent_tangent_plane(1)
50 print("Displaying Open3D pointcloud made using numpy array ...")
51 o3d.visualization.draw([pcd])
52
53 # Convert Open3D.o3d.geometry.PointCloud to numpy array.
54 xyz_converted = np.asarray(pcd.points)
55 print("Printing numpy array made using Open3D pointcloud ...")
56 print(xyz_converted)