我希望改进这段代码,以便在最后返回“解决方案”的list:
[bcoords[0, 1, 2, 3], R[0, 1, 2, 3], G[0, 1, 2, 3], B[0, 1, 2, 3]]
代码:
import csv
import numpy as np
import scipy.spatial
points = np.array([(float(X), float(Y), float(Z))
for R, G, B, X, Y, Z in csv.reader(open('XYZcolorlist_D65.csv'))])
# load XYZ coordinates of 'points' in a np.array
tri = scipy.spatial.Delaunay(points)
# do the triangulation
indices = tri.simplices
# indices of vertices
vert = points[tri.simplices]
# the vertices for each tetrahedron
targets = np.array([(float(X), float(Y), float(Z))
for X, Y, Z in csv.reader(open('targets.csv'))])
# load the XYZ target values in a np.array
tetrahedra = tri.find_simplex(targets)
# find which tetrahedron each point belong to
X = tri.transform[tetrahedra,:3]
Y = targets - tri.transform[tetrahedra,3]
b = np.einsum('ijk,ik->ij', X, Y)
bcoords = np.c_[b, 1 - b.sum(axis=1)]
# find the barycentric coordinates of each point
print bcoords_
代码将两个.csv文件加载到两个np.array中,并使用模块scipy.spatial.Delaunay在tetrahedron中查找point的重心坐标。
XYZcolorlist.csv是点R,G,B,X,Y,Z的云
targets.csv是一组目标X,Y,Z
XYZcolorlist.csv:
255,63,127,35.5344302104,21.380721966,20.3661095969
255,95,127,40.2074945517,26.5282949405,22.7094284437
255,127,127,43.6647438365,32.3482625492,23.6181801523
255,159,127,47.1225628354,39.1780944388,22.9366615044
255,223,159,61.7379149646,62.8387601708,32.3936200864
...targets.csv:
49.72,5,8.64
50.06,5,8.64
50.4,5,8.64
50.74,5,8.64
51.08,5,8.64
51.42,5,8.64
51.76,5,8.64
...对于targets.csv的每一点,我都想得到:
vertices的4 pointfloat(R), float(G), float(B):barycentric coordinates相关联的4个point我想用numpy来做
代码给出了所有这些,除了4R, G, B的
或者,我可以用以下代码加载整个文件的数据:
points = np.array([(float(R), float(G), float(B), float(X), float(Y), float(Z))
for R, G, B, X, Y, Z in csv.reader(open('XYZcolorlist_D65.csv'))])
# load R,G,B,X,Y,Z coordinates of 'points' in a np.array如何返回列表:
[bcoords[0, 1, 2, 3], R[0, 1, 2, 3], G[0, 1, 2, 3], B[0, 1, 2, 3]]?
可以构建一个dict[]吗?
谢谢
发布于 2014-02-10 19:19:02
我真的会使用np.genfromtxt来读取csv文件。下面是一个示例:
import numpy as np
X, Y, Z = np.genfromtxt('targets.csv', delimiter=',', unpack=True)这比csv容易得多,并且将立即返回numpy.ndarray。
https://stackoverflow.com/questions/21616537
复制相似问题