Edster
Edster

Reputation: 153

OpenCV reshape function does not work properly

I am using OpenCV reshape function in order to reshape Mat of 25 rows and 1 column (so we have 25 data values) into Mat of 5 rows and 5 columns.

data = mu.reshape(5, 5);

When I look at my data variable in debugger, it has 5 rows but 1 column. If I print data at row(0) and col(0) it outputs all five values. So basically each row at col(0) contains 5 values.

My desired result is to get 5 rows and 5 columns where on each (row,col) will be one value.

Thank you in advance for your help.

Upvotes: 5

Views: 8972

Answers (1)

Dan Mašek
Dan Mašek

Reputation: 19041

You seem to have misinterpreted the meaning of arguments of the reshape() function.

According to the documentation the signature is

Mat Mat::reshape(int cn, int rows=0) const

With the following meaning of the arguments:

  • cn – New number of channels. If the parameter is 0, the number of channels remains the same.
  • rows – New number of rows. If the parameter is 0, the number of rows remains the same.

Note that the number of columns is implicit -- it's calculated from the existing matrix properties and the two parameters.

According to this, the code

data = mu.reshape(5, 5);

creates a 5-channel matrix of 5 rows and 1 column.

In order to reshape you matrix to a single channel 5x5 matrix, you have to do the following:

data = mu.reshape(1, 5);

Alternately, since the input matrix is already single channel, you can also use

data = mu.reshape(0, 5);

Upvotes: 8

Related Questions