stitching.cpp鱼眼图像拼接融合 源码分析、

https://blog.csdn.net/wd1603926823/article/details/48846099

之前运行OpenCV官方示例的cpp时 看到stitching.cpp拼接融合还不错 然后我在MATLAB上 用之前编的经纬映射法校正三幅鱼眼图像后 不知道该怎样保存下校正好的图 如果save或者save as  那么会有figure的白色边缘  不能用来拼接 所以我直接截图 保存为jpg 这样就没有白色边缘了:

校正后的:然后把这三幅MATLAB运行出来的结果 传递给OpenCV的stitching.cpp  然后运行出来:

现在开始分析图像拼接融合的源码:stitching.cpp:

#include <iostream>
#include <fstream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/stitching/stitcher.hpp"
using namespace std;
using namespace cv;
bool try_use_gpu = false;
vector<Mat> imgs;
string result_name = "result3.jpg";    //保存全景图的文件名 可以在工程目录下找到
void printUsage();
int parseCmdArgs(int argc, char** argv);

int main(int argc, char* argv[])
{
    int retval = parseCmdArgs(argc, argv);
    if (retval) return -1;

Mat pano;
    Stitcher stitcher = Stitcher::createDefault(try_use_gpu);   //这个函数默认不使用GPU!
    Stitcher::Status status = stitcher.stitch(imgs, pano); //找寻待拼接的两幅图的旋转角度 并生成最后的全景图  这个函数才是要分析的重头戏!
    if (status != Stitcher::OK)   //确保拼接成功
    {
        cout << "Can't stitch images, error code = " << int(status) << endl;
        return -1;
    }
namedWindow("stitching result");
    imshow("stitching result", pano);
waitKey(0);
imwrite(result_name,pano);
    return 0;
}
void printUsage()     //默认不用GPU
{
    cout <<
        "Rotation model images stitcher.\n\n"
        "stitching img1 img2 [...imgN]\n\n"
        "Flags:\n"
        "  --try_use_gpu (yes|no)\n"
        "      Try to use GPU. The default value is 'no'. All default values\n"
        "      are for CPU mode.\n"
        "  --output <result_img>\n"
        "      The default is 'result.jpg'.\n";
}
int parseCmdArgs(int argc, char** argv)    //这个函数就是读入待拼接的图片 放在imgs这个装待拼图片的容器里面
{
    if (argc == 1)
    {
        printUsage();
        return -1;
    }
    for (int i = 1; i < argc; ++i)
    {
        if (string(argv[i]) == "--help" || string(argv[i]) == "/?")
        {
            printUsage();
            return -1;
        }
        else if (string(argv[i]) == "--try_use_gpu")
        {
            if (string(argv[i + 1]) == "no")
                try_use_gpu = false;
            else if (string(argv[i + 1]) == "yes")
                try_use_gpu = true;
            else
            {
                cout << "Bad --try_use_gpu flag value\n";
                return -1;
            }
            i++;
        }
        else if (string(argv[i]) == "--output")
        {
            result_name = argv[i + 1];
            i++;
        }
        else
        {
            Mat img = imread(argv[i]);
            if (img.empty())
            {
                cout << "Can't read image '" << argv[i] << "'\n";
                return -1;
            }
            imgs.push_back(img);
        }
    }
    return 0;
}

上面stitching.cpp很好理解  现在来看涉及图像拼接、融合的函数源码 :Stitcher::Status status = stitcher.stitch(imgs, pano); 用CMake打开源码 查看源码:

可以看到stitcher类下各个函数的定义  stitch()是可重载的  而程序里用的是第一个形式 这个定义非常简单  所以接下来又需要去看Status status = estimateTransform(images, rois);(估算几幅待拼图之间的关系 比如仿射变换 旋转 平移 缩放等等)和composePanorama(pano);(图像融合 组成全景图)这两个函数的定义  estimateTransform也简短

Stitcher::Status Stitcher::estimateTransform(InputArray images, const vector<vector<Rect> > &rois)
{
    images.getMatVector(imgs_);
    rois_ = rois;
    Status status;
    if ((status = matchImages()) != OK)  //要看这个的源码
        return status;

//接下来还要看这个函数estimateCameraParams();(估算相机参数)的源码
    estimateCameraParams();  
    return OK;
}

于是又接着找到:

Stitcher::Status Stitcher::matchImages()
{
    if ((int)imgs_.size() < 2)
    {
        LOGLN("Need more images");
        return ERR_NEED_MORE_IMGS; //待拼图像不能少于2幅图
    }
    work_scale_ = 1;
    seam_work_aspect_ = 1;
    seam_scale_ = 1;
    bool is_work_scale_set = false;
    bool is_seam_scale_set = false;
    Mat full_img, img;
    features_.resize(imgs_.size());      //这是??
    seam_est_imgs_.resize(imgs_.size());
    full_img_sizes_.resize(imgs_.size());

LOGLN("Finding features...");
#if ENABLE_LOG
    int64 t = getTickCount();   //找寻特征点计时
#endif

for (size_t i = 0; i < imgs_.size(); ++i)
    {
        full_img = imgs_[i];      //依次读取每幅待拼图像给full_img
        full_img_sizes_[i] = full_img.size();  //将每次读入图像的大小mxn给full_img_sizes
//registr_resol_是图像匹配的分辨率大小,图像的面积尺寸变为registr_resol_*100000  ??
        if (registr_resol_ < 0)    
        {
            img = full_img;
            work_scale_ = 1;
            is_work_scale_set = true;
        }
        else
        {
            if (!is_work_scale_set)
            {

//预处理 将图像缩放full_img.size().area()表示面积m、n的乘积

//计算work_scale,将图像resize到面积在registr_resol_*10^6以下
                work_scale_ = min(1.0, sqrt(registr_resol_ * 1e6 / full_img.size().area()));  
                is_work_scale_set = true;
            }

//将full_img和img的尺寸都缩放到计算出来的work_scale_下?!
            resize(full_img, img, Size(), work_scale_, work_scale_);
        }
        if (!is_seam_scale_set)
        {

//seam_est_resol_是拼接缝像素的大小 ,和匹配默认值一样有默认值?
            seam_scale_ = min(1.0, sqrt(seam_est_resol_ * 1e6 / full_img.size().area()));
            seam_work_aspect_ = seam_scale_ / work_scale_; //和上个if中的差不多意思
            is_seam_scale_set = true;
        }

if (rois_.empty())

//如果rois_是空矩阵 就找寻特征点给features_ (这个待会看源码)
            (*features_finder_)(img, features_[i]);   
        else
        {
            vector<Rect> rois(rois_[i].size());
            for (size_t j = 0; j < rois_[i].size(); ++j)
            {
                Point tl(cvRound(rois_[i][j].x * work_scale_), cvRound(rois_[i][j].y * work_scale_));
                Point br(cvRound(rois_[i][j].br().x * work_scale_), cvRound(rois_[i][j].br().y * work_scale_));
                rois[j] = Rect(tl, br);
            }
            (*features_finder_)(img, features_[i], rois);
        }
        features_[i].img_idx = (int)i;        //说明是第几幅图的特征点
        LOGLN("Features in image #" << i+1 << ": " << features_[i].keypoints.size());
      //将源图像resize到seam_scale_*10^6,并存入seam_est_imgs_[]中
        resize(full_img, img, Size(), seam_scale_, seam_scale_);
        seam_est_imgs_[i] = img.clone();
    }

// Do it to save memory
    features_finder_->collectGarbage();   //这里要看源码!!怎么找寻的
    full_img.release();  //release释放 待会儿去读下一幅图
    img.release();
  //找寻特征点至此结束  计算出时间
    LOGLN("Finding features, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");

LOG("Pairwise matching");
#if ENABLE_LOG
    t = getTickCount(); //开始进行匹配match
#endif
    (*features_matcher_)(features_, pairwise_matches_, matching_mask_);
    features_matcher_->collectGarbage();  //要看源码!!
    LOGLN("Pairwise matching, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec"); //匹配结束

// Leave only images we are sure are from the same panorama

//conf_thresh_是两幅图来自同一全景图的置信度  来判断读入的图片是否属于同一全景图
    indices_ = detail::leaveBiggestComponent(features_, pairwise_matches_, (float)conf_thresh_);
    vector<Mat> seam_est_imgs_subset;
    vector<Mat> imgs_subset;
    vector<Size> full_img_sizes_subset;
    for (size_t i = 0; i < indices_.size(); ++i)
    {
        imgs_subset.push_back(imgs_[indices_[i]]);
        seam_est_imgs_subset.push_back(seam_est_imgs_[indices_[i]]);
        full_img_sizes_subset.push_back(full_img_sizes_[indices_[i]]);
    }
    seam_est_imgs_ = seam_est_imgs_subset;
    imgs_ = imgs_subset;
    full_img_sizes_ = full_img_sizes_subset;
  //检查由上述筛选来自同一副全景图的图片的数量是否大于2幅图
    if ((int)imgs_.size() < 2)
    {
        LOGLN("Need more images");
        return ERR_NEED_MORE_IMGS;
    }

return OK;
}

结合这个人的http://blog.csdn.net/hanshuning/article/details/41960401和这个人的http://www.geekcome.com/content-10-8390-1.html分析上面的

在matchImage()分析完后 要分析estimateCameraParams():

void Stitcher::estimateCameraParams()
{
    detail::HomographyBasedEstimator estimator;
    estimator(features_, pairwise_matches_, cameras_);

for (size_t i = 0; i < cameras_.size(); ++i)
    {
        Mat R;
        cameras_[i].R.convertTo(R, CV_32F);
        cameras_[i].R = R;
        LOGLN("Initial intrinsic parameters #" << indices_[i] + 1 << ":\n " << cameras_[i].K());
    }

bundle_adjuster_->setConfThresh(conf_thresh_);
    (*bundle_adjuster_)(features_, pairwise_matches_, cameras_);

// Find median focal length and use it as final image scale
    vector<double> focals;
    for (size_t i = 0; i < cameras_.size(); ++i)
    {
        LOGLN("Camera #" << indices_[i] + 1 << ":\n" << cameras_[i].K());
        focals.push_back(cameras_[i].focal);
    }

std::sort(focals.begin(), focals.end());
    if (focals.size() % 2 == 1)
        warped_image_scale_ = static_cast<float>(focals[focals.size() / 2]);
    else
        warped_image_scale_ = static_cast<float>(focals[focals.size() / 2 - 1] + focals[focals.size() / 2]) * 0.5f;

if (do_wave_correct_)
    {
        vector<Mat> rmats;
        for (size_t i = 0; i < cameras_.size(); ++i)
            rmats.push_back(cameras_[i].R);
        detail::waveCorrect(rmats, wave_correct_kind_);
        for (size_t i = 0; i < cameras_.size(); ++i)
            cameras_[i].R = rmats[i];
    }
}

接下来生成全景图(图像融合等):composePanorama(pano);即看这个函数的源码

Stitcher::Status Stitcher::composePanorama(OutputArray pano)
{
    return composePanorama(vector<Mat>(), pano);  //就这一句 继续追踪下去 查看composePanorama(vector<Mat>(), pano);的源码
}
如下所示:
Stitcher::Status Stitcher::composePanorama(InputArray images, OutputArray pano)
{
    LOGLN("Warping images (auxiliary)... ");

vector<Mat> imgs;
    images.getMatVector(imgs);
    if (!imgs.empty())
    {
        CV_Assert(imgs.size() == imgs_.size());

Mat img;
        seam_est_imgs_.resize(imgs.size());

for (size_t i = 0; i < imgs.size(); ++i)
        {
            imgs_[i] = imgs[i];
            resize(imgs[i], img, Size(), seam_scale_, seam_scale_);
            seam_est_imgs_[i] = img.clone();
        }

vector<Mat> seam_est_imgs_subset;
        vector<Mat> imgs_subset;

for (size_t i = 0; i < indices_.size(); ++i)
        {
            imgs_subset.push_back(imgs_[indices_[i]]);
            seam_est_imgs_subset.push_back(seam_est_imgs_[indices_[i]]);
        }

seam_est_imgs_ = seam_est_imgs_subset;
        imgs_ = imgs_subset;
    }

Mat &pano_ = pano.getMatRef();

#if ENABLE_LOG
    int64 t = getTickCount();
#endif

vector<Point> corners(imgs_.size());
    vector<Mat> masks_warped(imgs_.size());
    vector<Mat> images_warped(imgs_.size());
    vector<Size> sizes(imgs_.size());
    vector<Mat> masks(imgs_.size());

// Prepare image masks
    for (size_t i = 0; i < imgs_.size(); ++i)
    {
        masks[i].create(seam_est_imgs_[i].size(), CV_8U);
        masks[i].setTo(Scalar::all(255));
    }

// Warp images and their masks
    Ptr<detail::RotationWarper> w = warper_->create(float(warped_image_scale_ * seam_work_aspect_));
    for (size_t i = 0; i < imgs_.size(); ++i)
    {
        Mat_<float> K;
        cameras_[i].K().convertTo(K, CV_32F);
        K(0,0) *= (float)seam_work_aspect_;
        K(0,2) *= (float)seam_work_aspect_;
        K(1,1) *= (float)seam_work_aspect_;
        K(1,2) *= (float)seam_work_aspect_;

corners[i] = w->warp(seam_est_imgs_[i], K, cameras_[i].R, INTER_LINEAR, BORDER_REFLECT, images_warped[i]);
        sizes[i] = images_warped[i].size();

w->warp(masks[i], K, cameras_[i].R, INTER_NEAREST, BORDER_CONSTANT, masks_warped[i]);
    }

vector<Mat> images_warped_f(imgs_.size());
    for (size_t i = 0; i < imgs_.size(); ++i)
        images_warped[i].convertTo(images_warped_f[i], CV_32F);

LOGLN("Warping images, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");

// Find seams
    exposure_comp_->feed(corners, images_warped, masks_warped);
    seam_finder_->find(images_warped_f, corners, masks_warped);

// Release unused memory
    seam_est_imgs_.clear();
    images_warped.clear();
    images_warped_f.clear();
    masks.clear();

LOGLN("Compositing...");
#if ENABLE_LOG
    t = getTickCount();
#endif

Mat img_warped, img_warped_s;
    Mat dilated_mask, seam_mask, mask, mask_warped;

//double compose_seam_aspect = 1;
    double compose_work_aspect = 1;
    bool is_blender_prepared = false;

double compose_scale = 1;
    bool is_compose_scale_set = false;

Mat full_img, img;
    for (size_t img_idx = 0; img_idx < imgs_.size(); ++img_idx)
    {
        LOGLN("Compositing image #" << indices_[img_idx] + 1);

// Read image and resize it if necessary
        full_img = imgs_[img_idx];
        if (!is_compose_scale_set)
        {
            if (compose_resol_ > 0)
                compose_scale = min(1.0, sqrt(compose_resol_ * 1e6 / full_img.size().area()));
            is_compose_scale_set = true;

// Compute relative scales
            //compose_seam_aspect = compose_scale / seam_scale_;
            compose_work_aspect = compose_scale / work_scale_;

// Update warped image scale
            warped_image_scale_ *= static_cast<float>(compose_work_aspect);
            w = warper_->create((float)warped_image_scale_);

// Update corners and sizes
            for (size_t i = 0; i < imgs_.size(); ++i)
            {
                // Update intrinsics
                cameras_[i].focal *= compose_work_aspect;
                cameras_[i].ppx *= compose_work_aspect;
                cameras_[i].ppy *= compose_work_aspect;

// Update corner and size
                Size sz = full_img_sizes_[i];
                if (std::abs(compose_scale - 1) > 1e-1)
                {
                    sz.width = cvRound(full_img_sizes_[i].width * compose_scale);
                    sz.height = cvRound(full_img_sizes_[i].height * compose_scale);
                }

Mat K;
                cameras_[i].K().convertTo(K, CV_32F);
                Rect roi = w->warpRoi(sz, K, cameras_[i].R);
                corners[i] = roi.tl();
                sizes[i] = roi.size();
            }
        }
        if (std::abs(compose_scale - 1) > 1e-1)
            resize(full_img, img, Size(), compose_scale, compose_scale);
        else
            img = full_img;
        full_img.release();
        Size img_size = img.size();

Mat K;
        cameras_[img_idx].K().convertTo(K, CV_32F);

// Warp the current image
        w->warp(img, K, cameras_[img_idx].R, INTER_LINEAR, BORDER_REFLECT, img_warped);
        // Warp the current image mask
        mask.create(img_size, CV_8U);
        mask.setTo(Scalar::all(255));
        w->warp(mask, K, cameras_[img_idx].R, INTER_NEAREST, BORDER_CONSTANT, mask_warped);
        // Compensate exposure
        exposure_comp_->apply((int)img_idx, corners[img_idx], img_warped, mask_warped);
        img_warped.convertTo(img_warped_s, CV_16S);
        img_warped.release();
        img.release();
        mask.release();
        // Make sure seam mask has proper size
        dilate(masks_warped[img_idx], dilated_mask, Mat());
        resize(dilated_mask, seam_mask, mask_warped.size());
        mask_warped = seam_mask & mask_warped;
        if (!is_blender_prepared)
        {
            blender_->prepare(corners, sizes);
            is_blender_prepared = true;
        }
        // Blend the current image
        blender_->feed(img_warped_s, mask_warped, corners[img_idx]);
    }
    Mat result, result_mask;
    blender_->blend(result, result_mask);
    LOGLN("Compositing, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
    // Preliminary result is in CV_16SC3 format, but all values are in [0,255] range,
    // so convert it to avoid user confusing
    result.convertTo(pano_, CV_8U);

return OK;
}

--------------------- 
作者:元气少女缘结神 
来源:CSDN 
原文:https://blog.csdn.net/wd1603926823/article/details/48846099 
版权声明:本文为博主原创文章,转载请附上博文链接!

图像融合——stitching.cpp鱼眼图像拼接融合(01)相关推荐

  1. stitching.cpp鱼眼图像拼接融合 源码分析

    之前运行OpenCV官方示例的cpp时 看到stitching.cpp拼接融合还不错 然后我在MATLAB上 用之前编的经纬映射法校正三幅鱼眼图像后 不知道该怎样保存下校正好的图 如果save或者sa ...

  2. matlab图像拼接融合(四种方法)

    matlab图像拼接的四种方法  1.直接拼接,  2.亮度调整后拼接,  3.按距离比例融合,  4.亮度调整后按距离比例融合 流程: 1.读入左,右图,并取出重合部分,并转化为亮度图 2.分别把每 ...

  3. 基于SIFT特征的图像拼接融合(matlab+vlfeat实现)

    基于SIFT特征的图像拼接融合(matlab+vlfeat实现) piccolo,之前做的东西,简单整理下,不是做图像方向的,写的不好轻喷 主要原理参看SIFT算法详解和SIFT特征匹配算法介绍--寻 ...

  4. C++ OpenCV 图像拼接融合

    目录 一:需求分析 二:步骤详解 三:完整源码分享 四:多张图像拼接  Stitcher算法 一:需求分析 将下面两张图像进行拼接 拼接得到一张完整的图像 二:步骤详解 1.选择特征点 //1.选择特 ...

  5. 【图像融合】自适应参考图像的可见光与热红外彩色图像融合算法

    彩色图像融合技术   可见光与红外热图像的彩色图像融合技术,主要用于夜视应用.基于人眼彩色视觉特性的彩色夜视技术充分利用微光和红外波段的夜视图像信息,可使观察者的目标识别速度和准确度提高30%~60% ...

  6. [开源项目]基于FPGA的视频图像拼接融合

    基于FPGA的视频图像拼接融合 本项目简单来说,就是实时生成视频全景图,该架构经过优化,可以实时视频输出. 算法 下图说明了描述算法每个步骤的系统框图 该系统大致可以分为三个子系统: 预处理 基于 S ...

  7. 探索图像语义分割中的特征融合

    简介 图像语义分割是计算机视觉领域一大重要分支,在benchmark性能一次次提升的过程中,特征融合起到很重要的作用.下面,将介绍图像语义分割任务中历年的比较不错的特征融合方法. 常用的Backbon ...

  8. 深度学习多模态融合_3D目标检测多模态融合综述

    0前言 本篇文章主要想对目前处于探索阶段的3D目标检测中多模态融合的方法做一个简单的综述,主要内容为对目前几篇几篇研究工作的总结和对这个研究方面的一些思考. 在前面的一些文章中,笔者已经介绍到了多模态 ...

  9. 数据融合(2)数据融合方法

    the Joint Directors of Laboratories提出了JDL模型,把数据融合划分为了5个层次. 层次零:Subobject refinement.对数据进行预处理以修正偏差,在时 ...

最新文章

  1. 7月23日 R进行层次聚类算法的继续完善
  2. 阿里分布式中间件Seata从入门到精通
  3. OO ALV 实现方式 ALV TABLE 之 栏位属性
  4. java编程基础码_1.java编程基础
  5. Docker教程小白实操入门(3)--如何启动一个已经停止的容器
  6. 对于新旧技术的争论,我很low的想法
  7. RouterOS利用(L2TP)实现异地组网
  8. 基于STM32F103驱动TM1640LED数码管代码实现
  9. 加法C语言基础程序,C语言菜鸟基础教程之加法
  10. 信息安全制度管理办法
  11. d3,svg中如何让rect上显示文字
  12. 导致页面布局混乱的几个元凶
  13. 2018年人均寿命是多少_那是2018年
  14. 服务器异常 随机名称的进程CPU资源占用过高
  15. 手撕设计模式,如何理解依赖倒置原则和好莱坞原则
  16. NYOJ284坦克大战
  17. ESP32 VHCI架构实现BLE扫描设备
  18. 可在线查看并附源码下载!基于SSM(spring springMVC mybatis mysql)框架的hrm人事管理系统
  19. 廊坊知恩:抖音店铺怎么运营
  20. R语言——线性判别分析(LDA)

热门文章

  1. 系统修复软件测试工资,技术员们有福了,关于怎么用MHDD修复硬盘坏道,现在免费给大家了!!!...
  2. 2021年中式烹调师(中级)考试报名及中式烹调师(中级)复审模拟考试
  3. 2023 年值得推荐的 Vue 库
  4. ESC, ESP, VSC 电子稳定系统 与 ABS
  5. 游戏服务器中多人交互逻辑业务的思考
  6. 报错解决:form表单提交存入数据库出现中文乱码
  7. 新南威尔士大学数学与统计学院课业解析M12 T11
  8. [强文]有几个还活着?十年应用软件之路
  9. MTK机型手机指令介绍
  10. Open3d之计算点云边界框