OpenCV實現(xiàn)多圖像拼接成一張大圖
更新時間:2019年01月15日 11:09:53 作者:call_me_yang
這篇文章主要為大家詳細介紹了OpenCV實現(xiàn)多圖像拼接成一張大圖,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了OpenCV實現(xiàn)多圖像拼接成大圖的具體代碼,供大家參考,具體內(nèi)容如下
開始嘗試merge函數(shù),具體如下:
定義四個矩陣A,B,C,D。得到矩陣combine。
#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函數(shù),這兩個函數(shù)opencv本身并沒有。
具體實現(xiàn)如下:
#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;
}
結果:

圖像拼接實現(xiàn)
#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;
}
圖像結果顯示如下:


以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Unity3D實現(xiàn)經(jīng)典小游戲Pacman
這篇文章主要介紹了基于Unity3D制作一做個經(jīng)典小游戲Pacman,文中的示例代碼講解詳細,對我們學習Unity3D有一定的幫助,感興趣的小伙伴可以了解一下2021-12-12

