OpenCV C++:提取矩阵的部分与矩阵的合并与拼接

像在matlab中可以直接对矩阵进行提取与拼接,比如在matlab中合并两个矩阵:
矩阵A、B、C
按行合并C=[A B]
按列合并C=[A;B]

同样,在使用OpenCV时,OpenCV其实也是自带了提取和拼接矩阵的函数
提取矩阵A的部分到矩阵B中:

#include 
#include 
#include 
 
using namespace cv;
using namespace std;
int main()
{
	Mat A = (Mat_<double>(3,3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);
	cout <<"原矩阵为:"<< A << endl;
	
	Mat B=A.colrang(1,2).clone();
	cout<<"提取A的1-2列:"<<B<<endl;
	
	Mat C=A.rowrange(1,2).clone();
	cout<<"提取A的1-2行:"<<C<<endl;
}

拼接矩阵的时候也有专门的函数:

vconcat(B,C,A); // 等同于A=[B ;C]
hconcat(B,C,A); // 等同于A=[B  C]

示例:

#include 
#include 
#include 
 
using namespace cv;
using namespace std;

int main()
{
	Mat A = (Mat_<double>(3,3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);
	cout <<"原矩阵为:"<< A << endl;

	Mat B=A.colrang(1,1).clone();
	cout<<"提取A的第1列:"<<B<<endl;
	
	Mat C=A.colrang(2,2).clone();
	cout<<"提取A的第2列:"<<B<<endl;
	
	//定义矩阵时先列后行
	Mat D(cv::Size(2, 1), CV_32FC1);

	hconcat(B,C,D);
	cout<<"拼接后的矩阵为:"<<D<<endl;
}

你可能感兴趣的:(C++,矩阵,opencv,c++,提取与拼接)