本文實例為大家分享了OpenCV實現多圖像拼接成大圖的具體代碼,供大家參考,具體內容如下
開始嘗試merge函數,具體如下:
定義四個矩陣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函數,這兩個函數opencv本身并沒有。
具體實現如下:
#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(){ //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;}
圖像結果顯示如下:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答
圖片精選