基于OPENCV3.3图像拼接

#include "stdafx.h"



#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp> 
#include "imgproc/imgproc.hpp"  
#include <opencv2/features2d/features2d.hpp> 
#include "opencv2/xfeatures2d/nonfree.hpp" 
#include<vector>
#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;
//计算原始图像点位在经过矩阵变换后在目标图像上对应位置  
Point2f getTransformPoint(const Point2f originalPoint, const Mat &transformMaxtri)
{
	Mat originelP, targetP;
	originelP = (Mat_<double>(3, 1) << originalPoint.x, originalPoint.y, 1.0);
	targetP = transformMaxtri * originelP;
	float x = targetP.at<double>(0, 0) / targetP.at<double>(2, 0);
	float y = targetP.at<double>(1, 0) / targetP.at<double>(2, 0);
	return Point2f(x, y);
}
/*************************************************
Copyright:bupt
Author:
Date:2010-08-25
Description:描述主要实现的功能  使用Fast算子提取特征点
**************************************************/
int main(int argc, char *argv[])
{
	Mat image01 = imread("image01.jpg");
	Mat image02 = imread("image02.jpg");
	imshow("拼接图像1", image01);
	imshow("拼接图像2", image02);

	//灰度图转换  
	Mat image1, image2;
	cvtColor(image01, image1, CV_RGB2GRAY);
	cvtColor(image02, image2, CV_RGB2GRAY);

	//提取特征点    
	int minHessian = 800;
	Ptr<xfeatures2d::SURF> suftDetector = xfeatures2d::SURF::create(minHessian);
	vector<KeyPoint> keyPoint1, keyPoint2;
	suftDetector->detect(image1, keyPoint1);
	suftDetector->detect(image2, keyPoint2);

	//特征点描述,为下边的特征点匹配做准备    
	Mat imageDesc1, imageDesc2;
	suftDetector->compute(image1, keyPoint1, imageDesc1);
	suftDetector->compute(image2, keyPoint2, imageDesc2);

	//获得匹配特征点,并提取最优配对     
	FlannBasedMatcher matcher;
	vector<DMatch> matchePoints;
	matcher.match(imageDesc1, imageDesc2, matchePoints, Mat());
	sort(matchePoints.begin(), matchePoints.end()); //特征点排序    
													//获取排在前N个的最优匹配特征点  
	vector<Point2f> imagePoints1, imagePoints2;
	for (int i = 0; i<10; i++)
	{
		imagePoints1.push_back(keyPoint1[matchePoints[i].queryIdx].pt);
		imagePoints2.push_back(keyPoint2[matchePoints[i].trainIdx].pt);
	}

	//获取图像1到图像2的投影映射矩阵,尺寸为3*3  
	Mat homo = findHomography(imagePoints1, imagePoints2, CV_RANSAC);
	Mat adjustMat = (Mat_<double>(3, 3) << 1.0, 0, image01.cols, 0, 1.0, 0, 0, 0, 1.0);
	Mat adjustHomo = adjustMat * homo;

	//获取最强配对点在原始图像和矩阵变换后图像上的对应位置,用于图像拼接点的定位  
	Point2f originalLinkPoint, targetLinkPoint, basedImagePoint;
	originalLinkPoint = keyPoint1[matchePoints[0].queryIdx].pt;
	targetLinkPoint = getTransformPoint(originalLinkPoint, adjustHomo);
	basedImagePoint = keyPoint2[matchePoints[0].trainIdx].pt;

	//图像配准  
	Mat imageTransform1;
	warpPerspective(image01, imageTransform1, adjustMat*homo, Size(image02.cols + image01.cols + 10, image02.rows));

	//在最强匹配点的位置处衔接,最强匹配点左侧是图1,右侧是图2,这样直接替换图像衔接不好,光线有突变  
	Mat ROIMat = image02(Rect(Point(basedImagePoint.x, 0), Point(image02.cols, image02.rows)));
	ROIMat.copyTo(Mat(imageTransform1, Rect(targetLinkPoint.x, 0, image02.cols - basedImagePoint.x + 1, image02.rows)));

	namedWindow("拼接结果", 0);
	imshow("拼接结果", imageTransform1);
	waitKey();
	return 0;
}

转载地址:https://blog.youkuaiyun.com/dcrmg/article/details/52629856







评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值