直接使用OpenCV提供的Stitcher进行拼接,简单快捷(代码来自 https://blog.csdn.net/Zero___Chen/article/details/122274445 )
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
bool OpenCV_Stitching(Mat image_left, Mat image_right);
int main() {
// 加载两幅待拼接的图像
Mat image_left = imread("IMG_20231130_153530.jpg");
Mat image_right = imread("IMG_20231130_153533.jpg");
// 进行图像拼接
bool success = OpenCV_Stitching(image_left, image_right);
if (!success) {
std::cerr << "Error: image stitching failed." << std::endl;
return 1;
}
waitKey(0);
destroyAllWindows();
return 0;
}
bool OpenCV_Stitching(Mat image_left, Mat image_right) {
// 将待拼接图片放进容器里面
vector<Mat> images;
images.push_back(image_left);
images.push_back(image_right);
// 创建Stitcher模型
Ptr<Stitcher> stitcher = Stitcher::create();
Mat result;
Stitcher::Status status = stitcher->stitch(images, result); // 使用stitch函数进行拼接
if (status != Stitcher::OK)
return false;
imshow("OpenCV图像全景拼接", result);
imwrite("image_stitched.jpg", result);
return true;
}
编译运行命令如下
g++ image_stitching.cpp -o image_stitching `pkg-config --cflags --libs opencv4`
./image_stitching
待拼接图像是这两个
拼接效果如下