#include"opencv2/opencv.hpp"
using namespace cv;
行数相等拼接 同行拼接
Mat comMatR(Mat Matrix1,Mat Matrix2,Mat &MatrixCom)
{
CV_Assert(Matrix1.rows==Matrix2.rows);//行数不相等,出现错误中断
MatrixCom.create(Matrix1.rows,Matrix1.cols+Matrix2.cols,Matrix1.type());
Mat temp=MatrixCom.colRange(0,Matrix1.cols);
Matrix1.copyTo(temp);
Mat temp1=MatrixCom.colRange(Matrix1.cols,Matrix1.cols+Matrix2.cols);
Matrix2.copyTo(temp1);
return MatrixCom;
}
列数相等拼接 同列拼接
Mat comMatC(Mat Matrix1,Mat Matrix2,Mat &MatrixCom)
{
CV_Assert(Matrix1.cols==Matrix2.cols);//列数不相等,出现错误中断
MatrixCom.create(Matrix1.rows+Matrix2.rows,Matrix1.cols,Matrix1.type());
Mat temp=MatrixCom.rowRange(0,Matrix1.rows);
Matrix1.copyTo(temp);
Mat temp1=MatrixCom.rowRange(Matrix1.rows,Matrix1.rows+Matrix2.rows);
Matrix2.copyTo(temp1);
return MatrixCom;
}
int main()
{
VideoCapture capture1(0); //调用1号摄像头
VideoCapture capture2(1); //调用2号摄像头
while(1)
{
Mat frame1,frame2; //定义Mat变量 frame1 frame2
Mat combine; //定义Mat变量 combine 组合后的Mat
capture1>>frame1; //获取图像1
capture2>>frame2; //获取图像2
comMatR(frame1,frame2,combine); //开始拼接
imshow("视频1",combine);//显示拼接后图像
waitKey(30);//延时30MS
}
}
借鉴以下博客
opencv图像拼接,即把两个矩阵合并为一个(vconcat和hconcat)、Mat成员函数size()_mikedadong的博客-CSDN博客_cv::mat vconcat一小菜鸟,大佬们勿笑。