imge stitching图像拼接stitching

2022-05-09 14:26:14 浏览数 (1)

有没有发现手机相机拍全景就是这个原理。

直接代码:

代码语言:javascript复制
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
// 新版本写在下面文件中:
#include <opencv2/nonfree/features2d.hpp>
//#include "opencv2/features2d/features2d.hpp"
#include<opencv2/legacy/legacy.hpp>
#include <opencv2/stitching/stitcher.hpp>

using namespace std;
using namespace cv;


bool try_use_gpu = false;
vector<Mat> imgs;
string result_name = "/Users/liupeng/Desktop/my/opencvLearn/opencvLearn/result.jpg";

void printUsage();
int parseCmdArgs(int argc, char** argv);

int main(int argc, char* argv[])
{
    argc = 4;
    argv[1] = "/Users/liupeng/Desktop/my/opencvLearn/opencvLearn/lp7.png";
    argv[2] = "/Users/liupeng/Desktop/my/opencvLearn/opencvLearn/lp8.png";
    argv[3] = "/Users/liupeng/Desktop/my/opencvLearn/opencvLearn/lp9.png";

    int retval = parseCmdArgs(argc, argv);
    if (retval) return -1;
    
    Mat pano;
    Stitcher stitcher = Stitcher::createDefault(try_use_gpu);
    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;
}


void printUsage()
{
    cout <<
    "Rotation model images stitcher.nn"
    "stitching img1 img2 [...imgN]nn"
    "Flags:n"
    "  --try_use_gpu (yes|no)n"
    "      Try to use GPU. The default value is 'no'. All default valuesn"
    "      are for CPU mode.n"
    "  --output <result_img>n"
    "      The default is 'result.jpg'.n";
}


int parseCmdArgs(int argc, char** argv)
{
    if (argc == 1)
    {
        printUsage();
        return -1;
    }
    for (int i = 1; i < argc;   i)
    {
        if (string(argv[i]) == "--help" || string(argv[i]) == "/?")
        {
            printUsage();
            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 valuen";
                return -1;
            }
            i  ;
        }
        else if (string(argv[i]) == "--output")
        {
            result_name = argv[i   1];
            i  ;
        }
        else
        {
            Mat img = imread(argv[i]);
            if (img.empty())
            {
                cout << "Can't read image '" << argv[i] << "'n";
                return -1;
            }
            imgs.push_back(img);
        }
    }
    return 0;
}

看一下效果图:

0 人点赞