Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

here's some code to illustrate it:

Mat_<float> m(3,3); m << 1,2,3,4,5,6,7,8,9;

cerr << m << endl;

[1, 2, 3; 4, 5, 6; 7, 8, 9]

cerr << m(0,1) << endl; 2 Mat_<float> m2 = m.reshape(0,9);

cerr << m2 << endl; [1; 2; 3; 4; 5; 6; 7; 8; 9]

cerr << m2(0,1) << endl;

OpenCV Error: Assertion failed ((unsigned)i1 < (unsigned)size.p[1]) in cv::Mat_<float>::operator (), file E:\code\opencv\build\install\include\opencv2/core/mat. inl.hpp, line 1602

your reshape is correct, but then, you seem to have trouble with different indexing conventions between c++ in general, and matlab.

please, when in doubt, you should run a debug build, a lot of important assertions are suppressed in release.

here's some code to illustrate it:

Mat_<float> m(3,3); m << 1,2,3,4,5,6,7,8,9;

1,2,3,4,5,6,7,8,9;

cerr << m << endl;

endl;

[1, 2, 3; 4, 5, 6; 7, 8, 9]

9]

cerr << m(0,1) << endl; 2 Mat_<float> m2 = m.reshape(0,9);

m.reshape(0,9);

cerr << m2 << endl; [1; 2; 3; 4; 5; 6; 7; 8; 9]

9]

cerr << m2(0,1) << endl;

endl;

OpenCV Error: Assertion failed ((unsigned)i1 < (unsigned)size.p[1]) in cv::Mat_<float>::operator (), file E:\code\opencv\build\install\include\opencv2/core/mat. inl.hpp, line 1602

1602

your reshape is correct, but then, you seem to have trouble with different indexing conventions between c++ in general, and matlab.

please, when in doubt, you should run a debug build, a lot of important assertions are suppressed in release.

here's some code to illustrate it:

Mat_<float> m(3,3); m << 1,2,3,4,5,6,7,8,9;    
cerr << m << endl;

[1, 2, 3;
 4, 5, 6;
 7, 8, 9]

cerr << m(0,1) << endl;
2


Mat_<float> m2 = m.reshape(0,9);   
cerr << m2 << endl;
[1;
 2;
 3;
 4;
 5;
 6;
 7;
 8;
 9]

// out of bounds:
cerr << m2(0,1) << endl;

OpenCV Error: Assertion failed ((unsigned)i1 < (unsigned)size.p[1]) in cv::Mat_<float>::operator (), file E:\code\opencv\build\install\include\opencv2/core/mat.
inl.hpp, line 1602

// this would be the correct way for a 1d col-mat:
cerr << m2(1,0) << endl;

2

your reshape is correct, but then, you seem to have trouble with different indexing conventions between c++ in general, and matlab.

please, when in doubt, you should run a debug build, a lot of important assertions are suppressed in release.

here's some code to illustrate it:

Mat_<float> m(3,3); m << 1,2,3,4,5,6,7,8,9;    
cerr << m << endl;

[1, 2, 3;
 4, 5, 6;
 7, 8, 9]

cerr << m(0,1) << endl;
2

cerr << m(1,0) << endl;
4


Mat_<float> m2 = m.reshape(0,9);   
cerr << m2 << endl;
[1;
 2;
 3;
 4;
 5;
 6;
 7;
 8;
 9]

// out of bounds:
cerr << m2(0,1) << endl;

OpenCV Error: Assertion failed ((unsigned)i1 < (unsigned)size.p[1]) in cv::Mat_<float>::operator (), file E:\code\opencv\build\install\include\opencv2/core/mat.
inl.hpp, line 1602

// this would be the correct way for a 1d col-mat:
cerr << m2(1,0) << endl;

2

your reshape is correct, but then, you seem to have trouble with different indexing conventions between c++ in general, and matlab.

please, when in doubt, you should run a debug build, a lot of important assertions are suppressed in release.

here's some code to illustrate it:

Mat_<float> m(3,3); m << 1,2,3,4,5,6,7,8,9;    
cerr << m << endl;

[1, 2, 3;
 4, 5, 6;
 7, 8, 9]

cerr << m(0,1) << endl;
2

cerr << m(1,0) << endl;
4


Mat_<float> m2 = m.reshape(0,9);   
cerr << m2 << endl;
[1;
 2;
 3;
 4;
 5;
 6;
 7;
 8;
 9]

// out of bounds:
cerr << m2(0,1) << endl;

OpenCV Error: Assertion failed ((unsigned)i1 < (unsigned)size.p[1]) in cv::Mat_<float>::operator (), file E:\code\opencv\build\install\include\opencv2/core/mat.
inl.hpp, line 1602

// this would be the correct way for a 1d col-mat:
cerr << m2(1,0) << endl;
 2

[edit:]

it seems, you want the transposed mat reshaped:

Mat mt = m.t();
cerr << mt << endl;
[1, 4, 7;
 2, 5, 8;
 3, 6, 9]

cerr << mt.reshape(0,9) << endl;
[1;
 4;
 7;
 2;
 5;
 8;
 3;
 6;
 9]