#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
}
}
笔者最近在研究双摄像头图像拼接,查阅网上大量资料,笔者只想实现简单的图像拼接,但是网上的大多数资料经过自己的修改或是不修改经常出现内存异常或是其他错误。我也曾试过vconcat,hconcat的拼接方法,但是失败了,最近网上看见一位自己写了的函数,试了一下,成功了,该大神的专栏网站https://blog.csdn.net/mikedadong/article/details/51305640 笔者就是一小菜鸟,大佬们勿笑。