##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。

若輸入:–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](http://academy.nearsoft.com/project-updates/makingapanoramapicture)
- 前言
- Win8.1下OpenCV2.4.9+Qt5.3.2開發環境搭建
- OpenCV2學習筆記(一)
- OpenCV2學習筆記(二)
- OpenCV2學習筆記(三)
- OpenCV2學習筆記(四)
- OpenCV2學習筆記(五)
- OpenCV2學習筆記(六)
- OpenCV2學習筆記(七)
- OpenCV2學習筆記(八)
- OpenCV2學習筆記(九)
- OpenCV2學習筆記(十)
- OpenCV2學習筆記(十一)
- OpenCV2學習筆記(十二)
- OpenCV2學習筆記(十三)
- OpenCV2學習筆記(十四)
- OpenCV2學習筆記(十五)
- OpenCV2學習筆記(十六)
- OpenCV2學習筆記(十七)
- OpenCV2學習筆記(十八)
- OpenCV2學習筆記(十九)
- OpenCV2學習筆記(二十)
- OpenCV2學習筆記(二十一)
- OpenCV2學習筆記(二十二)