NumPy_数据处理详解—矩阵运算

示例

 根据 出版的图书 <Numpy 数据处理详情>-python机器学习和数据科学中的高性能计算方法

代码示例

#!/usr/bin/python3
# -*- coding: utf-8 -*- 

import pandas as pd
import numpy  as np
import open3d as o3d 

def get_from_eye(rotation,trans):
    ## 单位矩阵-对角线元素为1,非对角线元素全为0的矩的正方矩阵 np.identity
    ## 二维矩阵-对角线元素为1,非对角线元素全为0的矩的N*M矩阵 np.eye 可以移动对角线
    tr_matrix = np.eye(4,dtype=np.float32)
    tr_matrix[0:3,0:3]= rotation
    tr_matrix[:3,3]= trans 
    return tr_matrix   

def get_from_zero(rotation,trans):
    ## 生成全为0数组的函数
    tr_matrix = np.zeros((4,4),dtype=np.float32)
    tr_matrix[0:3,0:3]= rotation
    tr_matrix[:3,3]= trans 
    tr_matrix[3,3]= 1
    return tr_matrix   

def generate_points_matrix(point):
    std_points  = np.zeros((point.shape[0],4),dtype=np.float32)
    std_points[:,:3]= point[:,:3]
    ## 生成全为1数组的函数
    std_points[:,3]= np.ones(point.shape[0])
    return std_points

def generate_points_homogeneous_matrix(point):
 ## 生成全为1数组的函数-点云齐次矩阵-第四列全为1 homogeneous
    std_points  = np.ones((point.shape[0],4),dtype=np.float32)
    std_points[:,:3]= point[:,:3]
    return std_points

def trans_points_matrix(pcd_file,rotation,trans,out_pcd_file):  
    orig_pcd = o3d.t.io.read_point_cloud(pcd_file)  
    points_xyz = orig_pcd.point.numpy
    intensity = orig_pcd["intensity"].numpy
    exp_points = generate_points_matrix(points_xyz)
    exp_tr_matrix = get_from_zero(rotation,trans)
    new_points = np.dot( np.linalg.inv(exp_tr_matrix),exp_points.T).T
    ##输出点云
    out_pcd = o3d.t.geometry.PointCloud()
    out_pcd.point["positions"] = o3d.core.Tensor(new_points[:,:3],dtype=o3d.core.Dtype.Float32)
    out_pcd.point["intensity"] = o3d.core.Tensor(intensity,dtype=o3d.core.Dtype.Float32)
    o3d.t.io.write_point_cloud(out_pcd_file,out_pcd,write_ascii=False,compressed=False) 

if __name__ == '__main__':
    rotation_data = np.array([1.1,3,4,5,6,7,8,9,2])
    rotation_matrix = rotation_data.reshape(3,-1)
    trans_data = np.array([30,20,10])
    
    ##生成连续数列或者等差数列的函数
    point_wxp = np.array(range(60)).reshape((-1,6))
    ## .reshape 和 resize 共享内存方式-报错的处理方式
    point_wxp = np.arange(60,dtype=np.float32).reshape((-1,6))

    #sig_matrix = get_from_eye(rotation_matrix,trans_data)
    sig_matrix = get_from_zero(rotation_matrix,trans_data)
    print(sig_matrix)
    print(np.linalg.inv(sig_matrix))
    exp_point = generate_points_matrix(point_wxp)
    print(exp_point)
posted @ 2023-05-19 17:59  辰令  阅读(18)  评论(0编辑  收藏  举报