开始尝试merge函数,具体如下:
定义四个矩阵A,B,C,D。得到矩阵combine。
- <span style="font-size:18px">#include<iostream>
- #include <core/core.hpp>
- #include <opencv2/imgproc/imgproc.hpp>
- #include <opencv2/highgui/highgui.hpp>
- using namespace std;
- using namespace cv;
- int main()
- {
- cv::Mat a = (cv::Mat_<int>(2,2)<<1,2,3,4);
- cv::Mat b = (cv::Mat_<int>(2,2)<<5,6,7,8);
- cv::Mat c = (cv::Mat_<int>(2,2)<<9,10,11,12);
- cv::Mat d = (cv::Mat_<int>(2,2)<<13,14,15,16);
- std::vector<cv::Mat> v1;
- v1.push_back(a);
- v1.push_back(b);
- v1.push_back(c);
- v1.push_back(d);
- cv::Mat combine;
- cv::merge(v1, combine);
- cout << "combine=" <<combine<< endl;
- cout<<"Size of combine:"<<combine.size()<<endl;
-
- system("pause");
- return 0;
- }</span>
#include<iostream>
#include <core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
cv::Mat a = (cv::Mat_<int>(2,2)<<1,2,3,4);
cv::Mat b = (cv::Mat_<int>(2,2)<<5,6,7,8);
cv::Mat c = (cv::Mat_<int>(2,2)<<9,10,11,12);
cv::Mat d = (cv::Mat_<int>(2,2)<<13,14,15,16);
std::vector<cv::Mat> v1;
v1.push_back(a);
v1.push_back(b);
v1.push_back(c);
v1.push_back(d);
cv::Mat combine;
cv::merge(v1, combine);
cout << "combine=" <<combine<< endl;
cout<<"Size of combine:"<<combine.size()<<endl;
system("pause");
return 0;
}
结果如下:

![]()
显然,不是我们需要的结果。
尝试hconcat和vconcat函数,这两个函数OpenCV本身并没有。
详细介绍参见hconcat和vconcat。
![]()
具体实现如下:
- <span style="font-size:18px">#include <iostream>
- #include <core/core.hpp>
- #include <opencv2/imgproc/imgproc.hpp>
- #include <opencv2/highgui/highgui.hpp>
- using namespace std;
- using namespace cv;
- int main()
- {
- cv::Mat a = (cv::Mat_<int>(2,2)<<1,2,3,4);
- cv::Mat b = (cv::Mat_<int>(2,2)<<5,6,7,8);
- cv::Mat c = (cv::Mat_<int>(2,2)<<9,10,11,12);
- cv::Mat d = (cv::Mat_<int>(2,2)<<13,14,15,16);
- Mat combine,combine1,combine2;
- hconcat(a,b,combine1);
- hconcat(c,d,combine2);
- vconcat(combine1,combine2,combine);
-
-
- cout<<"Combine=:"<<combine<<endl;
- system("pause");
- return 0;
- }</span>
#include <iostream>
#include <core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
cv::Mat a = (cv::Mat_<int>(2,2)<<1,2,3,4);
cv::Mat b = (cv::Mat_<int>(2,2)<<5,6,7,8);
cv::Mat c = (cv::Mat_<int>(2,2)<<9,10,11,12);
cv::Mat d = (cv::Mat_<int>(2,2)<<13,14,15,16);
Mat combine,combine1,combine2;
hconcat(a,b,combine1);
hconcat(c,d,combine2);
vconcat(combine1,combine2,combine);
//namedWindow("Combine",CV_WINDOW_AUTOSIZE);
//imshow("Combine",combine);
cout<<"Combine=:"<<combine<<endl;
system("pause");
return 0;
}
结果:

![]()
图像拼接实现
- #include <iostream>
- #include <core/core.hpp>
- #include <opencv2/imgproc/imgproc.hpp>
- #include <opencv2/highgui/highgui.hpp>
- using namespace std;
- using namespace cv;
- int main()
- {
-
-
-
-
- Mat combine,combine1,combine2;
- Mat a=imread("1.jpg");
- Mat b=imread("2.jpg");
- Mat c=imread("3.jpg");
- Mat d=imread("4.jpg");
- hconcat(a,b,combine1);
- hconcat(c,d,combine2);
- vconcat(combine1,combine2,combine);
- namedWindow("Combine",CV_WINDOW_AUTOSIZE);
- imshow("Combine",combine);
- waitKey(0);
-
- system("pause");
- return 0;
- }
#include <iostream>
#include <core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
//cv::Mat a = (cv::Mat_<int>(2,2)<<1,2,3,4);
//cv::Mat b = (cv::Mat_<int>(2,2)<<5,6,7,8);
//cv::Mat c = (cv::Mat_<int>(2,2)<<9,10,11,12);
//cv::Mat d = (cv::Mat_<int>(2,2)<<13,14,15,16);
Mat combine,combine1,combine2;
Mat a=imread("1.jpg");
Mat b=imread("2.jpg");
Mat c=imread("3.jpg");
Mat d=imread("4.jpg");
hconcat(a,b,combine1);
hconcat(c,d,combine2);
vconcat(combine1,combine2,combine);
namedWindow("Combine",CV_WINDOW_AUTOSIZE);
imshow("Combine",combine);
waitKey(0);
//cout<<"Combine=:"<<combine<<endl;
system("pause");
return 0;
}
图像结果显示如下:


![]()
![]()