目录
1. open3d.geometry.Image转numpy
2. numpy 转 open3d.geometry.Image
1. open3d.geometry.Image转numpy
np_x = np.asarray(x) # (h,w,3)
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import open3d as o3d
print("Convert an image to numpy")
sample_image = o3d.data.JuneauImage()
x = o3d.io.read_image(sample_image.path) # open3d.geometry.Image
np_x = np.asarray(x) # (h,w,3)
print(np.asarray(x)) # open3d.geometry.Image 转 numpy
2. numpy 转 open3d.geometry.Image
(1)yy = o3d.geometry.Image(y) # numpy 转 open3d.geometry.Image
print(
"Convert a numpy image to o3d.geometry.Image and show it with DrawGeomtries()."
)
y = mpimg.imread(sample_image.path) # numpy. (h,w,3)
print(y.shape)
# 注意,这里颜色通道会反转。会将bgr转成rgb显示
yy = o3d.geometry.Image(y) # numpy 转 open3d.geometry.Image
print(yy)
o3d.visualization.draw_geometries([yy])
(2)单通道图numpy转open3d.geometry.Image
yy = o3d.geometry.Image(y) # numpy 转 open3d.geometry.Image
print("Render a channel of the previous image.")
z = np.array(y[:, :, 1]) # (h,w,3)->(h,w)
print(z.shape)
print(z.strides)
zz = o3d.geometry.Image(z) # numpy转open3d.geometry.Image
print(zz)
o3d.visualization.draw_geometries([zz])
3. numpy转PointCloud
point_cloud_with_numpy.py
pcd.points = o3d.utility.Vector3dVector(xyz)
import open3d as o3d
import numpy as np
if __name__ == "__main__":
# 1. generate array (201,201)
# Generate some n x 3 matrix using a variant of sync function.
x = np.linspace(-3, 3, 201) # (-3,3) 201等分。生成array(201,)
# mesh_x: (201,201). 每一行都是(-3,3)201等分; mesh_y: (201,201). 每一列都是(-3,3)201等分
mesh_x, mesh_y = np.meshgrid(x, x)
z = np.sinc((np.power(mesh_x, 2) + np.power(mesh_y, 2))) # sinc(x) = sin(nx) / nx
z_norm = (z - z.min()) / (z.max() - z.min()) # (201,201)
# 2. 填充array.xyz坐标值
xyz = np.zeros((np.size(mesh_x), 3)) # (201*201, 3)
xyz[:, 0] = np.reshape(mesh_x, -1) # x轴坐标
xyz[:, 1] = np.reshape(mesh_y, -1) # y轴坐标
xyz[:, 2] = np.reshape(z_norm, -1) # z轴坐标
print("Printing numpy array used to make Open3D pointcloud ...")
print(xyz)
# 3. numpy转PointCloud
# Pass xyz to Open3D.o3d.geometry.PointCloud and visualize.
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(xyz)
# 4. view pcd
# Add color and estimate normals for better visualization.
pcd.paint_uniform_color([0.5, 0.5, 0.5])
pcd.estimate_normals() # 计算法线
pcd.orient_normals_consistent_tangent_plane(1)
print("Displaying Open3D pointcloud made using numpy array ...")
o3d.visualization.draw([pcd])
4. PointCloud转numpy
xyz_converted = np.asarray(pcd.points)
# 5. pcd to numpy
# Convert Open3D.o3d.geometry.PointCloud to numpy array.
xyz_converted = np.asarray(pcd.points)
print("Printing numpy array made using Open3D pointcloud ...")
print(xyz_converted)