图像拼接(九):双摄像头实时视频拼接(单应变换模型)

单应变换相比平移变换,具有更广泛的场景适应性,但同时稳定性会有一定程度下降。

设计到的技术细节有:

  • 特征检测与描述
  • 特征匹配与单应矩阵估计
  • 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″。

发布了75 篇原创文章 · 获赞 127 · 访问量 40万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览