OPENCV3 Learning: Reshape function

Source: Internet
Author: User

In OpenCV, the reshape function is interesting, it can not only change the number of channels of the Matrix, but also the matrix elements can be serialized, a very useful function.

Function Prototypes:

C + +: Mat mat::reshape (intint rows=0Const

There are fewer parameters, but you must be careful when setting them.

CN: Indicates the number of channels (channels), if set to 0, means that the number of hold channels is constant, otherwise it becomes the number of channels set.

Rows: Represents the number of matrix rows. Set to 0 to keep the number of rows unchanged, or the number of rows that are set.

First set up an initial matrix: a 20-row, 30-column, 1-channel matrix

intMain () {Mat data= Mat ( -, -, cv_32f);//set a 20-row, 30-column, 1-channel matrixcout <<"number of rows:"<< data.rows <<Endl; cout<<"Number of columns:"<< Data.cols <<Endl; cout<<"Channel:"<< data.channels () <<Endl; System ("Pause"); return 1;}

Output:

First Change : The number of channels is constant, and the matrix is serialized as a row vector of 1 rows n columns.

intMain () {Mat data= Mat ( -, -, cv_32f);//set a 20-row, 30-column, 1-channel matrixcout <<"number of rows:"<< data.rows <<Endl; cout<<"Number of columns:"<< Data.cols <<Endl; cout<<"Channel:"<< data.channels () <<Endl; cout<<Endl; Mat DST= Data.reshape (0,1); cout<<"number of rows:"<< dst.rows <<Endl; cout<<"Number of columns:"<< Dst.cols <<Endl; cout<<"Channel:"<< dst.channels () <<Endl; System ("Pause"); return 1;}

Second Change : The number of channels is constant, and the matrix is serialized as a column vector of n rows 1 columns.

intMain () {Mat data= Mat ( -, -, cv_32f);//set a 20-row, 30-column, 1-channel matrixcout <<"number of rows:"<< data.rows <<Endl; cout<<"Number of columns:"<< Data.cols <<Endl; cout<<"Channel:"<< data.channels () <<Endl; cout<<Endl; Mat DST= Data.reshape (0, data.rows*data.cols); cout<<"number of rows:"<< dst.rows <<Endl; cout<<"Number of columns:"<< Dst.cols <<Endl; cout<<"Channel:"<< dst.channels () <<Endl; System ("Pause"); return 1;}

It can be seen that the sequence of columns is more troublesome than the row vector, and it is necessary to calculate how many rows are needed. But we can sequence the row vectors first, then transpose

    Mat DST = Data.reshape (01);      // sequence rows vector    Mat DST = Data.reshape (01). T ();  // sequence column vector

Third Change : The number of channels is changed from 1 to 2, and the number of rows is unchanged.

intMain () {Mat data= Mat ( -, -, cv_32f);//set a 20-row, 30-column, 1-channel matrixcout <<"number of rows:"<< data.rows <<Endl; cout<<"Number of columns:"<< Data.cols <<Endl; cout<<"Channel:"<< data.channels () <<Endl; cout<<Endl; Mat DST= Data.reshape (2, 0); cout<<"number of rows:"<< dst.rows <<Endl; cout<<"Number of columns:"<< Dst.cols <<Endl; cout<<"Channel:"<< dst.channels () <<Endl; System ("Pause"); return 1;}

As you can see from the results, the number of columns is halved and placed in a second channel.

Similarly, if the number of channels changes from 1 to 3, the number of rows does not change. The number of columns per channel becomes the original One-third.

It is important to note that if the rows remain unchanged, the number of channels changed must be divisible by the number of columns, otherwise an error will occur.

Fourth Change : The number of channels changes from 1 to 2, and the number of rows becomes one-fifth.

intMain () {Mat data= Mat ( -, -, cv_32f);//set a 20-row, 30-column, 1-channel matrixcout <<"number of rows:"<< data.rows <<Endl; cout<<"Number of columns:"<< Data.cols <<Endl; cout<<"Channel:"<< data.channels () <<Endl; cout<<Endl; Mat DST= Data.reshape (2, data.rows/5); cout<<"number of rows:"<< dst.rows <<Endl; cout<<"Number of columns:"<< Dst.cols <<Endl; cout<<"Channel:"<< dst.channels () <<Endl; System ("Pause"); return 1;}

It can be seen that, however changed, it follows an equation:

Rows*cols*channels before change = rows*cols*channels after change

We can only change the number of channels and the number of rows, the number of columns can not be changed, it is automatically changed.

However, it is important to consider whether the division is divisible when changing. If the changed values are not divisible, an error will be obtained.

Finally, let's check again: is OPENCV serialized or column serialized?

We know that in MATLAB, is the column serialization, that is, the value is from the top to the bottom, from left to right, OpenCV is what?

intMain () {Mat data= (mat_<int> (2,3) <<1,2,3,Ten, -, -);//2 rows 3 columns of matrixcout << Data <<Endl; Mat Dst1= Data.reshape (0,6);//channel invariant, sequence column vectorcout <<endl<< Dst1 <<Endl; Mat Dst2= Data.reshape (0,1);//channel invariant, sequence of rows vectorcout << Endl << dst2 <<Endl; System ("Pause"); return 1;}

From the results, whether it is changing the line vector or the column vector, OpenCV is the row serialization, that is, from left to right, from top to bottom, and MATLAB is not the same.

A simple function, but very powerful! Are you going to use it?

OPENCV3 Learning: Reshape function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.