点云PCA特征值计算去除地面(附open3d python代码)

通过计算每个点的特征根和特征值,来判断是否为地面
计算每个点的法向量在Z轴的投影


# coding:utf-8

import open3d as o3d
import numpy as np
import copy


def pcd_ground_seg_pca(scan, th=0.80, z_offset=-1.1):
  """ Perform PCA over PointCloud to segment ground.
  """
  pcd = copy.deepcopy(scan)
  _, covariance = pcd.compute_mean_and_covariance()
  eigen_vectors = np.linalg.eig(covariance)[1]
  k = eigen_vectors.T[2]

  # magnitude of projecting each face normal to the z axis
  normals = np.asarray(scan.normals)
  points = np.asarray(scan.points)
  mag = np.linalg.norm(np.dot(normals, k).reshape(-1, 1), axis=1)
  ground = pcd.select_by_index(np.where((mag >= th) & (points[:, 2] < z_offset))[0])
  rest = pcd.select_by_index(np.where((mag >= th) & (points[:, 2] < z_offset))[0], invert=True)

  # Also remove the faces that are looking downwards
  up_normals = np.asarray(ground.normals)
  orientation = np.dot(up_normals, k)
  ground = ground.select_by_index(np.where(orientation > 0.0)[0])

  ground.paint_uniform_color([1.0, 0.0, 0.0])
  rest.paint_uniform_color([0.0, 0.0, 1.0])
  
  return ground, rest

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
以下是使用PCA算法进行人脸识别中特征脸提取的Python代码实现: ```python import numpy as np from PIL import Image import os # 读取数据集 def read_images(path, sz=None): c = 0 X, y = [], [] for dirname, dirnames, filenames in os.walk(path): for subdirname in dirnames: subject_path = os.path.join(dirname, subdirname) for filename in os.listdir(subject_path): try: # 将图像转换为灰度图像 im = Image.open(os.path.join(subject_path, filename)).convert('L') # 将图像大小重新调整为sz if sz is not None: im = im.resize(sz, Image.ANTIALIAS) # 将图像转换为NumPy数组 X.append(np.asarray(im, dtype=np.uint8)) y.append(c) except IOError as e: print("I/O error({0}): {1}".format(e.errno, e.strerror)) except: print("Unexpected error:", sys.exc_info()[0]) raise c = c+1 return [X,y] # 使用PCA算法进行特征脸提取 def pca(X): # 计算均值 mean_X = X.mean(axis=0) # 中心化X X = X - mean_X # 计算协方差矩阵 cov = np.dot(X.T, X) # 计算特征向量和特征值 evals, evecs = np.linalg.eig(cov) # 将特征向量按特征值大小降序排列 idx = np.argsort(evals)[::-1] evecs = evecs[:,idx] # 选择前k个特征向量 k = 100 evecs = evecs[:, :k] # 计算特征脸 X_pca = np.dot(X, evecs) return X_pca # 读取图像数据集 [X,y] = read_images('path/to/dataset') # 将图像数据集转换为NumPy数组 X = np.asarray(X) # 使用PCA算法进行特征脸提取 X_pca = pca(X) # 显示特征脸 for i in range(X_pca.shape[1]): im = Image.fromarray(X_pca[:,i].reshape(112,92)) im.show() ``` 在上面的代码中,`read_images`函数用于读取图像数据集,`pca`函数用于使用PCA算法进行特征脸提取,`X_pca`存储了特征脸,最后使用`Image`模块将特征脸转换为图像并显示出来。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

点云-激光雷达-Slam-三维牙齿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值