Ask Your Question
0

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

asked 2015-06-25 06:45:46 -0600

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 ?

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
5

answered 2015-06-26 00:52:37 -0600

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.
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2015-06-25 06:45:46 -0600

Seen: 19,521 times

Last updated: Jun 26 '15