简体   繁体   English

Python 找到 3D 点云或 3D 外壳的长轴

[英]Python find the long axis of a 3D point cloud or 3D hull

I am trying to find the 'long axis' of a 3D object (described as a 3D boolean or label array), which passes through (or near) the centroid of that object.我试图找到一个 3D 对象(描述为 3D 布尔或标签数组)的“长轴”,它穿过(或靠近)该对象的质心。

I would like to think I could simply iterate over every pair of points in the object and pick the pair with the largest distance between them, but in the case of the example object below, that line would not pass through the center of the object.我想我可以简单地遍历对象中的每一对点并选择它们之间距离最大的点对,但是在下面的示例对象的情况下,该线不会穿过对象的中心。

Similarly, I think I could calculate the distance between every pair of points to find that longest pair that passes within some minimum distance of the center, but I'm worried that I'm reinventing the wheel.同样,我认为我可以计算每对点之间的距离,以找到在距离中心的某个最小距离内通过的最长的一对,但我担心我正在重新发明轮子。 It seems like there should be some least-squares fit of a line but the only solutions I have been able to dig up have been for 2D arrays.似乎应该有一些最小二乘拟合一条线,但我能够挖掘的唯一解决方案是二维数组。

Any suggestions would be greatly appreciated.任何建议将不胜感激。

The following code displays the dimensions of a very simple 3D shape.以下代码显示了一个非常简单的 3D 形状的尺寸。 The shapes I am actually interested contains thousands of points and are more complex but are similar in that 1) the largest distance between two points may be far from the center of the object and 2) they are connected such that they could be described by a single connected outline.我真正感兴趣的形状包含数千个点并且更复杂,但相似之处在于 1)两点之间的最大距离可能远离对象的中心,并且 2)它们是连接在一起的,因此它们可以被描述为单个连接的轮廓。

import matplotlib.pyplot as plt
import numpy as np

# make a simple 3D shape
t = [[[0,0,0,0,0],
      [0,0,1,0,0],
      [0,0,0,0,0],
      [0,0,0,0,0],
      [0,0,0,0,0]],

     [[0,0,0,0,0],
      [0,0,1,0,0],
      [0,1,1,1,0],
      [0,0,0,0,0],
      [0,0,0,0,0]],

     [[0,0,0,0,0],
      [0,0,1,0,0],
      [0,1,1,1,0],
      [0,0,0,0,0],
      [0,0,0,0,0]],

     [[0,0,0,0,0],
      [0,0,1,0,0],
      [0,1,1,1,0],
      [0,0,0,0,0],
      [0,0,0,0,0]],

     [[0,0,0,0,0],
      [0,0,1,0,0],
      [0,0,0,0,0],
      [0,0,0,0,0],
      [0,0,0,0,0]]]

t = np.array(t)

# find the centroid of the object
coords = np.where(t == 1)
x = np.mean(coords[0])
y = np.mean(coords[1])
z = np.mean(coords[2])

# show the object at different angles
fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
ax1.imshow(np.max(t, axis=0))
ax1.plot(x,y, 'ro')
ax1.set_title('xy')
ax2.imshow(np.max(t, axis=1))
ax2.plot(x,z, 'ro')
ax2.set_title('xz')
ax3.imshow(np.max(t, axis=2))
ax3.plot(y,z, 'ro')
ax3.set_title('yz')
plt.show()

在此处输入图像描述

The solution is here解决方案在这里

Just use只需使用

image=np.array([[0,0,0,0,0],
                [0,0,1,0,0],
                [0,1,1,1,0],
                [0,0,0,0,0],
                [0,0,0,0,0]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM