OpenCV - 将网格图像拼接成全景图

22

我在 OpenCV 中找到了一些有关全景图像互相拼接的基本工作示例。我还在API 文档中找到一些有用的文档,但是我无法通过提供其他信息来加速处理。

在我的情况下,我生成了一个 20x20 的单帧网格图像集,总共有 400 张图像需要拼接成一张大图。这在现代 PC 上需要耗费大量时间,所以在开发板上可能需要数小时。

是否有任何方法可以告诉 OpenCV 实例有关图像的信息,例如我事先知道所有图像的相对位置,它们将如何显示在网格上?我目前看到的唯一 API 调用是通过 vImg.push_back() 将所有图像不加区别地添加到队列中。


参考资料

  1. Stitching. Image Stitching - OpenCV API Documentation,访问日期 2014-02-26,<http://docs.opencv.org/modules/stitching/doc/stitching.html>
  2. OpenCV Stitching example (Stitcher class, Panorama),访问日期 2014-02-26,<http://feelmare.blogspot.ca/2013/11/opencv-stitching-example-stitcher-class.html>
  • OpenCV中的全景图像拼接技术,访问日期2014-02-26,<http://ramsrigoutham.com/2012/11/22/panorama-image-stitching-in-opencv/>

  • 2
    考虑并行处理,类似于归并排序的方式。 - herohuyongtao
    开发板?你想将它移植到微控制器/可编程逻辑器件上吗? - lucasg
    @georgesl 不会用那么低功耗的东西,但肯定会使用开发板或微型电脑。总的来说,如果能减少所需的处理量,无论使用哪个平台,我都会选择它。 - Cloud
    5个回答

    10

    我曾经参与了拼接管道的工作,虽然我不认为自己是该领域的专家,但是我通过分别调整管道中的每个步骤来获得更好的性能和更好的结果。如您所见, Stitching 类只是这个管道的包装器:拼接管道概述

    一些有趣的部分可以调整,例如缩放步骤(进一步提高分辨率意味着更多的计算时间和更不准确的特征),匹配过程以及(尽管这只是一个猜测)提供良好的相机参数而不是执行估计。这需要在进行拼接之前获取相机参数,但这并不是真的很难。这里是一些参考资料:OpenCV 相机校准和三维重建

    再次说明:我不是专家,这只是基于我作为实习生使用库进行实验的经验!


    能否提取拼接操作使用的参数?我重新审视了这段代码,已经有将近3年的时间了,想知道是否可以使用一堆降采样(低分辨率)的文件副本进行拼接,然后再使用相同的参数来处理更高分辨率的副本,以减少计算时间。 - Cloud
    1
    当然。最终你得到的是一组图像之间关系的同态矩阵,它基本上解释了如何旋转和平移其中一个图像以便“接近”另一个图像。这种变换不受比例影响,因此你可以对降采样的图像执行所有步骤以获得一些加速,然后将最终变换应用于原始图像。请注意,如果你缩小得太多,管道的某些步骤可能会受到影响。 - martinarroyo

    7
    据我所知,目前没有办法在OpenCV引擎中提供除了图像列表之外的其他数据。不过它自己的工作已经很不错了。您可以查看一些示例代码,并测试每个拼接操作需要多长时间。通过我的实验使用4x6、4x8、...、4x20全景重建,所需的CPU时间似乎随着重叠图像的数量增加而增加。我想在现代计算机上,您的情况可能需要至少一分钟的计算时间。
    来源:https://code.ros.org/trac/opencv/browser/trunk/opencv/samples/cpp/stitching.cpp?rev=6682
    1   /*M///////////////////////////////////////////////////////////////////////////////////////
    2   //
    3   //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
    4   //
    5   //  By downloading, copying, installing or using the software you agree to this license.
    6   //  If you do not agree to this license, do not download, install,
    7   //  copy or use the software.
    8   //
    9   //
    10  //                          License Agreement
    11  //                For Open Source Computer Vision Library
    12  //
    13  // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
    14  // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
    15  // Third party copyrights are property of their respective owners.
    16  //
    17  // Redistribution and use in source and binary forms, with or without modification,
    18  // are permitted provided that the following conditions are met:
    19  //
    20  //   * Redistribution's of source code must retain the above copyright notice,
    21  //     this list of conditions and the following disclaimer.
    22  //
    23  //   * Redistribution's in binary form must reproduce the above copyright notice,
    24  //     this list of conditions and the following disclaimer in the documentation
    25  //     and/or other materials provided with the distribution.
    26  //
    27  //   * The name of the copyright holders may not be used to endorse or promote products
    28  //     derived from this software without specific prior written permission.
    29  //
    30  // This software is provided by the copyright holders and contributors "as is" and
    31  // any express or implied warranties, including, but not limited to, the implied
    32  // warranties of merchantability and fitness for a particular purpose are disclaimed.
    33  // In no event shall the Intel Corporation or contributors be liable for any direct,
    34  // indirect, incidental, special, exemplary, or consequential damages
    35  // (including, but not limited to, procurement of substitute goods or services;
    36  // loss of use, data, or profits; or business interruption) however caused
    37  // and on any theory of liability, whether in contract, strict liability,
    38  // or tort (including negligence or otherwise) arising in any way out of
    39  // the use of this software, even if advised of the possibility of such damage.
    40  //
    41  //M*/
    42  
    43  // We follow to these papers:
    44  // 1) Construction of panoramic mosaics with global and local alignment.
    45  //    Heung-Yeung Shum and Richard Szeliski. 2000.
    46  // 2) Eliminating Ghosting and Exposure Artifacts in Image Mosaics.
    47  //    Matthew Uyttendaele, Ashley Eden and Richard Szeliski. 2001.
    48  // 3) Automatic Panoramic Image Stitching using Invariant Features.
    49  //    Matthew Brown and David G. Lowe. 2007.
    50  
    51  #include <iostream>
    52  #include <fstream>
    53  #include "opencv2/highgui/highgui.hpp"
    54  #include "opencv2/stitching/stitcher.hpp"
    55  
    56  using namespace std;
    57  using namespace cv;
    58  
    59  void printUsage()
    60  {
    61      cout <<
    62          "Rotation model images stitcher.\n\n"
    63          "stitching img1 img2 [...imgN]\n\n"
    64          "Flags:\n"
    65          "  --try_use_gpu (yes|no)\n"
    66          "      Try to use GPU. The default value is 'no'. All default values\n"
    67          "      are for CPU mode.\n"
    68          "  --output <result_img>\n"
    69          "      The default is 'result.jpg'.\n";
    70  }
    71  
    72  bool try_use_gpu = false;
    73  vector<Mat> imgs;
    74  string result_name = "result.jpg";
    75  
    76  int parseCmdArgs(int argc, char** argv)
    77  {
    78      if (argc == 1)
    79      {
    80          printUsage();
    81          return -1;
    82      }
    83      for (int i = 1; i < argc; ++i)
    84      {
    85          if (string(argv[i]) == "--help" || string(argv[i]) == "/?")
    86          {
    87              printUsage();
    88              return -1;
    89          }
    90          else if (string(argv[i]) == "--try_gpu")
    91          {
    92              if (string(argv[i + 1]) == "no")
    93                  try_use_gpu = false;
    94              else if (string(argv[i + 1]) == "yes")
    95                  try_use_gpu = true;
    96              else
    97              {
    98                  cout << "Bad --try_use_gpu flag value\n";
    99                  return -1;
    100             }
    101             i++;
    102         }
    103         else if (string(argv[i]) == "--output")
    104         {
    105             result_name = argv[i + 1];
    106             i++;
    107         }
    108         else
    109         {
    110             Mat img = imread(argv[i]);
    111             if (img.empty())
    112             {
    113                 cout << "Can't read image '" << argv[i] << "'\n";
    114                 return -1;
    115             }
    116             imgs.push_back(img);
    117         }
    118     }
    119     return 0;
    120 }
    121 
    122 
    123 int main(int argc, char* argv[])
    124 {
    125     int retval = parseCmdArgs(argc, argv);
    126     if (retval) return -1;
    127 
    128     Mat pano;
    129     Stitcher stitcher = Stitcher::createDefault(try_use_gpu);
    130     Stitcher::Status status = stitcher.stitch(imgs, pano);
    131 
    132     if (status != Stitcher::OK)
    133     {
    134         cout << "Can't stitch images, error code = " << status << endl;
    135         return -1;
    136     }
    137 
    138     imwrite(result_name, pano);
    139     return 0;
    140 }
    141 
    142 
    

    5

    10
    这并不是一个答案,而只是一个链接。您应该在这里解释答案,并使用链接作为参考。如果链接失效,您所写的答案将毫无用处。 - Matthew Bakaitis
    你好,这里是2023年... ...链接出了问题 :) - undefined

    3
    考虑在Opencv拼接程序中启用GPU使用:
    bool try_use_gpu = true;
    Stitcher myStitcher = Stitcher::createDefault(try_use_gpu); 
    Stitcher::Status status = myStitcher.stitch(Imgs, pano);
    

    2
    如果您知道图像的相对位置,似乎可以将问题分解为子问题,并通过了解问题的子结构可能减少计算负载。基本上将图像集合分成相邻的4个图像组,处理帧,然后继续使用相同的思路处理生成的图像,直到到达全景图。话虽如此,我最近才开始使用这个opencv工具集。我知道这是一个非常简单的想法,但它可能对某些人有用。

    我最终通过拼接4x4网格来创建逐渐变大的子图像,然后再将它们拼接起来,以此类推。虽然会有轻微的质量损失,但由于许多图像看起来相似,这可以避免灾难性的伪影,例如图像完全位于错误位置。 - Cloud

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接