首页  编辑  

OpenCV代码示例:自动图像拼接

Tags: /超级猛料/Picture.图形图像编程/   Date Created:
// StitchPicture.cpp : 定义控制台应用程序的入口点。
// 编译后的使用方法: 见程序自带说明

#include "stdafx.h"

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/stitching/stitcher.hpp>
using namespace std;
using namespace cv;
bool try_use_gpu = true;
vector<Mat> imgs;
int main(int argc, char * argv[])
{
if (argc < 4) {
printf("Usage: StitchPicture [result.jpg p1.jpg p2.jpg p3.jpg ...]\r\n");
return -1;
}

try {
string result_name = argv[1];

for (int i = 2; i < argc; i++) {
Mat img = imread(argv[i]);
if (!img.empty()) {
imgs.push_back(img);
}
}

Stitcher stitcher = Stitcher::createDefault(try_use_gpu);
// 使用stitch函数进行拼接
Mat pano;

/*
//拼接风格
//PlaneWarper* cw = new PlaneWarper();  // 平面拼接
SphericalWarper* cw = new SphericalWarper();  // 柱形拼接,默认
// StereographicWarper* cw = new StereographicWarper();  //寻找特征点的方式
stitcher.setWarper(cw);
detail::SurfFeaturesFinder* featureFinder = new detail::SurfFeaturesFinder();
stitcher.setFeaturesFinder(featureFinder);
//匹配图像及估计相机旋转
Stitcher::Status status = stitcher.estimateTransform(imgs);
if (status != Stitcher::OK)
{
cout << "cannot stitch images!" << endl;
}
//拼接图像
status = stitcher.composePanorama(pano);
if (status != Stitcher::OK)
{
cout << "Can't stitch images, error code = " << int(status) << endl;
}
*/


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);
// if (waitKey() == 27)
return 0;
}
catch (exception E) {
printf(E.what());
return -1;
}
}