1

I would like to improve this code in order to return a list of "solutions" at the end:

[bcoords[0, 1, 2, 3], R[0, 1, 2, 3], G[0, 1, 2, 3], B[0, 1, 2, 3]]

code:

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

_

The code loads two .csv files in two np.array and use the module scipy.spatial.Delaunay to find the barycentric coordinates of a point in a tetrahedron.

XYZcolorlist.csv is a cloud of points R, G, B, X, Y, Z

and targets.csv is a set of targets 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
...

For each point of targets.csv, I want to get:

  • the 4 vertices containing the point

  • the 4 float(R), float(G), float(B) associated with each vertex:

  • the 4 barycentric coordinates associated with each point

and I want to do it fast, using numpy

The code gives all this, except for the 4 R, G, B's

alternatively, I could load the entire file's data with this code:

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

How can I return the list:

[bcoords[0, 1, 2, 3], R[0, 1, 2, 3], G[0, 1, 2, 3], B[0, 1, 2, 3]] ?

Is it possible to build a dict[] ?

Thanks

1 Answer 1

1

I'd really use np.genfromtxt to read the csv files. Here is an example:

import numpy as np
X, Y, Z = np.genfromtxt('targets.csv', delimiter=',', unpack=True)

This is much easier than csv, and will return a numpy.ndarray immediately.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.