OpenCV2学习笔记(十六):Stitching图像拼接

图像拼接stitching是OpenCV2.4.0出现的一个新模块,所有的相关函数都被封装在Stitcher类当中。关于Stitcher类的详细介绍,可以参考: 。

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

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

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

: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.//// By downloading, copying, installing or using the software you agree to this license.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.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.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 causedany theory of liability, whether in contract, strict liability,the use of this software, even if advised of the possibility of such damage.////M*/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 <<;}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。

人,都有不能称心如意的时候,都有愿望落空的窘迫,

OpenCV2学习笔记(十六):Stitching图像拼接

相关文章:

你感兴趣的文章:

标签云: