1. 程式人生 > >OpenCV使用Stitcher類生成全景影象

OpenCV使用Stitcher類生成全景影象

一,原圖片

 

二,原始碼

#include <fstream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/stitching/stitcher.hpp"
#include <iostream>

using namespace cv;
using namespace std;

vector<Mat> imgs; //儲存拼接的原始影象向量

//匯入所有原始拼接影象函式
void parseCmdArgs()
{
	//匯入拼接影象
	CString C_str;
	CString C_readImgPath;
	C_readImgPath = "D:\\opencv全景拼接圖";
	int imgNum = 5;
	for (int i = 1; i < 6; i++)
	{
		C_str.Format(_T("%s\\%d.jpg"), C_readImgPath, i);   //讀取影象路徑
		string strImgPath = (CW2A(C_str.GetString()));
		Mat img = imread(strImgPath);//讀取圖片
		if (img.empty())
		{
			cout << "Can't read image '" << strImgPath << "'\n";
		}
		imgs.push_back(img);
	}

	Mat pano;
	Stitcher stitcher = Stitcher::createDefault(false);
	Stitcher::Status status = stitcher.stitch(imgs, pano);//拼接
	if (status != Stitcher::OK) //判斷拼接是否成功
	{
		cout << "Can't stitch images, error code = " << int(status) << endl;
		return;
	}
	namedWindow("全景拼接", 0);
	imshow("全景拼接", pano);
	imwrite("D:\\全景拼接.jpg", pano);
	waitKey();
	return;
}

三,執行結果

原文地址:https://blog.csdn.net/dcrmg/article/details/52653366

歡迎掃碼關注我的微信公眾號