OpenCV2.4.13使用Stitcher类图像拼接生成全景图像
这篇文章不是我写的,看着好玩,转载过来,O(∩_∩)O哈哈~
一,原图片
二,源代码
#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;
}
三,运行结果