cv::Mat::reshape always returns 1 col (Bug #884)


Added by peter heintges about 14 years ago. Updated about 14 years ago.


Status:Done Start date:
Priority:High Due date:
Assignee:Vadim Pisarevsky % Done:

0%

Category:core
Target version:-
Affected version: Operating System:
Difficulty: HW Platform:
Pull request:

Description

testcase:

Mat m2;
Mat mat(4,4,CV_8UC1);
cout << mat.cols << ", " << mat.rows << std::endl;
m2 = mat.reshape(16,1);
cout << m2.cols << ", " << m2.rows << std::endl;
m2 = mat.reshape(1,16);
cout << m2.cols << ", " << m2.rows << std::endl;
m2 = mat.reshape(2,8);
std::cout << m2.cols << ", " << m2.rows << std::endl;
m2 = mat.reshape(8,2);
cout << m2.cols << ", " << m2.rows << std::endl;
result:
4, 4
1, 1
1, 16
1, 8
1, 2

looking at core/src/matrix.cpp, l736, i see that
total_width gets reassigned, thus always leading to a col-count of 1 later.

there seems to be confusion here, whether total_size or total_width
should be used for the recalculation of the cols.


Associated revisions

Revision 4d866ea3
Added by Vadim Pisarevsky almost 12 years ago

Merge pull request #884 from pengx17:2.4_pyrup_fix

History

Updated by Vadim Pisarevsky about 14 years ago

The output is correct indeed. mat.reshape(a, b) parameters are not (a=new_rows, b=new_columns), it's (a=new_number_of_channels, b=new_rows). new_columns is automatically computed from that.

You created 4x4 1-channel matrix.
Then you reshaped it to 16-channel 1x1 matrix.
Then - to 1-channel 16x1 matrix.
Then - to 2-channel 8x1 matrix.
Then - to 8-channel 2x1 matrix.

As you may see, in all cases the number of columns is indeed =1.

  • Status changed from Open to Done
  • (deleted custom field) set to worksforme

Also available in: Atom PDF