#include "opencv2/imgcodecs.hpp" #include "opencv2/highgui.hpp" #include "opencv2/stitching.hpp" #include <iostream> using namespace std; using namespace cv; bool try_use_gpu = false; bool divide_images = false; Stitcher::Mode mode = Stitcher::PANORAMA; vector<Mat> imgs; string result_name = "result.jpg"; void printUsage(char** argv); int parseCmdArgs(int argc, char** argv); int main(int argc, char* argv[]) { //int retval = parseCmdArgs(argc, argv); //if (retval) return -1; Mat img1 = imread("/hard_disk2/Receive_files/xls/mask-rcnn-test/QT/1.png"); Mat img2 = imread("/hard_disk2/Receive_files/xls/mask-rcnn-test/QT/2.png"); imgs.push_back(img1); imgs.push_back(img2); // imgs.push_back(img1); // imgs.push_back(img2); // imgs.push_back(img1); // imgs.push_back(img2); Mat pano; Ptr<Stitcher> stitcher = Stitcher::create(mode, try_use_gpu); Stitcher::Status status = stitcher->stitch(imgs, pano); if (status != Stitcher::OK) { cout << "Can't stitch images, error code = " << int(status) << endl; return -1; } imwrite(result_name, pano); waitKey(0); cout << "stitching completed successfully " << result_name << " saved!"; return 0; }
代码如上,参考https://docs.opencv.org/trunk/d8/d19/tutorial_stitcher.html
运行时报错cv::Stitcher::ERR_NEED_MORE_IMGS (1),后来查找原因,发现是我随便选取了两张图片进行拼接,然而这两张图片并没有重叠的部分,然后修改了两张图片之后,运行正常了。所以进行图片拼接的时候需要注意要有重叠部分才可以。