本文主要是介绍opencv全景图拼接,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
简介
基本的拼接方式参见: 《opencv实现图像的拼接功能》
本博文基于取景模式讲解图像拼接。API全面基本的介绍介绍参见:
OpenCV官网:http://docs.opencv.org/modules/stitching/doc/stitching.html
在opencv源代码中stitching.cpp 是简单版图像拼接实例,参见《opencv实现图像的拼接功能》。
stitching_detailed.cpp 复杂全面版图像拼接实例。
简单实例(stitching.cpp)
首先看下,opencv实现图像拼接的最简单实例,这是将stitching.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 = true; //false;
vector<Mat> imgs;
string result_name = "result.jpg";
int parseCmdArgs(int argc, char** argv)
{
for (int i = 1; i < argc; ++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::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);
imshow("show", pano);
cv::waitKey(0);
return 0;
}
效果演示
基于不同模式的全景拼接(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;
}
代码中设置生成结果图为:1:平面, 2:柱面, 3:立体画面。在它提供的复杂版实例:stitching_detailed.cpp,
有如下种类可以选择:
plane|cylindrical|spherical|fisheye|stereographic|compressedPlaneA2B1|
compressedPlaneA1.5B1|compressedPlanePortraitA2B1|compressedPlanePortraitA1.5B1|paniniA2B1|
paniniA1.5B1|paniniPortraitA2B1|paniniPortraitA1.5B1|mercator|transverseMercator
本例中结果展示:
1:平面
2:柱面
3:立体画面
这篇关于opencv全景图拼接的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!