按行合并
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
Mat mergeRows(Mat A, Mat B)
{
CV_ASSERT(A.cols == B.cols&&A.type() == B.type());
int totalRows = A.rows + B.rows;
Mat mergedDescriptors(totalRows, A.cols, A.type());
Mat submat = mergedDescriptors.rowRange(0, A.rows);
A.copyTo(submat);
submat = mergedDescriptors.rowRange(A.rows, totalRows);
B.copyTo(submat);
return mergedDescriptors;
}
int main()
{
Mat B = (cv::Mat_<double>(1, 2) << 1,2);
Mat C = (cv::Mat_<double>(2, 2) << 3, 2,3,2);
Mat A = mergeRows(B,C);
cout << "A:" << A << endl;
}
测试结果如图1
按列合并:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
Mat mergeRows(Mat A, Mat B)
{
assert(A.rows == B.rows&&A.type() == B.type());
int totalCols = A.cols + B.cols;
Mat mergedDescriptors(A.rows, totalCols, A.type());
Mat submat = mergedDescriptors.colRange(0, A.cols);
A.copyTo(submat);
submat = mergedDescriptors.colRange(A.cols, totalCols);
B.copyTo(submat);
return mergedDescriptors;
}
int main()
{
Mat B = (cv::Mat_<double>(2, 1) << 1,2);
Mat C = (cv::Mat_<double>(2, 2) << 3, 2,3,2);
Mat A = mergeRows(B,C);
cout << "A:" << A << endl;
}
测试结果如图2,