企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
##Stitching图像拼接 图像拼接stitching是OpenCV2.4.0出现的一个新模块,所有的相关函数都被封装在Stitcher类当中。关于Stitcher类的详细介绍,可以参考: [http://docs.opencv.org/2.4.2/modules/stitching/doc/high_level.html?highlight=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。 ![](https://box.kancloud.cn/2015-12-30_5683a765a9b4f.jpg) 若输入:–output 4.jpg,就会把1.jpg、2.jpg和3.jpg 进行拼接,而输出的文件是imageStitching.exe路径下的4.jpg,由于在例程默认输出为result.jpg,可以不用设置output。  由于选取的图像在拍摄时并不平行,能拼接出这种效果的图像已是不错: 1.jpg:  ![](https://box.kancloud.cn/2015-12-30_5683a765c82d1.jpg) 2.jpg:  ![](https://box.kancloud.cn/2015-12-30_5683a765de3eb.jpg) 3.jpg:  ![](https://box.kancloud.cn/2015-12-30_5683a7660839f.png) 拼接输出结果result.jpg:  ![](https://box.kancloud.cn/2015-12-30_5683a76638b73.png) 代码中使用函数:`Stitcher::Status status = stitcher.stitch(imgs, pano);`就能得出一个傻瓜拼接结果…如之前所提到的,这其中涉及到很多算法的实现过程,可以看到图像拼接算法是一个值得深入的领域。更多的算法可参考:  [http://academy.nearsoft.com/project-updates/makingapanoramapicture](http://academy.nearsoft.com/project-updates/makingapanoramapicture)