前言
最近剛下了最新版的opencv4.5,急不可待的試下操作,就用了opencv自帶的Stitcher類拼接下圖像,結果傻眼了,程序顯示Stitcher沒有createDefault成員,看了好久,終于找到了解決方法。
Stitcher原理
Stitcher類程序流程:
- 對圖像特征點進行檢測,默認是 orb(features from accelerated segment test)算法
- 對圖像的特征點進行匹配
- 得到正確的圖像序列。
- 求旋轉矩陣
- 拼接
環境
OpenCV:4.5.0
VS:2019 C++
平臺:Windows 10
代碼演示
#include <iostream>
#include <stdio.h>
#include <opencv2/stitching.hpp>
#include < opencv2\opencv.hpp >
#include <fstream>using namespace cv;
using namespace std;int main()
{vector<Mat> imgs;Mat image1,image2;image1 = imread("C://Users//**//Desktop//1.PNG");image2 = imread("C://Users//**//Desktop//2.PNG");resize(image1, image1, Size(600, 450), 0, 0, INTER_LINEAR);//圖片是截取的,所以使用resize做了尺寸修改resize(image2, image2, Size(600, 450), 0, 0, INTER_LINEAR);imshow("原圖1", image1); imshow("原圖2", image2);imgs.push_back(image1);imgs.push_back(image2);Ptr<Stitcher> stitcher = Stitcher::create();//調用create方法Mat pano;Stitcher::Status status = stitcher->stitch(imgs, pano); // 使用stitch函數進行拼接if (status != Stitcher::OK){cout << "Can't stitch images, error code = " << int(status) << endl;return -1;}// 顯示結果圖像imshow("全景圖像", pano);waitKey(0);
}
結果展示
原圖
結果:
借鑒了以下大佬的文章,附上鏈接
OpenCV3.4.2 實現圖像拼接與融合
OpenCV4中Stitch的應用