图像拼接摄像头拼接笔记
1、基于opencv3.4.1开发的视频拼接算法,集成了特征提取、双路视频自动拼接算法;
2、需要使用vs2015,显卡运行库已经拷贝到执行文件中,直接就可以运行,如果需要进一步优化,需要自己再继续改进;
3、完全开源,由于工程较大,所以上传到网盘,有需要的可以下载使用;
4、算法中使用了多线程,如果高手做了更深的改进,欢迎一起交流。
网盘地址:链接:https://pan.baidu.com/s/1TfB6ZWPFaWfn18MiXRyuvg 密码:ltct。
原文链接:https://blog.csdn.net/zhulong1984/article/details/80748202
单应变换相比平移变换,具有更广泛的场景适应性,但同时稳定性会有一定程度下降。
设计到的技术细节有:
特征检测与描述
特征匹配与单应矩阵估计
opencv采集视频
渐入渐出图像融合
这个解决方案的硬件条件包括:有两个USB接口的计算机,两个合理放置的USB摄像头。
合理放置是指:两个摄像头分隔一定夹角,相机中心相距接近,所拍摄场景有足够的重叠部分。以上保证了单应变换的可用性。
代码实现:
-
#include "opencv2/core/core.hpp"
-
#include "opencv2/highgui/highgui.hpp"
-
#include "opencv2/imgproc/imgproc.hpp"
-
# include "opencv2/features2d/features2d.hpp"
-
#include"opencv2/nonfree/nonfree.hpp"
-
#include"opencv2/calib3d/calib3d.hpp"
-
#include<iostream>
-
-
using namespace cv;
-
using namespace std;
-
-
int main()
-
{
-
VideoCapture cap1(0);
-
VideoCapture cap2(1);
-
-
double rate = 60;
-
int delay = 1000 / rate;
-
bool stop(false);
-
Mat img1;
-
Mat img2;
-
Mat result;
-
int d = 200;//渐入渐出融合宽度
-
Mat homography;
-
int k = 0;
-
-
namedWindow("cam1", CV_WINDOW_AUTOSIZE);
-
namedWindow("cam2", CV_WINDOW_AUTOSIZE);
-
namedWindow("stitch", CV_WINDOW_AUTOSIZE);
-
-
if (cap1.isOpened() && cap2.isOpened())
-
{
-
cout << "*** ***" << endl;
-
cout << "摄像头已启动!" << endl;
-
}
-
else
-
{
-
cout << "*** ***" << endl;
-
cout << "警告:请检查摄像头是否安装好!" << endl;
-
cout << "程序结束!" << endl << "*** ***" << endl;
-
return -1;
-
}
-
-
cap1.set(CV_CAP_PROP_FOCUS, 0);
-
cap2.set(CV_CAP_PROP_FOCUS, 0);
-
-
while (!stop)
-
{
-
if (cap1.read(img1) && cap2.read(img2))
-
{
-
imshow("cam1", img1);
-
imshow("cam2", img2);
-
-
//彩色帧转灰度
-
//cvtColor(img1, img1, CV_RGB2GRAY);
-
//cvtColor(img2, img2, CV_RGB2GRAY);
-
-
//计算单应矩阵
-
if (k < 1 || waitKey(delay) == 13)
-
{
-
cout << "正在匹配..." << endl;
-
-
vector<KeyPoint> keypoints1, keypoints2;
-
//构造检测器
-
//Ptr<FeatureDetector> detector = new ORB(120);
-
Ptr<FeatureDetector> detector = new SIFT(80);
-
detector->detect(img1, keypoints1);
-
detector->detect(img2, keypoints2);
-
//构造描述子提取器
-
Ptr<DescriptorExtractor> descriptor = detector;
-
//提取描述子
-
Mat descriptors1, descriptors2;
-
-
descriptor->compute(img1, keypoints1, descriptors1);
-
descriptor->compute(img2, keypoints2, descriptors2);
-
//构造匹配器
-
BFMatcher matcher(NORM_L2, true);
-
//匹配描述子
-
vector<DMatch> matches;
-
matcher.match(descriptors1, descriptors2, matches);
-
-
vector<Point2f> selPoints1, selPoints2;
-
vector<int> pointIndexes1, pointIndexes2;
-
for (vector<DMatch>::const_iterator it = matches.begin(); it != matches.end(); ++it)
-
{
-
selPoints1.push_back(keypoints1.at(it->queryIdx).pt);
-
selPoints2.push_back(keypoints2.at(it->trainIdx).pt);
-
}
-
-
vector<uchar> inliers(selPoints1.size(), 0);
-
homography = findHomography(selPoints1, selPoints2, inliers, CV_FM_RANSAC, 1.0);
-
-
//根据RANSAC重新筛选匹配
-
vector<DMatch> outMatches;
-
vector<uchar>::const_iterator itIn = inliers.begin();
-
vector<DMatch>::const_iterator itM = matches.begin();
-
for (; itIn != inliers.end(); ++itIn, ++itM)
-
{
-
if (*itIn)
-
{
-
outMatches.push_back(*itM);
-
}
-
-
}
-
k++;
-
-
//画出匹配结果
-
//Mat matchImage;
-
//drawMatches(img1, keypoints1, img2, keypoints2, outMatches, matchImage, 255, 255);
-
//imshow("match", matchImage);
-
///
-
-
}
-
//拼接
-
double t = getTickCount();
-
warpPerspective(img1, result, homography, Size(2 * img1.cols-d, img1.rows));//Size设置结果图像宽度,宽度裁去一部分,d可调
-
-
Mat half(result, Rect(0, 0, img2.cols - d, img2.rows));
-
img2(Range::all(), Range(0, img2.cols - d)).copyTo(half);
-
for (int i = 0; i < d; i++)
-
{
-
result.col(img2.cols - d + i) = (d - i) / (float)d*img2.col(img2.cols - d + i) + i / (float)d*result.col(img2.cols - d + i);
-
}
-
-
imshow("stitch", result);
-
t = ((double)getTickCount() - t) / getTickFrequency();
-
//cout << t << endl;
-
-
}
-
else
-
{
-
cout << "----------------------" << endl;
-
cout << "waitting..." << endl;
-
}
-
-
if (waitKey(1) == 27)
-
{
-
stop = true;
-
cout << "程序结束!" << endl;
-
cout << "*** ***" << endl;
-
}
-
}
-
return 0;
-
}
实验效果:
上述视频是用录屏软件录制的,分辨率会有下降。实际测试中,直接观察显示良好。两幅输入的源图像均为640*480分辨率,能够做到实时的实现。在我的具有i3处理器配置的笔记本上运行,拼接图像显示间隔为0.10″~0.12″。
————————————————
版权声明:本文为CSDN博主「czl389」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/czl389/article/details/60757000
文章来源: blog.csdn.net,作者:AI视觉网奇,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jacke121/article/details/125226693
- 点赞
- 收藏
- 关注作者
评论(0)