高級拼接API(Stitcher類)

2018-10-24 10:39 更新

目標(biāo)

在本教程中,您將學(xué)習(xí)如何:

  • 使用高級拼接API來提供拼接 cv::Stitcher
  • 了解如何使用預(yù)配置的拼接器配置來使用不同的相機(jī)型號縫合圖像。

Code

本教程代碼如下所示。您也可以從這里下載。

#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ì)信息,請參閱cv :: Stitcher :: Mode。這些配置將設(shè)置多個(gè)拼接器屬性以在預(yù)定義的場景之一中運(yùn)行。在預(yù)定義的配置中創(chuàng)建stitcher之后,您可以通過設(shè)置任何stitcher 屬性來調(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ò)誤代碼,請參閱cv :: Stitcher ::狀態(tài)文檔。

相機(jī)型號

目前有2種相機(jī)模型在stitching pipeline中實(shí)現(xiàn)。

相機(jī)模型對于創(chuàng)建照相機(jī)拍攝的照片全景照片非常有用,而基于仿射的模型可用于針對專門設(shè)備拍攝的掃描和對象進(jìn)行拍攝。

注意
cv :: Stitcher的某些詳細(xì)設(shè)置可能沒有意義。特別是在實(shí)施異形模型時(shí),不要混合實(shí)現(xiàn)仿射模型和類的類,當(dāng)他們使用不同的轉(zhuǎn)換。

試試看

如果您啟用構(gòu)建樣品,您可以找到二進(jìn)制文件build/bin/cpp-example-stitching。這個(gè)例子是一個(gè)控制臺(tái)應(yīng)用程序,運(yùn)行它沒有參數(shù)來查看幫助。opencv_extra提供一些樣本數(shù)據(jù),用于測試所有可用的配置

嘗試全景模式運(yùn)行:

./cpp-example-stitching --mode panorama <path to opencv_extra>/testdata/stitching/boat*

高級拼接API

嘗試掃描模式運(yùn)行(數(shù)據(jù)集從家庭級掃描儀):

./cpp-example-stitching --mode scans <path to opencv_extra>/testdata/stitching/newspaper*

高級拼接API

或(專業(yè)書籍掃描儀的數(shù)據(jù)集):

./cpp-example-stitching --mode scans <path to opencv_extra>/testdata/stitching/budapest*

高級拼接API

注意
上面的示例預(yù)計(jì)POSIX平臺(tái),在Windows上,您必須顯式提供所有文件名(例如boat1.jpg boat2.jpg...),因?yàn)閃indows命令行不支持*擴(kuò)展。

也可以看看

如果你想學(xué)習(xí)stitching pipeline內(nèi)部或者你想詳細(xì)配置實(shí)驗(yàn)看stitching_detailed.cpp在opencv/samples/cpp文件夾中。


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號