1

I have this piece of code:

   points = np.array([[207.0,489.0], [500.0,58.0], [84.0,17.0],[197.0,262.0]])
    #read data from the csv file 
    x = df["XC"]
    y = df["YC"]
    # make the x,y a point type 
    pointP = np.array([x,y])
    #print(pointP)
    rW,rV,rU = calcRatios(points,pointP,ratioU,ratioV,ratioW)

and this is the calcRatios function

def calcRatios(points,centreP,ratioU,ratioV,ratioW):
    v0 = points[1] - points[0]
    v1 = points[2] - points[0]
    v2 = centreP - points[0]
    #dot product of the vects
    d00 = np.dot(v0, v0)
    d01 = np.dot(v0, v1)
    d11 = np.dot(v1, v1)
    d20 = np.dot(v2, v0)
    d21 = np.dot(v2, v1)
    #calc denom
    denom = d00 * d11 - d01 * d01
    #barycentric ratios of v,w,u
    ratioV = (d11 * d20 - d01 * d21) / denom
    ratioW = (d00 * d21 - d01 * d20) / denom
    ratioU = 1.0 - ratioV - ratioW
    return ratioV,ratioW,ratioU

The data from the dataframe is stored like this:

       Index      XC      YC    R    G    B
           1       0       0  227  227  227
           2       1       0  237  237  237
           3       2       0   0     0    0
           4       3       0  232  232  232
           5       4       0  233  233  233
...        ...     ...     ...  ...  ...  ...

However right now there seems to be an issue with the centreP point that I am passing into the function and I am not sure why.

The error I get says : ValueError: operands could not be broadcast together with shapes (2,28686) (2,) for this line v2 = centreP - points[0]

Could someone tell me why this is happening and how it should be fixed?

Thank you!

10
  • What is the value of df["XC"] and df["YC"]? Commented Jun 18, 2021 at 4:53
  • @haccks I printed out pointP and [[0 1 2 ... 504 505 506] [ 0 0 0 ... 508 508 508]] this is what I get. I wanted it to be saved as [x,y] points. Commented Jun 18, 2021 at 4:56
  • This won't do what you are expecting. Use list comprehensions to make list of points out of the two lists. Commented Jun 18, 2021 at 4:58
  • @haccks made an edit to show the dataframe :) Commented Jun 18, 2021 at 4:58
  • 1
    Did it solve the problem? Commented Jun 18, 2021 at 5:23

2 Answers 2

2

Your centreP is an array of two long arrays: [[0, 1, 2...], [0,0,0...]], while points[0] is a single array of length 2, [207.0,489.0]. numpy's broadcasting rules can't handle subtraction of these two shapes. However if you transpose centreP to [[0,0], [1,0], [2,0]...] it will handle this by subtracting points[0] from each row:

 v2 = centreP.T - points[0]

Better still, pass df[[XC, YC]] instead of pointP - it will be coerced to a numpy array in the right shape.

rW,rV,rU = calcRatios(points, df[["XC","YC"]]) # no need to transpose
Sign up to request clarification or add additional context in comments.

Comments

0

np.array([x,y]) will return an array of arrays, not what you are expecting. If you want to have an array of points out of x and y then this will do the job

pointP = np.array([[a, b] for a, b in zip(x, y)])  

1 Comment

It's unnecessary and inefficient to zip together columns from the same dataframe. In general, use numpy or pandas methods to manipulate and reshape data stored in those formats, rather than converting to lists and then back to an array.

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.