c++全景图像拼接

48 篇文章 4 订阅

拼接前
在这里插入图片描述

拼接后在这里插入图片描述


#include <fstream>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\stitching.hpp>
#include <iostream>


using namespace cv;
using namespace std;

vector<Mat> imgs; //保存拼接的原始图像向量

//导入所有原始拼接图像函数
void parseCmdArgs()
{
    for (int i = 1; i<17; i++)
    {
        string file = "";
        /*if (i < 10)
            file = "../img/0" + to_string(i) + ".png";
        else file = "../img/" + to_string(i) + ".png";*/

        file = "data/16/" + to_string(i) + ".jpg";
        cout << " read image '" << file << "'\n";
        Mat img = imread(file);
//        cv::resize(img, img, Size(1280, 640));
        if (img.empty())
        {
            cout << "Can't read image '" << file << "'\n";
        }
        imgs.push_back(img);
    }
}

int main()
{
    //导入拼接图像
    parseCmdArgs();

    Mat pano;
    Stitcher stitcher = Stitcher::createDefault(false);
    Stitcher::Status status = stitcher.stitch(imgs, pano);//拼接
     cout << "Stitcher";
    if (status != Stitcher::OK) //判断拼接是否成功
    {
        cout << "Can't stitch images, error code = " << int(status) << endl;
        return -1;
    }

    //cv::Mat pano = cv::imread("./quanjingtuxiang.png");
    imshow("全景拼接", pano);

    imwrite("./quanjingtuxiang.png", pano);

    cv::Mat mask;
    cv::cvtColor(pano, mask, CV_RGB2GRAY);
    int h = mask.rows;
    int w = mask.cols;
    Scalar value = Scalar(0);
    cv::copyMakeBorder(mask, mask, 10, 10, 10, 10, BORDER_CONSTANT, value);

    cv::threshold(mask, mask, 0, 255, THRESH_BINARY);

    std::vector<std::vector<cv::Point> > contours;
    cv::Mat temp;
    mask.copyTo(temp);
    cv::findContours(temp, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
    cv::Rect box = cv::boundingRect(contours[0]);
    int x = box.x;
    int y = box.y;
    int dh = box.height;//std::memcpy(image_feature_data.data, data, size_);
    int dw = box.width;

    cv::Mat RECT(h+20, w+20, CV_8UC1, cv::Scalar(0));
    RECT(box).setTo(255);


    cv::Mat minRect;
    RECT.copyTo(minRect);
    cv::Mat sub;
    mask.copyTo(sub);
    cv::Mat element = getStructuringElement(MORPH_RECT, Size(3, 3));
    while (cv::countNonZero(sub) > 0) {
        cv::erode(minRect, minRect, element);
        cv::subtract(minRect, mask, sub);
    }


    value = Scalar(0, 0, 0);
    cv::copyMakeBorder(pano, pano, 10, 10, 10, 10, BORDER_CONSTANT, value);
    cv::findContours(minRect, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
    box = cv::boundingRect(contours[0]);

    cv::Mat Result;
    pano(box).copyTo(Result);
    imwrite("./final_16.png", Result);

    waitKey();
    return 0;
}

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vqt5_qt6

你的鼓励是我们创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值