基于opencv快速实现图像拼接

直接使用OpenCV提供的Stitcher进行拼接,简单快捷(代码来自 https://blog.csdn.net/Zero___Chen/article/details/122274445 )

image_stitching.cpp代码如下

#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

待拼接图像是这两个

拼接效果如下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值