opencv图片全景拼接详解
簡介
基本的拼接方式參見: 《opencv實現圖像的拼接功能》
本博文基于取景模式講解圖像拼接。API全面基本的介紹介紹參見:
opencv官網:http://docs.opencv.org/modules/stitching/doc/stitching.html
在opencv源代碼中stitching.cpp 是簡單版圖像拼接實例,參見《opencv實現圖像的拼接功能》。
stitching_detailed.cpp 復雜全面版圖像拼接實例。
簡單實例(stitching.cpp)
首先看下,opencv實現圖像拼接的最簡單實例,這是將stitching.cpp裁剪到最簡單的代碼
#include <iostream> ?
#include <fstream> ?
#include "opencv2/highgui/highgui.hpp" ?
#include "opencv2/stitching/stitcher.hpp" ?
? ?
using namespace std; ?
using namespace cv; ?
? ?
bool try_use_gpu = true; //false; ?
vector<Mat> imgs; ?
string result_name = "result.jpg"; ?
? ?
int parseCmdArgs(int argc, char** argv)
{ ?
??? for (int i = 1; i < argc; ++i){ ?
??????? Mat img = imread(argv[i]); ?
??????? if (img.empty()){ ?
??????????? cout << "Can't read image '" << argv[i] << "'\n"; ?
??????????? return -1; ?
??????? } ?
??????? imgs.push_back(img); ?
??????? imshow(argv[i], img); ?
??? } ?
??? return 0; ?
} ?
? ?
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 = " << int(status) << endl; ?
??????? return -1; ?
??? } ?
? ?
??? imwrite(result_name, pano); ?
??? imshow("show", pano); ?
??? cv::waitKey(0); ?
??? return 0; ?
}
效果演示
?基于不同模式的全景拼接(stitching_detailed.cpp精簡版)
代碼具體代碼如下:
#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"; ?
? ?
int parseCmdArgs(int argc, char** argv)
{? //輸入的圖片全部填充到容器imgs中,并將輸入的圖片顯示出來。
??? for (int i = 1; i < argc-1; ++i)
??? { ?
??????? Mat img = imread(argv[i]); ?
??????? if (img.empty()){ ?
??????????? cout << "Can't read image '" << argv[i] << "'\n"; ?
??????????? return -1; ?
??????? } ?
??????? imgs.push_back(img); ?
? ?
??????? imshow(argv[i], img); ?
??? } ?
??? return 0; ?
} ?
? ?
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對象。
? ?
??? if(argv[4][0] == '1')
??? {? //1:平面拼接
??????? PlaneWarper* cw = new PlaneWarper(); ?
??????? stitcher.setWarper(cw); ?
??? }
??? else if(argv[4][0] == '2')
??? {//2:柱面 拼接
??????? SphericalWarper* cw = new SphericalWarper();???? ?
??????? stitcher.setWarper(cw); ?
??? }
??? else if(argv[4][0] == '3')
??? {//3:立體畫面拼接
??????? StereographicWarper *cw = new cv::StereographicWarper();???? ?
??????? stitcher.setWarper(cw); ?
??? } ?
? ?
??? //使用Surf算法來尋找特征點,支持Surf和Orb兩種方式
??? detail::SurfFeaturesFinder *featureFinder = new detail::SurfFeaturesFinder(); ?
??? stitcher.setFeaturesFinder(featureFinder); ?
? ?
??? /*匹配給定的圖像和估計相機的旋轉*/ ?
??? Stitcher::Status status = stitcher.estimateTransform(imgs);? //另一種方式來實現拼接
??? if (status != Stitcher::OK) ?
??? { ?
??????? cout << "Can't stitch images, error code = " << int(status) << endl; ?
??????? return -1; ?
??? } ?
? ?
??? /*生成全景圖像*/ ?
??? status = stitcher.composePanorama(pano); ?
??? if (status != Stitcher::OK) ?
??? { ?
??????? cout << "Can't stitch images, error code = " << int(status) << endl; ?
??????? return -1; ?
??? } ?
? ?
??? imwrite(result_name, pano); ?
??? imshow("show", pano); ?
??? cv::waitKey(0); ?
??? return 0; ?
}?
代碼中設置生成結果圖為:1:平面, 2:柱面, 3:立體畫面。在它提供的復雜版實例:stitching_detailed.cpp,
有如下種類可以選擇:
plane|cylindrical|spherical|fisheye|stereographic|compressedPlaneA2B1|
compressedPlaneA1.5B1|compressedPlanePortraitA2B1|compressedPlanePortraitA1.5B1|paniniA2B1|
paniniA1.5B1|paniniPortraitA2B1|paniniPortraitA1.5B1|mercator|transverseMercator
本例中結果展示:
1:平面
2:柱面
3:立體畫面
總結
以上是生活随笔為你收集整理的opencv图片全景拼接详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图像低频高频区域分离
- 下一篇: 关于Retinex图像增强算法的一些新学