最近在做视频拼接,了解到OPENCV自带图像拼接stitcher类,想看看效果。于是就在网上搜了搜stitcher类的用法,发现网上大家发的都来自一篇,或者说都是opencv自带的例子stitching.cpp。这个类当中我们可能用到的成员函数有createDefault、estimateTransform、composePanorama、stitch。但例子只用到createDefault和stitch,实现的例子为:
Stitcher stitcher = Stitcher::createDefault(false); //不使用GPU加速 Stitcher::Status status = stitcher.stitch(imgs, pano); //imgs为输入图像组成的vector,pano为拼接结果
因为我做的视频拼接,这样子每次做就太慢了,我想先计算矩阵,以后直接调用,就不知道estimateTransform、composePanorama怎么用,opencv里也没有详细说明。废话不多说了,给出我的使用示例,希望对来者有所帮助,不走弯路。
图像拼接:
#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; }
原始图:
拼接图:
视频拼接:
#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; }