日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

opencv图片全景拼接详解

發布時間:2025/7/25 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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图片全景拼接详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。