天天看點

OpenCV 進行圖像融合拼接

環境:

    運作環境: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();
}
           

素材: 

OpenCV 進行圖像融合拼接

p0.jpg

OpenCV 進行圖像融合拼接

p1.jpg

OpenCV 進行圖像融合拼接

p2.jpg

OpenCV 進行圖像融合拼接

p3.jpg

OpenCV 進行圖像融合拼接

p4.jpg

運作結果: 

OpenCV 進行圖像融合拼接

result.jpg