VS2015+OpenCV4.2.0实现四图拼接

VS2015+OpenCV4.2.0使用stitch函数实现四图拼接

使用平台:VS2015+OpenCV4.2.0
网上见到的教程用的都是OpenCV4.0以下的,然而在新的版本的OpenCV拼接函数有点不一样。我采用的是stitch函数,代码如下:

#include   
#include   
#include "opencv2/core.hpp"  
#include "opencv2/core/utility.hpp"  
#include "opencv2/core/ocl.hpp"  
#include "opencv2/imgcodecs.hpp"  
#include "opencv2/highgui.hpp"  
#include "opencv2/features2d.hpp"  
#include "opencv2/calib3d.hpp"  
#include "opencv2/imgproc.hpp"  
#include"opencv2/flann.hpp"  
#include "opencv2/features2d.hpp"  
#include 

using namespace cv;
using namespace std;

Stitcher::Mode mode = Stitcher::PANORAMA;
vector<Mat> imgs;
string result_name = "dst1.jpg";
int main(int argc, char * argv[])
{
	Mat img1 = imread("D:\\vsprojects\\pingjie423\\x1.png");
	Mat img2 = imread("D:\\vsprojects\\pingjie423\\x2.png");
	Mat img3 = imread("D:\\vsprojects\\pingjie423\\x3.png");
	Mat img4 = imread("D:\\vsprojects\\pingjie423\\x4.png");
	imshow("p1", img1);
	imshow("p2", img2);
	imshow("p3", img3);
	imshow("p4", img4);
	if (img3.empty() || img4.empty())//本来要确认四张图,偷个懒
	{
		cout << "Can't read image" << endl;
		return -1;
	}
	imgs.push_back(img1);
	imgs.push_back(img2);
	imgs.push_back(img3);
	imgs.push_back(img4);
	Mat pano;
	// 使用stitch函数进行拼接
	Ptr<Stitcher> stitcher = Stitcher::create(mode);
	Stitcher::Status status = stitcher->stitch(imgs, pano);
	if (status != Stitcher::OK)
	{
		cout << "Can't stitch images, error code = " << int(status) << endl;
		return -1;
	}
	imwrite(result_name, pano);
	Mat pano2 = pano.clone();
	imshow("全景图像", pano);
	imwrite("D:\\vsprojects\\pingjie423\\destination.jpg", pano);
}

图片拼接后效果如下

输出四张细胞图片
VS2015+OpenCV4.2.0实现四图拼接_第1张图片

输出图片: VS2015+OpenCV4.2.0实现四图拼接_第2张图片

关于最新的stitch函数的使用可以参考OpenCV官方教程

https://docs.opencv.org/4.2.0/d8/d19/tutorial_stitcher.html

OpenCV 3.2.0版本的参考大神教程

https://blog.csdn.net/hongtao_6/article/details/81910691?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1

你可能感兴趣的:(树莓派)