环境:
运行环境:Win10 x64
OpenCV 版本: 4.5.1
源代码:
#include <iostream>
#include <vector>
#include <chrono>
#include <string>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/stitching.hpp>
int main()
{
const int image_amount{ 5 };
const std::string parent_dir{ "./image/test/" };
std::vector<cv::Mat> imgs;
for(int i = 0; i < image_amount - 1; i++)
imgs.emplace_back(cv::imread(parent_dir + 'p' + std::to_string(i) + ".jpg"));
cv::Mat res;
auto stitcher = cv::Stitcher::create();
auto start = std::chrono::steady_clock::now();
auto status = stitcher->stitch(imgs, res); // 使用 stitch 函数进行拼接
auto end = std::chrono::steady_clock::now();
auto elapsed_seconds = std::chrono::duration<double>(end - start);
std::cout << "Elapsed time: " << elapsed_seconds.count() << " seconds.\n";
if (status == cv::Stitcher::OK) {
cv::imshow("Stitching Result", res);
cv::imwrite(parent_dir + "result.jpg", res);
} else {
std::cout << "Can't stitch images, error code = " << int(status) << std::endl;
return -1;
}
cv::waitKey();
}
素材:
![](https://img-blog.csdnimg.cn/20210222091542253.jpg)
![](https://img-blog.csdnimg.cn/20210222091547222.jpg)
![](https://img-blog.csdnimg.cn/20210222091552134.jpg)
![](https://img-blog.csdnimg.cn/20210222091555453.jpg)
![](https://img-blog.csdnimg.cn/20210222091603300.jpg)
运行结果:
![](https://img-blog.csdnimg.cn/20210222091607907.jpg)