【发布时间】:2017-03-17 13:49:24
【问题描述】:
我想将多个图像 (25) 拼接到一个塑料零件直表面的单个图像中。图片如下所示:
我正在尝试使用 Stitcher 类表单 opencv。我的代码在这里:
#include <iostream>
#include <fstream>
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/stitching.hpp>
using namespace std;
using namespace cv;
bool try_use_gpu = false;
vector<Mat> imgs;
string result_name = "result.jpg";
Mat img1, img2,img3,img4,img5,img6,img7, pano;
void printUsage();
//int parseCmdArgs(int argc, char** argv);
int main(int argc, char* argv[])
{
// Load images from HD.
img1 = imread("1.bmp");
img2 = imread("2.bmp");
img3 = imread("3.bmp");
img4 = imread("4.bmp");
// Put images into vector of images "imgs".
imgs.push_back(img1);
imgs.push_back(img2);
imgs.push_back(img3);
imgs.push_back(img4);
// Create stitcher instance and use stitch method with imgs.
Stitcher stitcher = Stitcher::createDefault(try_use_gpu);
stitcher.setPanoConfidenceThresh(0.8);
Stitcher::Status status = stitcher.stitch(imgs, pano);
if (status != Stitcher::OK)
{
cout << "Can't stitch images, error code = " << status << endl;
return -1;
}
imwrite(result_name, pano);
return 0;
}
我总是收到一条错误消息:“无法拼接图像,错误代码 = 1”,因此系统说它需要更多图像。调试时,我看到图像已正确加载,然后正确创建了矢量 img。可能是什么原因?我的计算也持续了很长时间(2 秒)...
【问题讨论】:
-
恕我直言,Stitcher 对于您的输入来说有点矫枉过正。假设图像是有序的,它们没有失真,并且相机沿一个轴以一致的方向移动,您可以利用这一点。您可以使用模板匹配来查找相邻图像的重叠部分并使用它来制作合成图像。最简单的方法是连接不重叠的部分。交替地对重叠的部分进行一些混合。 |然而,理想的方法是完全消除这样做的需要——一个线阵相机 + 一些编码器。
-
@skoda23 一个非常简单的prototype in Python,生成this result。您可以添加一些垂直的“摆动空间”,以防切片不像您显然合成的示例那样排列整齐。还添加一些重叠部分的混合,或者使用
addWeighted的一半,或者使用权重的渐变。 -
顺便说一句,如果你能提供完整的25张图片就很方便了,所以我们可以做一些有意义的比较。
-
好的,我已经看到你了started porting it :) 有时间我会看看。
-
@skoda23 Here's a rough c++ implementation,支持一些垂直错位,助您一臂之力。明天我会评论python和c++代码并写一些答案。
标签: opencv image-processing image-stitching opencv-stitching