open3d Image和numpy互转,PointCloud和numpy互转

目录

1. open3d.geometry.Image转numpy

2. numpy 转 open3d.geometry.Image

3. numpy转PointCloud

4. PointCloud转numpy


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)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.Q

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值