当前位置:网站首页>法向量点云旋转
法向量点云旋转
2022-07-04 09:32:00 【Coding的叶子】
在点云处理过程中,我们有时需要根据法向量把点云旋转到指定方向。例如,我们需要把激光雷达点云中地面旋转到与xoy平面平行。本节将详细介绍其中原理和python代码。
1 平面方程
平面方程可以用如下公式表示:
则(A, B, C)为平面的一个法向量,推导方式请参考博客:python三维点云投影(一)_Coding的叶子的博客-CSDN博客_点云投影。
2 法向量旋转
如下图所示,假设向量n0是原始法向量,n1是目标向量方向。我们的目标是将n0旋转到n1方向。n0的坐标为(x0, y0, z0),n1的坐标为(x1, y1, z1),原点坐标为O(0, 0, 0)。这三个坐标构成了一个平面,并且旋转轴垂直于该平面,且经过坐标原点,即该平面的法向量。通过这三个点计算出平面方程如下:
即:
则该平面的一个法向量为,也就是旋转轴的向量。
向量n0到向量n1的旋转角度theta为这两个向量之间的夹角,即:
3 open3d点云旋转
点云旋转的方法已在博客:点云旋转平移(三)—python open3d点云旋转_Coding的叶子的博客-CSDN博客_python 点云旋转详细介绍,包括欧拉角旋转、轴角旋转、四元数旋转等。这里采用轴角的方式进行旋转,其中旋转轴向量的模长为旋转角度大小。根据第2节中的旋转轴向量和旋转角度即可得到open3d中所需要的轴向量,计算公式如下:
4 参考代码
点云可视化的方式种类较多,如open3d、mayavi、pcl、matplotib、cloudcompare等,在之前的博客里都有介绍。这里采用matplotlib的方式进行点云可视化。如果还需要将点云进一步平移,可以参考本专栏之前的博客。
# -*- coding: utf-8 -*-
"""
乐乐感知学堂公众号
@author: https://blog.csdn.net/suiyingy
"""
import numpy as np
import open3d as o3d
import matplotlib.pyplot as plt
from copy import deepcopy
def viz_matplot(points):
x = points[:, 0] # x position of point
y = points[:, 1] # y position of point
z = points[:, 2] # z position of point
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, # x
y, # y
z, # z
c=z, # height data for color
cmap='rainbow',
marker=".")
ax.axis()
def pcd_rotate_normal(pointcloud, n0, n1):
"""
Parameters
----------
pointcloud : open3d PointCloud, 输入点云
n0 : array, 1x3, 原始法向量
n1 : array, 1x3, 目标法向量
Returns
-------
pcd : open3d PointCloud, 旋转后点云
"""
pcd = deepcopy(pointcloud)
n0_norm2 = np.sqrt(sum(n0 ** 2))
n1_norm2 = np.sqrt(sum(n1 ** 2))
theta = np.arccos(sum(n0 * n1) / n0_norm2 / n1_norm2)
r_axis = np.array([n1[2]*n0[1]-n0[2]*n1[1], n0[2]*n1[0]-n1[2]*n0[0], n0[0]*n1[1]-n1[0]*n0[1]])
r_axis = r_axis * theta / np.sqrt(sum(r_axis ** 2))
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(p)
R = pcd.get_rotation_matrix_from_axis_angle(r_axis.T)
pcd.rotate(R)
return pcd
if __name__ == '__main__':
#生成平面点云
x = np.arange(301).reshape(-1, 1).repeat(100, 0) / 100.
y = np.arange(301).reshape(-1, 1).repeat(100, 1).T.reshape(-1, 1) / 100.
#平面方程
z = (12 - 4*x -4*y) / 3
p = np.concatenate((x, y, z), 1)
p = p[np.where(p[:,2]>=0)]
viz_matplot(p)
#原始法向量
n0 = np.array([4, 4, 3])
#目标法向量
n1 = np.array([0, 0, 1])
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(p)
pcd = pcd_rotate_normal(pcd, n0, n1)
p = np.array(pcd.points)
#旋转后z的值应该基本相等
print('min Z: ', np.min(p[:, -1]), 'max Z: ', np.max(p[:, -1]))
viz_matplot(p)
plt.show()
5 旋转效果
旋转前和旋转后的图片如下图所示。目标法向量为(0,0,1),即于xoy平面垂直,旋转后的平面应与xoy平面平行。
6 【python三维深度学习】python三维点云从基础到深度学习_Coding的叶子的博客-CSDN博客_python 三维点云
更多三维、二维感知算法和金融量化分析算法请关注“乐乐感知学堂”微信公众号,并将持续进行更新。
边栏推荐
- Launpad | Basics
- How do microservices aggregate API documents? This wave of show~
- Markdown syntax
- Report on research and investment prospect prediction of China's electronic grade sulfuric acid industry (2022 Edition)
- LeetCode 74. Search 2D matrix
- How does idea withdraw code from remote push
- Review of last week's hot spots (6.27-7.3)
- Lauchpad x | MODE
- Function comparison between cs5261 and ag9310 demoboard test board | cost advantage of cs5261 replacing ange ag9310
- 2022-2028 global elastic strain sensor industry research and trend analysis report
猜你喜欢
How to batch change file extensions in win10
Mac platform forgets the root password of MySQL
After unplugging the network cable, does the original TCP connection still exist?
C语言-入门-基础-语法-数据类型(四)
HMS core helps baby bus show high-quality children's digital content to global developers
Logstack configuration details -- elasticstack (elk) work notes 020
How should PMP learning ideas be realized?
At the age of 30, I changed to Hongmeng with a high salary because I did these three things
If you can quickly generate a dictionary from two lists
C language - Introduction - Foundation - syntax - [operators, type conversion] (6)
随机推荐
Global and Chinese PCB function test scale analysis and development prospect planning report Ⓑ 2022 ~ 2027
Launpad | 基礎知識
Global and Chinese markets of thrombography hemostasis analyzer (TEG) 2022-2028: Research Report on technology, participants, trends, market size and share
Review of last week's hot spots (6.27-7.3)
Global and Chinese markets for laser assisted liposuction (LAL) devices 2022-2028: Research Report on technology, participants, trends, market size and share
2022-2028 global edible probiotic raw material industry research and trend analysis report
Les différents modèles imbriqués de listview et Pageview avec les conseils de flutter
In depth research and investment strategy report on China's hydraulic parts industry (2022 Edition)
A subclass must use the super keyword to call the methods of its parent class
C language - Introduction - Foundation - syntax - [operators, type conversion] (6)
Awk from getting started to digging in (11) detailed explanation of awk getline function
2022-2028 global elastic strain sensor industry research and trend analysis report
Reload CUDA and cudnn (for tensorflow and pytorch) [personal sorting summary]
20220701 Barbalat引理证明
There are 100 people eating 100 apples, one adult eating 4 apples, and four children eating 1 apple. How can they eat exactly 100 apples? Use any high-level language you are familiar with
Mac platform forgets the root password of MySQL
At the age of 30, I changed to Hongmeng with a high salary because I did these three things
Summary of the most comprehensive CTF web question ideas (updating)
2022-2028 global intelligent interactive tablet industry research and trend analysis report
Awk from entry to earth (18) GAW K line manual