简介
基本的拼接方式参见: 《opencv实现图像的拼接功能》
本博文基于取景模式讲解图像拼接。API全面基本的介绍介绍参见:
opencv官网:http://docs.opencv.org/modules/stitching/doc/stitching.html
在opencv源代码中stitching.cpp 是简单版图像拼接实例,参见《opencv实现图像的拼接功能》。
stitching_detailed.cpp 复杂全面版图像拼接实例。
简单实例(stitching.cpp)
首先看下,opencv实现图像拼接的最简单实例,这是将stitching.cpp裁剪到最简单的代码
基于不同模式的全景拼接(stitching_detailed.cpp精简版)
代码具体代码如下:
#include <iostream>
#include <fstream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/stitching/stitcher.hpp"
using namespace std;
using namespace cv;
bool try_use_gpu = false;
vector<Mat> imgs;
string result_name = "result.jpg";
int parseCmdArgs(int argc, char** argv)
{ //输入的图片全部填充到容器imgs中,并将输入的图片显示出来。
for (int i = 1; i < argc-1; ++i)
{
Mat img = imread(argv[i]);
if (img.empty()){
cout << "Can't read image '" << argv[i] << "'\n";
return -1;
}
imgs.push_back(img);
imshow(argv[i], img);
}
return 0;
}
int main(int argc, char* argv[])
{
int retval = parseCmdArgs(argc, argv);
if (retval) return -1;
Mat pano;
Stitcher stitcher = Stitcher::createDefault(try_use_gpu); //创建一个stitcher对象。
if(argv[4][0] == '1')
{ //1:平面拼接
PlaneWarper* cw = new PlaneWarper();
stitcher.setWarper(cw);
}
else if(argv[4][0] == '2')
{//2:柱面 拼接
SphericalWarper* cw = new SphericalWarper();
stitcher.setWarper(cw);
}
else if(argv[4][0] == '3')
{//3:立体画面拼接
StereographicWarper *cw = new cv::StereographicWarper();
stitcher.setWarper(cw);
}
//使用Surf算法来寻找特征点,支持Surf和Orb两种方式
detail::SurfFeaturesFinder *featureFinder = new detail::SurfFeaturesFinder();
stitcher.setFeaturesFinder(featureFinder);
/*匹配给定的图像和估计相机的旋转*/
Stitcher::Status status = stitcher.estimateTransform(imgs); //另一种方式来实现拼接
if (status != Stitcher::OK)
{
cout << "Can't stitch images, error code = " << int(status) << endl;
return -1;
}
/*生成全景图像*/
status = stitcher.composePanorama(pano);
if (status != Stitcher::OK)
{
cout << "Can't stitch images, error code = " << int(status) << endl;
return -1;
}
imwrite(result_name, pano);
imshow("show", pano);
cv::waitKey(0);
return 0;
}