1

I'm trying to write a function that will take as input a numpy array in the form:

a = [[0,0], [10,0], [10,10], [5,4]]

and return a numpy array b such that:

b = [[[0,0]], [[10,0]], [[10,10]], [[5,4]]]

For some reason I'm finding this surprisingly difficult.

The reason I'm doing this is that I have some contours generated using skimage that I'm attempting to use opencv2 on to calculate features ( area, perimeter etc...) but the opencv functions will only take arrays in the form of b as input, rather than a.

1 Answer 1

2

a is shape (4,2), b is (4,1,2)

a.reshape(4,1,2)
np.expanddims(a, 1)
a[:,None]

all work


In [503]: B
Out[503]: 
array([[[ 0,  0]],

       [[10,  0]],

       [[10, 10]],

       [[ 5,  4]]])
In [504]: B.tolist()
Out[504]: [[[0, 0]], [[10, 0]], [[10, 10]], [[5, 4]]]
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.