自由Man

图像拼接Stitcher中estimateTransform、composePanorama函数用法

        最近在做视频拼接,了解到OPENCV自带图像拼接stitcher类,想看看效果。于是就在网上搜了搜stitcher类的用法,发现网上大家发的都来自一篇,或者说都是opencv自带的例子stitching.cpp。这个类当中我们可能用到的成员函数有createDefault、estimateTransform、composePanorama、stitch。但例子只用到createDefault和stitch,实现的例子为:

  1. Stitcher stitcher = Stitcher::createDefault(false);  //不使用GPU加速
      Stitcher::Status status = stitcher.stitch(imgs, pano);  //imgs为输入图像组成的vector,pano为拼接结果

        因为我做的视频拼接,这样子每次做就太慢了,我想先计算矩阵,以后直接调用,就不知道estimateTransform、composePanorama怎么用,opencv里也没有详细说明。废话不多说了,给出我的使用示例,希望对来者有所帮助,不走弯路。

       图像拼接:

  1. #include <stdio.h>  
    #include <iostream>  
    #include <vector>  
    #include "opencv2/highgui/highgui.hpp"  
    #include "opencv2/stitching/stitcher.hpp"  //stitching 影像拼接  
    using namespace cv;  
    using namespace std;  
      
    int main()  
      
    {  
      
       IplImage* img_1 = cvLoadImage("left.jpg");  
       IplImage* img_2 = cvLoadImage("right.jpg");  
       vector<Mat> imgs;  
       imgs.push_back(img_1);  
       imgs.push_back(img_2);  
       //拼接  
       Mat pano;  
       Stitcher stitcher = Stitcher::createDefault(false);  
       stitcher.estimateTransform(imgs);
       stitcher.composePanorama(imgs,pano);
       imshow("result",pano);
       waitKey(1);
       return 0;
    }

原始图:

twopic.jpg

拼接图:

       视频拼接:
  1. #include "stdafx.h"
    #include "opencv/highgui.h"
    #include "opencv/cxcore.h"
    #include "opencv2/stitching/stitcher.hpp"
    #include "cv.h"
    using namespace std;
    using namespace cv; 
    int _tmain(int argc, _TCHAR* argv[])
    {
       vector<Point> MPoints;
       CvMat* warp_mat = cvCreateMat(3,3,CV_32FC1);
       vector<Point2f> LPoints;
       vector<Point2f> RPoints;
       //********Video Init******//
       VideoCapture leftvideo("left_cross.mp4");
       VideoCapture rightvideo("right_cross.mp4");
       if(!leftvideo.isOpened() || !rightvideo.isOpened()){
            printf("NO Video!");
            return 1;}
       Matleftimg;
       Mat rightimg;
       vector<Mat> imgs;
       Mat pano;
       leftvideo.read(leftimg);
       imgs.push_back(leftimg);
       rightvideo.read(rightimg); 
       imgs.push_back(rightimg);
       Stitcher stitchFor2pics = Stitcher::createDefault(false);
       stitchFor2pics.estimateTransform(imgs);
       do 
       {
            stitchFor2pics.composePanorama(imgs,pano);
            imshow("tiled image",pano);
            waitKey(1);
            leftimg = cvQueryFrame(leftvideo);
            rightimg = cvQueryFrame(rightvideo);
            while(0!=imgs.size())
            imgs.pop_back();
            leftvideo.read(leftimg);
            imgs.push_back(leftimg);
            rightvideo.read(rightimg); 
            imgs.push_back(rightimg);
        } while (((NULL!=leftimg)&&(NULL!=rightimg)));
        system("pause");
        return 0;
    }

  • 评论列表:
  •  buct
     发布于 2015-03-09 15:30:54  回复该评论
  • 我最近刚开始做视频拼接,可不可以向你请教一些问题啊?
    •  ziyouman
       发布于 2015-04-30 14:05:11  回复该评论
    • 不好意思,最近空间商那边出了点问题,现在才回复你!请问有什么问题?
      •  tony
         发布于 2016-05-06 20:44:48  回复该评论
      • 请问一下你的视频拼接的代码能运行么?第34和35行运行有问题!
  •  电动客车
     发布于 2017-08-25 13:03:08  回复该评论
  • 偶然路过,进来拜访一下,冒昧打搅,请勿见怪!http://www.greenwheelcv.com

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-BlogPHP 1.5.2 Zero Theme By 爱墙纸

Copyright ZiYouMan.cn. All Rights Reserved. 蜀ICP备15004526号