必须有重叠才能拼,压缩越多,拼接越快
#include <opencv2opencv.hpp> #include <opencv2stitching.hpp> using namespace cv; using namespace std; vector<Mat> imgs; string result_name = "result.jpg"; double fScale = 0.1; const int LEN = 12; string str[LEN] = { "F://1.jpg", "F://2.jpg", "F://3.jpg", "F://4.jpg", "F://5.jpg", "F://6.jpg", "F://7.jpg", "F://8.jpg", "F://9.jpg", "F://10.jpg", "F://11.jpg", "F://12.jpg" }; void AddPic(string picURL) { Mat pic = imread(picURL); Size dsize = Size(pic.cols*fScale, pic.rows*fScale); Mat pic2 = Mat(dsize, CV_32S); resize(pic, pic2, dsize); imgs.push_back(pic2); } int main() { for (int i = 0; i < LEN; i++) { AddPic(str[i]); } Mat pano; Stitcher stitcher = Stitcher::createDefault(true); 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); waitKey(0); }