Skip to content

Instantly share code, notes, and snippets.

@prerakmody
Last active June 23, 2020 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prerakmody/2acccd5f0c4af63cfd40cdbd81a94ea0 to your computer and use it in GitHub Desktop.
Save prerakmody/2acccd5f0c4af63cfd40cdbd81a94ea0 to your computer and use it in GitHub Desktop.
Open3D
import numpy as np
import open3d as o3d
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(np.random.random(10,10,10))
o3d.io.write_point_cloud("mypcd.pcd", pcd)
pcd_new, _ = pcd.remove_statistical_outlier(nb_neighbors=5,std_ratio=2.0)
import numpy as np
import open3d as o3d
path_view_write = ''
vis_visible=False
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(np.random.random(10,10,10))
vis = o3d.visualization.Visualizer()
vis.create_window(window_name='3D', visible=vis_visible)
vis.add_geometry(voxel)
rot_mat = get_rot_matrix( euler=[0,0,0], camera_origin=[0,0,2])
cam = vis.get_view_control().convert_to_pinhole_camera_parameters()
cam.extrinsic = rot_mat
vis.get_view_control().convert_from_pinhole_camera_parameters(cam)
vis.update_geometry()
vis.poll_events()
vis.update_renderer()
vis.capture_screen_image(str(path_view_write), 0)
if vis_visible is True:
vis.run()
def get_rot_matrix(self, euler=[0,0,0], camera_origin=[0,0,2]):
matrix_trans = np.eye(4)
matrix_rot = np.eye(4)
matrix_trans[0:3:,3] = -1*np.array([camera_origin[0], camera_origin[1], camera_origin[2]])
matrix_rot[:3, :3] = scipy_rotation.from_euler('xyz', euler, degrees=True).as_dcm()
return matrix_rot.dot(matrix_trans)
def rotation_matrix(self, angle, direction, point=None):
sina = math.sin(angle)
cosa = math.cos(angle)
direction = (direction[:3])
# rotation matrix around unit vector
R = np.diag([cosa, cosa, cosa])
R += np.outer(direction, direction) * (1.0 - cosa)
direction *= sina
R += np.array([[0.0, -direction[2], direction[1]],
[direction[2], 0.0, -direction[0]],
[-direction[1], direction[0], 0.0]])
M = np.identity(4)
M[:3, :3] = R
if point is not None:
# rotation not around origin
point = np.array(point[:3], dtype=np.float64, copy=False)
M[:3, 3] = point - np.dot(R, point)
return M
def get_euler_angles_from_rotation_matrix(R, in_degrees=False, roundBool=False):
'''
From a paper by Gregory G. Slabaugh (undated),
"Computing Euler angles from a rotation matrix
'''
import math
def isclose(x, y, rtol=1.e-5, atol=1.e-8):
return abs(x-y) <= atol + rtol * abs(y)
phi = 0.0
if isclose(R[2,0],-1.0):
theta = math.pi/2.0
psi = math.atan2(R[0,1],R[0,2])
elif isclose(R[2,0],1.0):
theta = -math.pi/2.0
psi = math.atan2(-R[0,1],-R[0,2])
else:
theta = -math.asin(R[2,0])
cos_theta = math.cos(theta)
psi = math.atan2(R[2,1]/cos_theta, R[2,2]/cos_theta)
phi = math.atan2(R[1,0]/cos_theta, R[0,0]/cos_theta)
if in_degrees == True:
if roundBool is True:
return round(math.degrees(psi),4), round(math.degrees(theta),4), round(math.degrees(phi),4) #[theta_x, theta_y, theta_z]
else:
return math.degrees(psi), math.degrees(theta), math.degrees(phi) #[theta_x, theta_y, theta_z]
else:
return psi, theta, phi
import pandas as pd
from pyntcloud import PyntCloud
# Step 1 - Create a PyntCloud and voxelize
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(np.random.random(10,10,10))
pcd_pts = np.asarray(pcd.points)
cloud = PyntCloud(pd.DataFrame(pcd_pts, columns=['x','y','z']))
voxelgrid_id = cloud.add_structure("voxelgrid", n_x=, n_y=, n_z=) # will create cubes i.e. fixed side length
voxelgrid_id = cloud.add_structure("voxelgrid", size_x=, size_y=, size_z=)
voxelgrid = cloud.structures[voxelgrid_id]
x_cords = voxelgrid.voxel_x
y_cords = voxelgrid.voxel_y
z_cords = voxelgrid.voxel_z
voxel_pts = np.hstack((x_cords_col, y_cords_col, z_cords_col))
voxel = o3d.geometry.PointCloud()
voxel.points = o3d.utility.Vector3dVector(voxel_pts/100.0) # Just for visulization purposes
# Step 2 - Visualize
mesh_axes = o3d.geometry.TriangleMesh.create_coordinate_frame(size=0.5, origin=[0,0,0])
voxel_bbox = voxel.get_axis_aligned_bounding_box()
vis = o3d.visualization.Visualizer()
vis.create_window(window_name='3D', visible=vis_visible)
vis.add_geometry(mesh_axes)
vis.add_geometry(voxel_bbox)
vis.add_geometry(voxel)
vis.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment