1

Given a numpy array like the one below, can you convert it to a multi-channel cv mat, collapsing the data into a single column?

arr = [[x1, y1],
       [x2, y2],  
       [x3, y3]]

arr = np.array(arr, dtype='f4')

mat = cv.fromarray(arr)

Done this way, the resulting mat has 3 rows and 2 single-channel columns (the mat is of type 32FC1). I need the resulting mat to have 3 rows and a single 2-channel column (specifically, to be of type 32FC2).

2
  • Does this thread helps ? stackoverflow.com/questions/12535715/… Commented Jul 9, 2015 at 10:57
  • Unfortunately no :( Attempting to use cv.Convert gives the following error: cv2.error: src.size == dst.size && src.channels() == dst.channels(). Looks like you can't convert between mats with different numbers of channels. Commented Jul 9, 2015 at 14:14

2 Answers 2

0

You can use np.array() on an existing array to create an array that will convert to an OpenCV Mat with additional channels:

a = np.array([1,2,3], dtype='float32')
a = np.array([a])

Will convert to Mat of type CV_32FC2 (two channels) under cv.fromarray

Source

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

Comments

0

The answer to your question is YES.

Given the example:

arr = [[x1, y1],
       [x2, y2],  
       [x3, y3]]
arr = np.array(arr, dtype='f4')
mat = arr[:, np.newaxis, :]

The resulting mat will be 3 rows, 1 column and 2 channels interpreted by OpenCV. The official numpy doc: Shape Manipulation explains more details.

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.