reshape函数既可以改变矩阵的通道数,又可以对矩阵元素进行序列化,而且不需要复制数据。

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

参数虽然比较少,但是设置时需要小心

参数说明:
cn - 表示通道数(channels), 如果设为0,则表示保持通道数不变,否则则变为设置的通道数。
rows - 表示矩阵行数。 如果设为0,则表示保持原有的行数不变,否则则变为设置的行数。

首先我们设置一个20行30列1通道的一个矩阵

#include <opencv2\opencv.hpp>

using namespace cv;
using namespace std;

int main(int /*argc*/, char** /*argv*/)
{
	Mat data = Mat(20, 30, CV_32F);  //设置一个20行30列1通道的一个矩阵
	cout << "行数: " << data.rows << endl;
	cout << "列数: " << data.cols << endl;
	cout << "通道: " << data.channels() << endl;
	system("pause");
	return 0;
}

输出结果为:




reshape参数的意思 reshape函数参数_reshape参数的意思


第一次变化:通道数保持不变,将矩阵序列化1行N列的行向量

cout << endl;
	Mat dst = data.reshape(0, 1);
    cout << "行数: " << dst.rows << endl;
    cout << "列数: " << dst.cols << endl;
    cout << "通道: " << dst.channels() << endl;

输出结果为:



reshape参数的意思 reshape函数参数_reshape参数的意思_02


第二次变化:通道数不变,将矩阵序列化N行1列的列向量。

cout << endl;
	Mat dst = data.reshape(0, data.rows*data.cols);
	cout << "行数: " << dst.rows << endl;
	cout << "列数: " << dst.cols << endl;
	cout << "通道: " << dst.channels() << endl;

输出结果为:



reshape参数的意思 reshape函数参数_reshape参数的意思_03

可见,序列成列向量比行向量要麻烦一些,还得去计算出需要多少行。但我们可以先序列成行向量,再转置。

Mat dst = data.reshape(0, 1);      //序列成行向量
    Mat dst = data.reshape(0, 1).t();  //序列成列向量

第三次变化:通道数由1变为2,行数不变。

cout << endl;
	Mat dst = data.reshape(2, 0);
	cout << "行数: " << dst.rows << endl;
	cout << "列数: " << dst.cols << endl;
	cout << "通道: " << dst.channels() << endl;

输出结果



reshape参数的意思 reshape函数参数_序列化_04


从结果可以看出,列数被分出一半,放在第二个通道里去了。
同理,如果通道数由1变为3,行数不变。则每通道的列数变为原来的三分之一。
需要注意的是,如果行保持不变,改变的通道数一定要能被列数整除,否则会出错。

第四次变化:通道数由1变为2,行数变为原来的五分之一。

cout << endl;
	Mat dst = data.reshape(2, data.rows / 5);
	cout << "行数: " << dst.rows << endl;
	cout << "列数: " << dst.cols << endl;
	cout << "通道: " << dst.channels() << endl;

输出结果:



reshape参数的意思 reshape函数参数_reshape参数的意思_05


可见,不管怎么变,都遵循这样一个等式:
变化之前的 rowscolschannels = 变化之后的 rowscolschannels
我们只能改变通道数和行数,列数不能改变,它是自动变化的。
但是要注意的是,在变化的时候,要考虑到是否整除的情况。如果改变的数值出现不能整除,就会报错。

最后,再验证一下:opencv在序列化的时候是行序列化还是列序列化呢?

注:我们知道,在matlab里面,是列序列化, 即取值为从上到下,从左到右,opencv又是怎么样的呢

int main(int /*argc*/, char** /*argv*/)
{
	
	Mat data = (Mat_<int>(2, 3) << 1, 2, 3, 10, 20, 30);  //2行3列的矩阵
	cout << data << endl;
	Mat dst1 = data.reshape(0, 6);   //通道不变,序列成列向量
	cout << endl << dst1 << endl;
	Mat dst2 = data.reshape(0, 1);   //通道不变,序列成行向量
	cout << endl << dst2 << endl;
	system("pause");
	return 1;
	
}

输出结果:



reshape参数的意思 reshape函数参数_整除_06


从结果看出,不管是变化成行向量还是列向量,opencv都是行序列化,即从左到右,从上到下,与matlab是不一样的。