C++和Opencv4.5 实现全景图像拼接

前言

最近刚下了最新版的opencv4.5,急不可待的试下操作,就用了opencv自带的Stitcher类拼接下图像,结果傻眼了,程序显示Stitcher没有createDefault成员,看了好久,终于找到了解决方法。

Stitcher原理

Stitcher类程序流程:

  1. 对图像特征点进行检测,默认是 surf(Speeded Up Robust Features)算法
  2. 对图像的特征点进行匹配
  3. 得到正确的图像序列。
  4. 求旋转矩阵
  5. 拼接

环境

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的应用

猜你喜欢

转载自blog.csdn.net/baidu_35536188/article/details/109692561