Ask Your Question

Matrix to single row

asked 2013-06-15 14:01:55 -0500

Hansg91 gravatar image


I need to convert a square cv::Mat to a single row. Right now I iterate over its rows and append them horizontally (cv::hconcat) to a separate matrix to get my single row matrix, but is there perhaps a better way?

Best regards, Hans

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-06-15 14:12:23 -0500

berak gravatar image

updated 2013-06-15 14:16:37 -0500

Mat row = mat.reshape(1,1);

and it even comes almost for free, as it's just adjusting the Mat's row/col members, no copy involved.

edit flag offensive delete link more


Hmm, thought I had tried that but it didn't work back then for some reason. Tried it again and seems to work now, hehe, thanks !

Hansg91 gravatar imageHansg91 ( 2013-06-15 14:37:23 -0500 )edit

Ah, I tried it again in another situation, reshape only works for continuous matrices, that was the problem I was having ... any reshape call for that?

Hansg91 gravatar imageHansg91 ( 2013-06-15 14:39:43 -0500 )edit

yea, have to admit, that the arguments for reshape are quite confusing

oh, imho it won't work at all for non-continuous Mat's. iterating is your only option there

berak gravatar imageberak ( 2013-06-15 14:42:40 -0500 )edit

Hmm, too bad, was hoping for some cv function.

Hansg91 gravatar imageHansg91 ( 2013-06-15 14:48:07 -0500 )edit

or cloning beforehand and then reshaping it

Guanta gravatar imageGuanta ( 2013-06-15 15:37:47 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2013-06-15 14:01:55 -0500

Seen: 1,769 times

Last updated: Jun 15 '13