📜  使用OpenCV和C++拼接输入图像(全景)

📅  最后修改于: 2021-05-30 10:47:07             🧑  作者: Mango

该程序旨在通过使用OpenCV库stitching.hpp将图像拼接在一起来从一组图像中创建全景图,并且该实现是在C++中完成的。程序将生成的针迹图像保存在与程序文件相同的目录中。如果未拼接图像组,则错误退出程序。出现此错误的原因是,输入图像没有公共区域,或者它们没有共享公共像素点。

实施代码的步骤:

1)确保在本地计算机上安装了OpenCV
2)将输入图像放置在与程序相同的目录中。
3)像往常一样从命令提示符处编译代码。
4)运行代码时,将所有输入图像作为参数。
5)通过名称“ result.jpg”检查生成的图像

缝合线:

此图像显示了拼接算法工作原理的基本体系结构。该图像基于M. Brown和D. Lowe题为“使用不变特征的自动全景图像拼接”的研究论文。请参考参考资料中的第二个链接。

有关详细信息,请参考此opencv.org图像。

执行

// CPP program to Stitch
// input images (panorama) using OpenCV 
#include 
#include 
  
// Include header files from OpenCV directory
// required to stitch images.
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/stitching.hpp"
  
using namespace std;
using namespace cv;
  
// Define mode for stitching as panoroma 
// (One out of many functions of Stitcher)
Stitcher::Mode mode = Stitcher::PANORAMA;
  
// Array for pictures
vector imgs;
  
int main(int argc, char* argv[])
{
    // Get all the images that need to be 
    // stitched as arguments from command line 
    for (int i = 1; i < argc; ++i)
    {
            // Read the ith argument or image 
            // and push into the image array
            Mat img = imread(argv[i]);
            if (img.empty())
            {
                // Exit if image is not present
                cout << "Can't read image '" << argv[i] << "'\n";
                return -1;
            }
            imgs.push_back(img);
    }
      
    // Define object to store the stitched image
    Mat pano;
      
    // Create a Stitcher class object with mode panoroma
    Ptr stitcher = Stitcher::create(mode, false);
      
    // Command to stitch all the images present in the image array
    Stitcher::Status status = stitcher->stitch(imgs, pano);
  
    if (status != Stitcher::OK)
    {
        // Check if images could not be stiched
        // status is OK if images are stiched successfully
        cout << "Can't stitch images\n";
        return -1;
    }
      
    // Store a new image stiched from the given 
    //set of images as "result.jpg"
    imwrite("result.jpg", pano);
      
    // Show the result
    imshow("Result", pano);
      
    waitKey(0);
    return 0;
}

输入图像:

输出:

参考:

1)http://docs.opencv.org/2.4/modules/stitching/doc/stitching.html
2)http://docs.opencv.org/2.4/modules/stitching/doc/introduction.html

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”