栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 深度图转点云以及可视化

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 深度图转点云以及可视化

python 深度图转点云 并且可视化
# coding=utf-8

import numpy as np
from argparse import Namespace

from PIL import Image
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

camera_matrix = {'xc': 127.5, 'zc': 127.5, 'f': 128}
camera_matrix = Namespace(**camera_matrix)
def get_point_cloud_from_z(Y, camera_matrix, scale=1):
    """Projects the depth image Y into a 3D point cloud.
    Inputs:
        Y is ...xHxW
        camera_matrix
    Outputs:
        X is positive going right
        Y is positive into the image
        Z is positive up in the image
        XYZ is ...xHxWx3
    """

    x, z = np.meshgrid(np.arange(Y.shape[-1]),
                       np.arange(Y.shape[-2] - 1, -1, -1))
    for i in range(Y.ndim - 2):
        x = np.expand_dims(x, axis=0)
        z = np.expand_dims(z, axis=0)
    X = (x[::scale, ::scale] - camera_matrix.xc) * Y[::scale, ::scale] / camera_matrix.f
    Z = (z[::scale, ::scale] - camera_matrix.zc) * Y[::scale, ::scale] / camera_matrix.f
    #

    XYZ = np.concatenate((X[..., np.newaxis], Y[::scale, ::scale][..., np.newaxis]
                          ,Z[..., np.newaxis]), axis=X.ndim)
    return XYZ



image = Image.open("grey.png")
image = image.resize((256,256))
img = np.asarray(image)

XYZ = get_point_cloud_from_z(img,camera_matrix,scale=1)

ax = plt.figure(1).gca(projection='3d')
ax.plot(np.ndarray.flatten(XYZ[::,::,0]),np.ndarray.flatten(XYZ[::,::,1]),np.ndarray.flatten( XYZ[::,::,2]), 'b.', markersize=0.2)

plt.title('point cloud')
plt.show()



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/674742.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号