关于图像拼接,在网上看了很多博客,一开始不明白为什么有opencv自带的stitch类,还有很多人在自己编写图像拼接的程序,,,直到我自己写了这个实时的图像拼接才明白,,stitch虽然拼接效果好,,但是速度真的慢,,尤其是连续拼接,真的卡到死
虽然卡到死,但还是想把程序附出来,,供大家参考,一方面想证明它有多卡,,另一方面想提供个摄像头实时输入视频,实时处理,实时输出视频的框架
程序如下:
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/legacy/legacy.hpp>
#include <opencv2/stitching/stitcher.hpp>
#include <vector>
using namespace std;
using namespace cv;
void stitch(vector<Mat> imgs,Mat& resultMat)
{
bool flag = true;
Stitcher stitcher = Stitcher :: createDefault(flag);
Stitcher::Status status = stitcher.stitch(imgs, resultMat);
//imshow("resultMat1", resultMat);
//return 5;
}
int main()
{
//读取摄像头的程序,可参考之前的博客http://blog.csdn.net/haikuotiankong7/article/details/77878892
VideoCapture cap1(1);//开启外接摄像头,顺序要从大到小,
VideoCapture cap0(0);
if (cap1.isOpened() && cap0.isOpened())
cout << "camera is opened"<< endl;
else
cout << "camera is not opened" << endl;
bool stop = false;
vector<Mat>imgs;
Mat resultMat1;
//设置摄像头分辨率640*480 帧率30fps
// cap0.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
/// cap0.set(CV_CAP_PROP_FRAME_WIDTH, 640);
// cap1.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
// cap1.set(CV_CAP_PROP_FRAME_WIDTH, 640);
Mat videoPlay0,videoPlay1;
int count = 150;//因为stitch拼接实在太卡,想设置读取一部分图像
namedWindow("videoplay0", WINDOW_AUTOSIZE);
namedWindow("videoplay1", WINDOW_AUTOSIZE);
namedWindow("stitch", WINDOW_AUTOSIZE);
while (count)
{
cap0 >> videoPlay0;
cap0.read(videoPlay0) ; //有的时候VS没有 cap0.read(videoPlay0) 这句话就是不读图像,我也没办法
imshow("videoplay0", videoPlay0);
cap1 >> videoPlay1;
cap1.read(videoPlay1);
imshow("videoplay1", videoPlay1);
//imwrite ("1.jpg", videoPlay0);
//imwrite ("2.jpg", videoPlay1);
//Mat img1 = imread("1.jpg");
//Mat img2 = imread("2.jpg");
//imgs.push_back(img1);
//imgs.push_back(img2);
imgs.push_back(videoPlay0);
imgs.push_back(videoPlay1);
stitch(imgs, resultMat1);
imgs.clear();//其实用vector去频繁清除是不合适的,最好还是使用数组,clear()也不是完全清除,
waitKey(100);
count --;
}
return 0;
}
补充:其实实时处理,就是在用摄像头实时读取每一帧的时候进行处理,然后显示处理,这个很考验算法的速度,还需要去继续改进拼接算法才可以