高級拼接API(Stitcher類)

2018-10-24 10:39 更新

目標

在本教程中,您將學習如何:

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

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)建了一個新的stitcher實例,并且cv :: Stitcher :: stitch將完成所有的努力。

cv :: Stitcher :: create可以在預定義的配置(參數(shù)mode)之一中創(chuàng)建拼接器。有關詳細信息,請參閱cv :: Stitcher :: Mode。這些配置將設置多個拼接器屬性以在預定義的場景之一中運行。在預定義的配置中創(chuàng)建stitcher之后,您可以通過設置任何stitcher 屬性來調(diào)整stitcher。

如果你有cuda設備cv :: Stitcher可以配置卸載某些操作到GPU。如果您喜歡此配置設置try_use_gpu為true。OpenCL加速將基于全局OpenCV設置透明地使用,而不管該標志。

Stitching可能會因為幾個原因而失敗,您應該始終檢查一切是否良好,并導致產(chǎn)生的全景圖被存儲pano。有關可能的錯誤代碼,請參閱cv :: Stitcher ::狀態(tài)文檔。

相機型號

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

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

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

試試看

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

嘗試全景模式運行:

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

高級拼接API

嘗試掃描模式運行(數(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

注意
上面的示例預計POSIX平臺,在Windows上,您必須顯式提供所有文件名(例如boat1.jpg boat2.jpg...),因為Windows命令行不支持*擴展。

也可以看看

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


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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號