法线的夹角

import numpy as np
from scipy.spatial.transform import Rotation
# x + y + z + 2.6494 = 0
# 平面1的法向量
normal_plane1 = np.array([0.9941, -0.0218, -0.1060])

# 平面2的法向量
normal_plane2 = np.array([1,0,0])
# normal_plane2 = np.array([0, -0.03, -0.01])

# 计算法向量的长度(用于单位化)
length_plane1 = np.linalg.norm(normal_plane1)
length_plane2 = np.linalg.norm(normal_plane2)

# 单位化法向量
unit_normal_plane1 = normal_plane1 / length_plane1
unit_normal_plane2 = normal_plane2 / length_plane2

# 计算两法向量之间的夹角
cos_theta = np.dot(unit_normal_plane1, unit_normal_plane2)
theta = np.arccos(cos_theta)

# 计算旋转轴,使用叉积
rotation_axis = np.cross(unit_normal_plane1, unit_normal_plane2)
rotation_axis /= np.linalg.norm(rotation_axis)
print(rotation_axis)
# 创建旋转对象
r = Rotation.from_rotvec(theta * rotation_axis)

# 获取旋转矩阵
rotation_matrix = r.as_matrix()

# 计算平移矢量
translation_vector = np.array([0, 0, 0])  # 不需要平移,因为平面2是x=0

# 输出旋转矩阵和平移矢量
print("Rotation Matrix:")
print(rotation_matrix)
print("Translation Vector:")
print(translation_vector)

已知旋转前向量为P, 旋转后变为Q。由点积定义可知:

image可推出P,Q之间的夹角为:

image

2.旋转轴

由1中可知,旋转角所在的平面为有P和Q所构成的平面,那么旋转轴必垂直该平面。

假定旋转前向量为a(a1, a2, a3), 旋转后向量为b(b1, b2, b3)。由叉乘定义得:

image

所以旋转轴c(c1, c2, c3)为:

image

如何估计平面法线

import open3d as o3d
pcd = o3d.io.read_point_cloud(r"C:\Users\peog\Desktop\volume\plane\a.pcd")
plane_model, inliers = pcd.segment_plane(distance_threshold=0.01,
                                         ransac_n=50,
                                         num_iterations=1000)
[a, b, c, d] = plane_model
print(f"Plane equation: {a:.4f}x + {b:.4f}y + {c:.4f}z + {d:.4f} = 0")

inlier_cloud = pcd.select_by_index(inliers)
inlier_cloud.paint_uniform_color([1.0, 0, 0])
outlier_cloud = pcd.select_by_index(inliers, invert=True)
outlier_cloud.paint_uniform_color([0, 1, 0])
# o3d.visualization.draw_geometries([inlier_cloud, outlier_cloud])

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用Python实现该算法的代码: ```python import numpy as np from scipy.spatial import cKDTree def kd_tree_neighbor_points(PV, k=20): tree = cKDTree(PV) neighbor_indices = tree.query(PV, k=k+1, n_jobs=-1)[1][:,1:] neighbor_points = np.take(PV, neighbor_indices, axis=0) return neighbor_points def compute_normal_and_curvature(neighbor_points): centroid = np.mean(neighbor_points, axis=0) neighbor_points_centered = neighbor_points - centroid covariance_matrix = np.dot(neighbor_points_centered.T, neighbor_points_centered) / (neighbor_points.shape[0] - 1) eig_values, eig_vectors = np.linalg.eig(covariance_matrix) normal = eig_vectors[:, 2] curvature = eig_values[2] / (eig_values[0] + eig_values[1] + eig_values[2]) return normal, curvature def extract_plane_points(PV, g=3): remaining_points = PV.copy() plane_points = [] while remaining_points.shape[0] > 0: seed = remaining_points[0] neighbor_points = kd_tree_neighbor_points(remaining_points) normal, curvature = compute_normal_and_curvature(neighbor_points) if curvature < 0.1: plane_point_indices = [0] for i in range(1, neighbor_points.shape[0]): neighbor_normal, _ = compute_normal_and_curvature(neighbor_points[i:]) angle = np.degrees(np.arccos(np.dot(normal, neighbor_normal))) if angle < g: plane_point_indices.append(i) plane_points.append(neighbor_points[plane_point_indices]) remaining_points = np.delete(remaining_points, plane_point_indices, axis=0) else: remaining_points = np.delete(remaining_points, 0, axis=0) return plane_points, remaining_points ``` 其中,`PV` 表示三维点云,`k` 是 Kd-tree 中邻域点的个数,`g` 是法线夹角的阈值。函数 `kd_tree_neighbor_points` 用 Kd-tree 查找每个点的邻域点集合,函数 `compute_normal_and_curvature` 计算邻域协方差矩阵并提取出法线和曲率,函数 `extract_plane_points` 则是按照曲率排序,遍历每个点并计算法线夹角,提取出平面点云和剩余点云。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值