图像拼接stitching是OpenCV2.4.0出现的一个新模块,所有的相关函数都被封装在Stitcher类当中。关于Stitcher类的详细介绍,可以参考: http://docs.opencv.org/2.4.2/modules/stitching/doc/high_level.html?highlight=stitcher#stitcher。

这个类当中我们主要用到的成员函数有createDefault,用于创建缺省参数的stitcher;estimateTransform,用于 生成最后的拼接图像;而对于composePanorama和stitch,文档中提示如果对stitching的整过过程不熟悉的话,最好不要使用以上 两个函数,直接使用stitch就行了。整个拼接的算法实现过程十分复杂,其中涉及到图像特征点的提取和匹配、摄像机的校准、图像融合、图像的变形、曝光 补偿等算法的结合。

说得这么复杂,但实际上这些模块的接口调用,OpenCV都为我们搞定了,我们只需要调用createDefault函数生成默认的参数,再使用stitch函数进行拼接就ok了。

图像拼接的实例代码如下,在VS2013平台上运行成功:

/*M///////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                          License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/#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"; // 默认输出文件名及格式void printUsage();
int parseCmdArgs(int argc, char** argv);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 = " << status << endl;return -1;}imwrite(result_name, pano);return 0;
}void printUsage()
{cout <<"Rotation model images stitcher.\n\n""stitching img1 img2 [...imgN]\n\n""Flags:\n""  --try_use_gpu (yes|no)\n""      Try to use GPU. The default value is 'no'. All default values\n""      are for CPU mode.\n""  --output <result_img>\n""      The default is 'result.jpg'.\n";
}int parseCmdArgs(int argc, char** argv)
{if (argc == 1){printUsage();return -1;}for (int i = 1; i < argc; ++i){if (string(argv[i]) == "--help" || string(argv[i]) == "/?"){printUsage();return -1;}else if (string(argv[i]) == "--try_use_gpu") // 默认不使用gpu加速{if (string(argv[i + 1]) == "no")try_use_gpu = false;else if (string(argv[i + 1]) == "yes")try_use_gpu = true;else{cout << "Bad --try_use_gpu flag value\n";return -1;}i++;}else if (string(argv[i]) == "--output") // 若定义了输出图像名,则更改result_name{result_name = argv[i + 1];i++;}else{Mat img = imread(argv[i]);if (img.empty()){cout << "Can't read image '" << argv[i] << "'\n";return -1;}imgs.push_back(img);}}return 0;
}

这里在自己的机子上实现简单的图像拼接,程序生成的可执行文件名为imageStitching.exe,如果直接运行程序将直接退出,需要在cmd中进行以下操作:

找到imageStitching.exe所在的目录,在终端里输入imageStitching+被拼接的图像路径(若与imageStitching.exe在同个文件夹,直接输入图像的名字和后缀名即可),如这里输入:imageStitching 1.jpg 2.jpg 3.jpg。

若输入:–output 4.jpg,就会把1.jpg、2.jpg和3.jpg 进行拼接,而输出的文件是imageStitching.exe路径下的4.jpg,由于在例程默认输出为result.jpg,可以不用设置output。
由于选取的图像在拍摄时并不平行,能拼接出这种效果的图像已是不错:

1.jpg:

2.jpg:

3.jpg:

拼接输出结果result.jpg:

代码中使用函数:Stitcher::Status status = stitcher.stitch(imgs, pano);就能得出一个傻瓜拼接结果…如之前所提到的,这其中涉及到很多算法的实现过程,可以看到图像拼接算法是一个值得深入的领域。更多的算法可参考:
http://academy.nearsoft.com/project-updates/makingapanoramapicture

OpenCV2学习笔记(十六):Stitching图像拼接相关推荐

  1. 电脑安装python3.74_python3.4学习笔记(十六) windows下面安装easy_install和pip教程

    python3.4学习笔记(十六) windows下面安装easy_install和pip教程 easy_install和pip都是用来下载安装Python一个公共资源库PyPI的相关资源包的 首先安 ...

  2. Polyworks脚本开发学习笔记(十六)-用C#进行Polyworks二次开发

    Polyworks脚本开发学习笔记(十六)-用C#进行Polyworks二次开发 Polyworks支持C#二次开发,用对应的SDK文档试着做一下开发样例. 新建一个C#项目,在解决方案中右键添加引用 ...

  3. Mr.J-- jQuery学习笔记(十六)--展开和收起动画折叠菜单的实现

    之前写过动画的隐藏与显示:Mr.J-- jQuery学习笔记(十四)--动画显示隐藏 动画隐藏与显示的一个小demo--对联广告:Mr.J-- jQuery学习笔记(十五)--实现页面的对联广告 与动 ...

  4. C语言结构体变量和结构体数组-学习笔记(十六)

    一.结构体变量 1.结构体概念 将不同类型的数据组合成一个有机的整体即为结构体.结构体由许多组织在一起的数据项组成,这些数据项不需要属于同一类型. 2.结构体类型及结构体变量定义 (1)结构体类型声明 ...

  5. 【theano-windows】学习笔记十六——深度信念网络DBN

    前言 前面学习了受限玻尔兹曼机(RBM)的理论和搭建方法, 如果稍微了解过的人, 肯定知道利用RBM可以堆叠构成深度信念网络(deep belief network, DBN)和深度玻尔兹曼机(dee ...

  6. MonoRail学习笔记十六:AJax在MonoRail中的使用

    AJax几乎成了web2.0的一个代表,Java和Asp.net中都提供了一些AJax操作的控件.在MonoRail中也同样提供了AJax操作的共通类:AJaxHelper AJaxHelper可以指 ...

  7. JavaScript权威设计--CSS(简要学习笔记十六)

    1.Document的一些特殊属性 document.lastModified document.URL document.title document.referrer document.domai ...

  8. Jenkins deploy to container部署war到tomcat(学习笔记十六)

    /** * lihaibo * 文章内容都是根据自己工作情况实践得出. *如有错误,请指正 * 版权声明:本博客欢迎转发,但请保留原作者信息! http://www.cnblogs.com/horiz ...

  9. IOS学习笔记十六(NSString和NSMutableString)

    1.NSString和NSMutableString NSString是不变字符串类,有点像java里面的String,NSMutableString是可变字符串类,有点类似java里面的String ...

  10. 最优化学习笔记(十六)——拟牛顿法(2)

    Hessian矩阵逆矩阵的近似 一.拟牛顿法的基本思路 令H0,H1,H2,-\boldsymbol{H_0,H_1, H_2}, \dots表示Hessian矩阵逆矩阵F(x(k))−1\bolds ...

最新文章

  1. python如何安装matplotlib_详解python安装matplotlib库三种失败情况
  2. Linux操作系统下Oracle主要监控工具介绍
  3. 使用HttpHandler解析并展示PDF文档内容
  4. Hive Cilent数据操作
  5. 《数据结构与抽象:Java语言描述(原书第4版)》一P.4.1 标识类
  6. 死磕算法!35 篇算法设计实例+6 本超赞好书打包送你
  7. java2017下载_Download Java for OS X 2017-001
  8. Flink SQL中的函数
  9. 【转】vs2010下创建webservice
  10. 反三角函数在线计算机,反三角函数(反正弦,反余弦,反正切,反余切,反正割,反余割)在线计算器_三贝计算网_23bei.com...
  11. 软件测试 测试用例笔记
  12. 让xcode5能使用ios6.1模拟器
  13. 免费下载 客道巴巴文档 教程
  14. 硅谷的精神火种——多元化和天马行空(三)
  15. 获取连接到wifi热点的手机信息。
  16. 35岁的程序员:第14章,前奏
  17. python-格式化写入xml文件
  18. 录制课程用什么软件好?3款超好用的课程视频录课软件
  19. 倪光南院士 你该检讨一下了
  20. JAVA计算机毕业设计桌游店会员管理系统Mybatis+系统+数据库+调试部署

热门文章

  1. Solidworks 2011 软件下载、安装和破解 图文教程
  2. 淘沙河 - 清凉解暑之电风扇
  3. IntelliJ IDEA 学习笔记 - @Autowired 报红波浪线
  4. 压缩包打开密码有什么方法破解
  5. 计算机屏幕上的框的英语,电脑打开steam平台弹出一个英文框的解决方法
  6. 鸿蒙幻境这么做,天下手游鸿蒙幻境玩法详细介绍 鸿蒙幻境规则及奖励一览
  7. 魔百盒配置服务器信息,中国移动魔百盒的使用设置?
  8. 2022/9/16-2022/9/20
  9. IT工程师造“飞机维修安全卫士”,百度飞桨EasyDL担当“算法团队”
  10. AlphaCode(编程版阿法狗)