OpenCV中reshape()函数详解-改变矩阵的通道数,对矩阵元素进行序列化


OpenCV中reshape()函数详解-改变矩阵的通道数,对矩阵元素进行序列化

在opencv中reshape函数,既可以改变矩阵的通道数,又可以对矩阵元素进行序列化

1、函数原型
Mat Mat::reshape(
	int cn, 
	int rows=0
) const

参数解释:
cn:通道数,如果设为0,则表示保持通道数不变,否则变为设置的通道数;
rows:矩阵行数,如果设为0,则表示保持原有的行数不变,否则则变为设置的行数;
2、示例

初始化一个矩阵,20行30列1通道

#include <opencv2\opencv.hpp>
#include <iostream>
#include <demo.h>

using namespace cv;
using namespace std;

int main() {
    
    
	system("chcp 65001");

	// 初始化一个矩阵,20行30列1通道
	Mat data = Mat(20, 30, CV_32F);
	cout << "行数: " << data.rows << endl;
	cout << "列数: " << data.cols << endl;
	cout << "通道: " << data.channels() << endl;
	cout << endl;
	//(1)通道数不变,将矩阵序列化为1行N列的行向量
	Mat dstRows = data.reshape(0, 1);
	cout << "行数: " << dstRows.rows << endl;
	cout << "列数: " << dstRows.cols << endl;
	cout << "通道: " << dstRows.channels() << endl;
	cout << endl;
	//(2)通道数不变,将矩阵序列化N行1列的列向量
	/**
	* 序列成列向量比行向量要麻烦一些,还得去计算出需要多少行,但我们可以先序列成行向量,再转置;
	* Mat dst = data.reshape(0, 1);      //序列成行向量
	* Mat dst = data.reshape(0, 1).t();  //序列成列向量
	*/
	Mat dstCols = data.reshape(0, data.rows*data.cols);
	cout << "行数: " << dstCols.rows << endl;
	cout << "列数: " << dstCols.cols << endl;
	cout << "通道: " << dstCols.channels() << endl;
	cout << endl;

	//(3)通道数由1变为2,行数不变
	// 注意:从结果可以看出,列数被分出一半,放在第二个通道里去了;如果通道数由1变为3,行数不变,则每通道的列数变为原来的三分之一;需要注意的是,如果行保持不变,改变的通道数一定要能被列数整除,否则会报错
	Mat dstChannel1 = data.reshape(2, 0);
	cout << "行数: " << dstChannel1.rows << endl;
	cout << "列数: " << dstChannel1.cols << endl;
	cout << "通道: " << dstChannel1.channels() << endl;
	cout << endl;
	//(4)通道数由1变为2,行数变为原来的五分之一
	Mat dstChannel2 = data.reshape(2, data.rows/5);
	cout << "行数: " << dstChannel2.rows << endl;
	cout << "列数: " << dstChannel2.cols << endl;
	cout << "通道: " << dstChannel2.channels() << endl;
	cout << endl;

	waitKey();
	destroyAllWindows();
	return 0;
}

在这里插入图片描述

3、结论:
  • 由此可见,不管怎么变,都遵循这样一个等式:变化之前的 rows*cols*channels = 变化之后的 rows*cols*channels,我们只能改变通道数和行数,列数不能改变,它是自动变化的,但是需要注意的是,在变化的时候,要考虑到是否整除的情况,如果改变的数值出现不能整除的情况,就会报错;
  • opencv在序列化的时候是行序列化,即从左到右,从上到下;

猜你喜欢

转载自blog.csdn.net/qq_33867131/article/details/131769674
今日推荐