First time here? Check out the FAQ!

Ask Your Question
0

Converting Mat img to one dimensional float array in C++ which is in row, column, channel order?

asked Jun 25 '15

erogol2 gravatar image

I try to convert a Mat image object to float array which includes the data in row major order. Which means 1D array includes the first row of the image, then the second row etc. At the end, it repeats this for the next image channel.

Here is the silly pseudo description float *A = [first_row_first_channel, second_row_first_channel, ..., first_row_second_channel, ...]

Does Mat img.data provides this ordering or it gives something different? If this is different how can I get the desired orientation ?

Preview: (hide)

1 answer

Sort by » oldest newest most voted
5

answered Jun 26 '15

berak gravatar image

opencv has a reshape method, that will flatten your image quite cheap(no memory is moved, it's just rearranging the header)

 Mat m = ...
 Mat m_flat = m.reshape(1,1); // flat 1d
 // also note, that it *returns* a new Mat header (common pitfall, folks tend to forget to catch it)

if you really need to seperate the channels, it gets more expensive:

vector<Mat> chans;
split(m, chans);

Mat res; // stack the channels into a new mat:
for (size_t i=0; i<chans.size(); i++)
     res.push_back(chans[i]);

res = res.reshape(1,1); // flat 1d, again.
Preview: (hide)

Question Tools

2 followers

Stats

Asked: Jun 25 '15

Seen: 19,795 times

Last updated: Jun 26 '15