C++ OpenCV - 从图像网格拼接图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22056737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenCV - Stitching Images from a grid of images
提问by Cloud
I have found some basic working examples on stitching via OpenCV for panoramic images. I have also found some useful documentation in the API docs, but I can't find out how to speed up the processing by providing additional information.
我找到了一些关于通过 OpenCV 拼接全景图像的基本工作示例。我还在API docs 中找到了一些有用的文档,但我无法找到如何通过提供附加信息来加快处理速度。
In my case, I generate a set of images in a 20x20 grid of individual frames, for a total of 400 images to be stitched into a single large one. This takes an enormous amount of time on a modern PC, so it would likely take hours on a developer board.
在我的例子中,我在单个帧的 20x20 网格中生成一组图像,将总共 400 张图像拼接成一个大的图像。这在现代 PC 上需要大量时间,因此在开发板上可能需要数小时。
Is there any way to tell the OpenCV instance information about the images, such as me knowing in advance the relative positioning of all the images as they would appear on a grid? The only API calls I see so far is to just add all the images indiscriminately to a queue via vImg.push_back()
.
有没有办法告诉 OpenCV 实例有关图像的信息,例如我事先知道所有图像在网格上出现时的相对位置?到目前为止,我看到的唯一 API 调用是通过vImg.push_back()
.
References
参考
- Stitching. Image Stitching - OpenCV API Documentation, Accessed 2014-02-26,
<http://docs.opencv.org/modules/stitching/doc/stitching.html>
- OpenCV Stitching example (Stitcher class, Panorama), Accessed 2014-02-26,
<http://feelmare.blogspot.ca/2013/11/opencv-stitching-example-stitcher-class.html>
- Panorama – Image Stitching in OpenCV, Accessed 2014-02-26,
<http://ramsrigoutham.com/2012/11/22/panorama-image-stitching-in-opencv/>
- 缝合。图像拼接 - OpenCV API 文档,2014 年 2 月 26 日访问,
<http://docs.opencv.org/modules/stitching/doc/stitching.html>
- OpenCV 拼接示例(Stitcher 类,全景图),2014 年 2 月 26 日访问,
<http://feelmare.blogspot.ca/2013/11/opencv-stitching-example-stitcher-class.html>
- 全景图 - OpenCV 中的图像拼接,2014年 2 月 26 日访问,
<http://ramsrigoutham.com/2012/11/22/panorama-image-stitching-in-opencv/>
采纳答案by Ill Tempered Sea Bass
So far as I know, there is no means to provide additional data to the OpenCV engine beyond just giving it a list of images. It does a pretty good job on its own though. I would check out some of the example code, and test how long each stitching operation takes. From my experiments using 4x6, 4x8, ..., 4x20 panoramic reconstructions, the CPU time required seems to increase with the number of overlapping images. I would imagine your case would require at least a minute to compute on a modern machine.
据我所知,除了给 OpenCV 引擎提供一个图像列表之外,没有办法为它提供额外的数据。不过,它本身做得很好。我会查看一些示例代码,并测试每个拼接操作需要多长时间。从我使用 4x6、4x8、...、4x20 全景重建的实验来看,所需的 CPU 时间似乎随着重叠图像的数量而增加。我想你的情况至少需要一分钟才能在现代机器上计算。
Source: https://code.ros.org/trac/opencv/browser/trunk/opencv/samples/cpp/stitching.cpp?rev=6682
来源: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
回答by martinarroyo
I did some work with the stitching pipeline and though I do not consider myself an expert on the field, I did get better performance (and better results as well) adjusting each step of the pipeline separately. As you can see in the picture, the Stitching class is nothing but a wrapper of this pipeline:
我在拼接管道方面做了一些工作,虽然我不认为自己是该领域的专家,但我确实获得了更好的性能(以及更好的结果)分别调整管道的每个步骤。正如您在图片中看到的,Stitching 类只不过是此管道的包装器:
Some interesting parts you can adjust are the resizing steps (there comes a point were more resolution just means more computation time and more inaccurate features), the matching process and (though this is just a guess) giving a good camera parameters instead of performing an estimation. This involves getting the camera parameters before doing the stitching, but it is not really hard. Here you have some reference: OpenCV Camera Calibration and 3D Reconstruction.
您可以调整的一些有趣的部分是调整大小的步骤(有一点,更高的分辨率只是意味着更多的计算时间和更不准确的特征)、匹配过程和(尽管这只是猜测)给出一个好的相机参数而不是执行估计。这涉及在进行拼接之前获取相机参数,但这并不难。这里有一些参考:OpenCV Camera Calibration and 3D Reconstruction。
Again: I am not an expert, this is just based on my experience as an intern doing some experiments with the library!
再说一遍:我不是专家,这只是基于我作为实习生在图书馆做一些实验的经验!
回答by RonenKi
Maybe this could help? https://software.intel.com/en-us/articles/fast-panorama-stitching
也许这会有所帮助? https://software.intel.com/en-us/articles/fast-panorama-stitching
Specifically the part about pairwise matching
具体关于成对匹配的部分
Ronen
罗宁
回答by Samer
Consider enabling the use of GPU in the Opencv Stitcher:
考虑在 Opencv Stitcher 中启用 GPU 的使用:
bool try_use_gpu = true;
Stitcher myStitcher = Stitcher::createDefault(try_use_gpu);
Stitcher::Status status = myStitcher.stitch(Imgs, pano);
回答by EnergyElk
If you know the relative positions of the images, it seems that you could break down the problem into sub-problems and possibly reduce the computational load by approaching it with knowledge of the substructure of the problem. Basically break the set of images into groups of 4 adjacent images, process the frames, then proceed to process the resulting images using the same idea until you have arrived at your panorama. That being said, I've only recently began toying with this toolset of opencv. I know it's a pretty simple idea, but it might be useful to someone.
如果您知道图像的相对位置,似乎您可以将问题分解为子问题,并可能通过了解问题的子结构来处理它来减少计算负载。基本上将一组图像分成 4 个相邻图像的组,处理帧,然后使用相同的想法继续处理结果图像,直到您获得全景图。话虽如此,我最近才开始玩弄这个 opencv 工具集。我知道这是一个非常简单的想法,但它可能对某人有用。