W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
在本教程中,您將學(xué)習(xí)如何:
本教程代碼如下所示。您也可以從這里下載。
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/stitching.hpp"
#include <iostream>
using namespace std;
using namespace cv;
bool try_use_gpu = false;
bool divide_images = false;
Stitcher::Mode mode = Stitcher::PANORAMA;
vector<Mat> imgs;
string result_name = "result.jpg";
void printUsage(char** argv);
int parseCmdArgs(int argc, char** argv);
int main(int argc, char* argv[])
{
int retval = parseCmdArgs(argc, argv);
if (retval) return -1;
Mat pano;
Ptr<Stitcher> stitcher = Stitcher::create(mode, 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);
cout << "stitching completed successfully\n" << result_name << " saved!";
return 0;
}
void printUsage(char** argv)
{
cout <<
"Images stitcher.\n\n" << "Usage :\n" << argv[0] <<" [Flags] img1 img2 [...imgN]\n\n"
"Flags:\n"
" --d3\n"
" internally creates three chunks of each image to increase stitching success"
" --try_use_gpu (yes|no)\n"
" Try to use GPU. The default value is 'no'. All default values\n"
" are for CPU mode.\n"
" --mode (panorama|scans)\n"
" Determines configuration of stitcher. The default is 'panorama',\n"
" mode suitable for creating photo panoramas. Option 'scans' is suitable\n"
" for stitching materials under affine transformation, such as scans.\n"
" --output <result_img>\n"
" The default is 'result.jpg'.\n\n"
"Example usage :\n" << argv[0] << " --d3 --try_use_gpu yes --mode scans img1.jpg img2.jpg";
}
int parseCmdArgs(int argc, char** argv)
{
if (argc == 1)
{
printUsage(argv);
return -1;
}
for (int i = 1; i < argc; ++i)
{
if (string(argv[i]) == "--help" || string(argv[i]) == "/?")
{
printUsage(argv);
return -1;
}
else if (string(argv[i]) == "--try_use_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]) == "--d3")
{
divide_images = true;
}
else if (string(argv[i]) == "--output")
{
result_name = argv[i + 1];
i++;
}
else if (string(argv[i]) == "--mode")
{
if (string(argv[i + 1]) == "panorama")
mode = Stitcher::PANORAMA;
else if (string(argv[i + 1]) == "scans")
mode = Stitcher::SCANS;
else
{
cout << "Bad --mode flag value\n";
return -1;
}
i++;
}
else
{
Mat img = imread(argv[i]);
if (img.empty())
{
cout << "Can't read image '" << argv[i] << "'\n";
return -1;
}
if (divide_images)
{
Rect rect(0, 0, img.cols / 2, img.rows);
imgs.push_back(img(rect).clone());
rect.x = img.cols / 3;
imgs.push_back(img(rect).clone());
rect.x = img.cols / 2;
imgs.push_back(img(rect).clone());
}
else
imgs.push_back(img);
}
}
return 0;
}
最重要的代碼部分是:
Mat pano;
Ptr<Stitcher> stitcher = Stitcher::create(mode, 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;
}
創(chuàng)建了一個(gè)新的stitcher實(shí)例,并且cv :: Stitcher :: stitch將完成所有的努力。
cv :: Stitcher :: create可以在預(yù)定義的配置(參數(shù)mode)之一中創(chuàng)建拼接器。有關(guān)詳細(xì)信息,請(qǐng)參閱cv :: Stitcher :: Mode。這些配置將設(shè)置多個(gè)拼接器屬性以在預(yù)定義的場(chǎng)景之一中運(yùn)行。在預(yù)定義的配置中創(chuàng)建stitcher之后,您可以通過(guò)設(shè)置任何stitcher 屬性來(lái)調(diào)整stitcher。
如果你有cuda設(shè)備cv :: Stitcher可以配置卸載某些操作到GPU。如果您喜歡此配置設(shè)置try_use_gpu為true。OpenCL加速將基于全局OpenCV設(shè)置透明地使用,而不管該標(biāo)志。
Stitching可能會(huì)因?yàn)閹讉€(gè)原因而失敗,您應(yīng)該始終檢查一切是否良好,并導(dǎo)致產(chǎn)生的全景圖被存儲(chǔ)pano。有關(guān)可能的錯(cuò)誤代碼,請(qǐng)參閱cv :: Stitcher ::狀態(tài)文檔。
目前有2種相機(jī)模型在stitching pipeline中實(shí)現(xiàn)。
相機(jī)模型對(duì)于創(chuàng)建照相機(jī)拍攝的照片全景照片非常有用,而基于仿射的模型可用于針對(duì)專(zhuān)門(mén)設(shè)備拍攝的掃描和對(duì)象進(jìn)行拍攝。
如果您啟用構(gòu)建樣品,您可以找到二進(jìn)制文件build/bin/cpp-example-stitching。這個(gè)例子是一個(gè)控制臺(tái)應(yīng)用程序,運(yùn)行它沒(méi)有參數(shù)來(lái)查看幫助。opencv_extra提供一些樣本數(shù)據(jù),用于測(cè)試所有可用的配置
嘗試全景模式運(yùn)行:
./cpp-example-stitching --mode panorama <path to opencv_extra>/testdata/stitching/boat*
嘗試掃描模式運(yùn)行(數(shù)據(jù)集從家庭級(jí)掃描儀):
./cpp-example-stitching --mode scans <path to opencv_extra>/testdata/stitching/newspaper*
或(專(zhuān)業(yè)書(shū)籍掃描儀的數(shù)據(jù)集):
./cpp-example-stitching --mode scans <path to opencv_extra>/testdata/stitching/budapest*
boat1.jpg
boat2.jpg
...),因?yàn)閃indows命令行不支持*
擴(kuò)展。如果你想學(xué)習(xí)stitching pipeline內(nèi)部或者你想詳細(xì)配置實(shí)驗(yàn)看stitching_detailed.cpp在opencv/samples/cpp文件夾中。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: