Python Delaunay 三角剖分
Python Delaunay Triangulation
我有一个数组,其中包含代表点的不同向量。我想要得到的是每个点的所有邻居。这些点位于 'numpy.ndarray' 中,类型为 'numpy.float64'。我想使用 Delaunay Triangulation,但我的问题是我在我的数组中使用 'numpy.float64' 而不是整数。我已经发现的是这样的:
from scipy.spatial import Delaunay
import numpy as np
points = np.array([[-0.30352158, 0.73558974, 0.60562561],
[ 0.46504451, -0.4754239, 0.74679697],
[-0.52149363, 0.11833734, -0.84500927],
[ 0.11225645, 0.80278751, -0.58560285],
[-0.72246172, 0.57197704, 0.38844732],
[ 0.89957812, -0.07875899, -0.42960008],
[-0.4316689, -0.20747224, 0.87784807],
[-0.19440343, 0.55628405, -0.80793277]])
tri = Delaunay(points)
neighbor_cell = []
for i in range(len(points)):
neighbor = tri.vertex_neighbor_vertices[1][
tri.vertex_neighbor_vertices[0][i]:tri.vertex_neighbor_vertices[0][i + 1]] #from stack overflow
neighbor_cell.append(points[neighbor])
我不明白的是,使用上面的列表(点和 dir_vec)它可以工作,但是当我在我的实际代码中使用它时它不起作用。在我的实际代码中,我从 3D 图片中提取点并将这些点放入列表中。该列表与 "points" 列表相同,只是条目更多。因此,在将其放入我的主代码之前,我尝试获取前 8 个条目(如上所示的代码)的邻居。
当我在我的代码上尝试这个时,我得到这个错误:
"Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/Users/.../PycharmProjects/.../Code.py", line 80, in <module>
neighborPoints(convertCells())
File "/Users/.../PycharmProjects/.../Code.py", line 71, in neighborPoints
neighbor_cells.append(cells2[neighbor])
TypeError: only integer scalar arrays can be converted to a scalar index"
我不明白为什么上面的代码可以工作,但在我的实际代码中却不行。
我的实际代码:
import _pickle as cPickle
from Cell import *
from scipy.spatial import Delaunay
def load(pick):
with open(pick, 'rb') as input:
cells = cPickle.load(input, encoding='latin1')
coms = cPickle.load(input, encoding='latin1')
point_tree = cPickle.load(input, encoding='bytes')
print("cPickled loaded")
return cells, coms, point_tree
cells, coms, point_tree = load("...")
def convertCells():
# Converts Cells from Cell.Cell to np.array
cells2 = []
for i in range(len(cells)):
cells2.append(cells[i].getMainDir())
return cells2
def neighborPoints(cells2):
tri = Delaunay(cells2)
neighbor_cells = [] # local vector
help_func = []
neighbor_cell_dir = [] # directional vector
for i in range(len(cells2)):
neighbor = tri.vertex_neighbor_vertices[1][
tri.vertex_neighbor_vertices[0][i]:tri.vertex_neighbor_vertices[0][i+1]]
neighbor_cells.append(cells2[neighbor])
help_func.append(neighbor)
for i in help_func:
neighbor_cell_dir.append(coms[i])
return neighbor_cells, neighbor_cell_dir
cells2 的前 8 个条目与 Points 中的相同。如果我打印 cells2 的类型,然后打印前 8 个条目,就会出现这种情况:
print(type(cell2))
for i in range(8):
print(cells2[i])
我得到这个输出:
<class 'numpy.ndarray'>
[[-0.30352158 0.73558974 0.60562561]
[ 0.46504451 -0.4754239 0.74679697]
[-0.52149363 0.11833734 -0.84500927]
[ 0.11225645 0.80278751 -0.58560285]
[-0.72246172 0.57197704 0.38844732]
[ 0.89957812 -0.07875899 -0.42960008]
[-0.4316689 -0.20747224 0.87784807]
[-0.19440343 0.55628405 -0.80793277]]
我想要一个新列表中的所有相邻点,以便我可以进一步分析它们。有谁知道我做错了什么吗?
正如您在评论中 post 所说,cells2
是一个列表,而不是一个 numpy 数组。索引数组不适用于列表,您需要一个 numpy 数组。
也许这行得通:
def convertCells(cells):
cells2 = np.array(len(cells),3)
for i in range(len(cells)):
cells2[i,:] = cells[i].getMainDir()
return cells2
或者试试:
cells2 = np.asarray(cells2)
请注意,您post编辑的异常不能由列表引发。所以我假设你的 post 代表了几个不同问题的混合体。
我有一个数组,其中包含代表点的不同向量。我想要得到的是每个点的所有邻居。这些点位于 'numpy.ndarray' 中,类型为 'numpy.float64'。我想使用 Delaunay Triangulation,但我的问题是我在我的数组中使用 'numpy.float64' 而不是整数。我已经发现的是这样的:
from scipy.spatial import Delaunay
import numpy as np
points = np.array([[-0.30352158, 0.73558974, 0.60562561],
[ 0.46504451, -0.4754239, 0.74679697],
[-0.52149363, 0.11833734, -0.84500927],
[ 0.11225645, 0.80278751, -0.58560285],
[-0.72246172, 0.57197704, 0.38844732],
[ 0.89957812, -0.07875899, -0.42960008],
[-0.4316689, -0.20747224, 0.87784807],
[-0.19440343, 0.55628405, -0.80793277]])
tri = Delaunay(points)
neighbor_cell = []
for i in range(len(points)):
neighbor = tri.vertex_neighbor_vertices[1][
tri.vertex_neighbor_vertices[0][i]:tri.vertex_neighbor_vertices[0][i + 1]] #from stack overflow
neighbor_cell.append(points[neighbor])
我不明白的是,使用上面的列表(点和 dir_vec)它可以工作,但是当我在我的实际代码中使用它时它不起作用。在我的实际代码中,我从 3D 图片中提取点并将这些点放入列表中。该列表与 "points" 列表相同,只是条目更多。因此,在将其放入我的主代码之前,我尝试获取前 8 个条目(如上所示的代码)的邻居。 当我在我的代码上尝试这个时,我得到这个错误:
"Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/Users/.../PycharmProjects/.../Code.py", line 80, in <module>
neighborPoints(convertCells())
File "/Users/.../PycharmProjects/.../Code.py", line 71, in neighborPoints
neighbor_cells.append(cells2[neighbor])
TypeError: only integer scalar arrays can be converted to a scalar index"
我不明白为什么上面的代码可以工作,但在我的实际代码中却不行。 我的实际代码:
import _pickle as cPickle
from Cell import *
from scipy.spatial import Delaunay
def load(pick):
with open(pick, 'rb') as input:
cells = cPickle.load(input, encoding='latin1')
coms = cPickle.load(input, encoding='latin1')
point_tree = cPickle.load(input, encoding='bytes')
print("cPickled loaded")
return cells, coms, point_tree
cells, coms, point_tree = load("...")
def convertCells():
# Converts Cells from Cell.Cell to np.array
cells2 = []
for i in range(len(cells)):
cells2.append(cells[i].getMainDir())
return cells2
def neighborPoints(cells2):
tri = Delaunay(cells2)
neighbor_cells = [] # local vector
help_func = []
neighbor_cell_dir = [] # directional vector
for i in range(len(cells2)):
neighbor = tri.vertex_neighbor_vertices[1][
tri.vertex_neighbor_vertices[0][i]:tri.vertex_neighbor_vertices[0][i+1]]
neighbor_cells.append(cells2[neighbor])
help_func.append(neighbor)
for i in help_func:
neighbor_cell_dir.append(coms[i])
return neighbor_cells, neighbor_cell_dir
cells2 的前 8 个条目与 Points 中的相同。如果我打印 cells2 的类型,然后打印前 8 个条目,就会出现这种情况:
print(type(cell2))
for i in range(8):
print(cells2[i])
我得到这个输出:
<class 'numpy.ndarray'>
[[-0.30352158 0.73558974 0.60562561]
[ 0.46504451 -0.4754239 0.74679697]
[-0.52149363 0.11833734 -0.84500927]
[ 0.11225645 0.80278751 -0.58560285]
[-0.72246172 0.57197704 0.38844732]
[ 0.89957812 -0.07875899 -0.42960008]
[-0.4316689 -0.20747224 0.87784807]
[-0.19440343 0.55628405 -0.80793277]]
我想要一个新列表中的所有相邻点,以便我可以进一步分析它们。有谁知道我做错了什么吗?
正如您在评论中 post 所说,cells2
是一个列表,而不是一个 numpy 数组。索引数组不适用于列表,您需要一个 numpy 数组。
也许这行得通:
def convertCells(cells):
cells2 = np.array(len(cells),3)
for i in range(len(cells)):
cells2[i,:] = cells[i].getMainDir()
return cells2
或者试试:
cells2 = np.asarray(cells2)
请注意,您post编辑的异常不能由列表引发。所以我假设你的 post 代表了几个不同问题的混合体。