原图像如下图所示,第一张图像的右侧与第二张图像的左侧有重合区域,通过图像拼接将两张图合成一张图。使用OpenCV的Stitcher函数实现。
完整代码如下,亲测可用。
#include < stdio.h > #include < opencv2\opencv.hpp > #include < opencv2\stitching.hpp >int main(){std::vector< cv::Mat > vImg;cv::Mat rImg;vImg.push_back(cv::imread("xiaoyuan1.bmp"));vImg.push_back(cv::imread("xiaoyuan2.bmp"));cv::Stitcher::Mode mode = cv::Stitcher::PANORAMA;cv::Ptr<cv::Stitcher> stitcher = cv::Stitcher::create(mode);unsigned long AAtime = 0, BBtime = 0; //check processing timeAAtime = cv::getTickCount(); //check processing timecv::Mat mergeImage;bool status = stitcher->stitch(vImg, mergeImage);BBtime = cv::getTickCount(); //check processing time printf("Time consuming: %.2lf sec \n", (BBtime - AAtime) / cv::getTickFrequency()); //check processing timeif (cv::Stitcher::OK == status)cv::imshow("Stitching Result", mergeImage);elseprintf("Stitching fail.");cv::waitKey(0);}