串联匹配百张图像拼接2

本文介绍了一种快速图像拼接算法,适用于航拍图像序列的处理。该算法利用OpenCV库,通过特征点提取、匹配、单应性计算及图像融合等步骤,实现了100张532*300分辨率图像的高效拼接,整个过程仅需60秒。文章详细阐述了代码实现细节,包括特征点匹配、单应性矩阵计算、图像缩小处理以及最终的图像拼接流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

60s运行100张532*300的图片。
之前的方法只是适合相机不旋转的情况,这个就没有特殊要求了。
但是我们注意到,由于图像从最后一张开始拼接,在拼接的过程中,不断变得模糊,因为经过多次处理,而图片的左侧,就保持最初的分辨率,比较清晰。
由于航拍图像序列太过密集导致,一般图像拼接则不会出现这个问题

OpenCV2.4.9

#include "opencv2/core/core.hpp"
#include "highgui.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include "opencv2/legacy/legacy.hpp"
#include <iostream>
#include <fstream>
#include <string>
#include<vector>
#include "opencv2/opencv_modules.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/stitching/detail/autocalib.hpp"
#include "opencv2/stitching/detail/blenders.hpp"
#include "opencv2/stitching/detail/camera.hpp"
#include "opencv2/stitching/detail/exposure_compensate.hpp"
#include "opencv2/stitching/detail/matchers.hpp"
#include "opencv2/stitching/detail/motion_estimators.hpp"
#include "opencv2/stitching/detail/seam_finders.hpp"
#include "opencv2/stitching/detail/util.hpp"
#include "opencv2/stitching/detail/warpers.hpp"
#include "opencv2/stitching/warpers.hpp"
#include "opencv2/calib3d/calib3d.hpp"

using namespace std;
using namespace cv;
using namespace cv::detail;

//如果图像太大缩小一半
Mat mynarrow(Mat img)
{
	Mat dst;//读出一个图
	if (img.rows*img.cols > 2400 * 1200)
		resize(img, dst, Size(), 0.5, 0.5);
	else
		dst = img.clone();
	return dst;
}

//利用findHomography函数利用匹配的关键点找出相应的变换:
Mat myfindHomography(std::vector< DMatch > & good_matches, std::vector<KeyPoint>& keypoints_1, std::vector<KeyPoint> & keypoints_2)
{
	//-- Localize the object from img_1 in img_2     //在img_2中定位来自img_1的对象
	std::vector<Point2f> obj;
	std::vector<Point2f> scene;

	for (unsigned int i = 0; i < good_matches.size(); i++)
	{
		//-- Get the keypoints from the good matches    //从好的匹配中获取关键点
		obj.push_back(keypoints_1[good_matches[i].queryIdx].pt);
		scene.push_back(keypoints_2[good_matches[i].trainIdx].pt);
	}

	//两个平面上相匹配的特征点求出变换公式
	Mat H = findHomography(obj, scene, CV_RANSAC);

	return H;
}


//用单应性过滤匹配
bool refineMatchesWithHomography(const std::vector<cv::KeyPoint>& queryKeypoints,
	const std::vector<cv::KeyPoint>& trainKeypoints,
	float reprojectionThreshold,
	std::vector<cv::DMatch>& matches//,
	//cv::Mat& homography
)
{
	cv::Mat homography;
	const int minNumberMatchesAllowed = 4;
	if (matches.size() < minNumberMatchesAllowed)
		return false;
	// 为 cv::findHomography 准备数据
	std::vector<cv::Point2f> queryPoints(matches.size());
	std::vector<cv::Point2f> trainPoints(matches.size());
	for (size_t i = 0; i < matches.size(); i++)
	{
		queryPoints[i] = queryKeypoints[matches[i].queryIdx].pt;
		trainPoints[i] = trainKeypoints[matches[i].trainIdx].pt;
	}
	// 查找单应矩阵并获取内点掩码
	std::vector<unsigned char> inliersMask(matches.size());
	homography = findHomography(queryPoints,
		trainPoints,
		CV_FM_RANSAC,
		reprojectionThreshold,
		inliersMask);
	std::vector<cv::DMatch> inliers;
	for (size_t i = 0; i < inliersMask.size(); i++)
	{
		if (inliersMask[i])
			inliers.push_back(matches[i]);
	}
	matches.swap(inliers);
	//Mat homoShow;
	//drawMatches(src,queryKeypoints,frameImg,trainKeypoints,matches,homoShow,Scalar::all(-1),CV_RGB(255,255,255),Mat(),2);
	//imshow("homoShow",homoShow);
	return matches.size() > minNumberMatchesAllowed;

}

//获得匹配点坐标函数
Point2f get_match_points(vector<KeyPoint>& keypoints1,vector<KeyPoint>& keypoints2, vector< DMatch > & matches, vector<Point2f>& points1,vector< Point2f>& points2)
{
	for (int i = 0; i < matches.size(); i++)
	{
		int index1 = matches.at(i).queryIdx;
		int index2 = matches.at(i).trainIdx;
		points1.push_back(keypoints1.at(index1).pt);
		points2.push_back(keypoints2.at(index2).pt);

	}

}



int main()
{
	/*	特征点的提取与匹配 	*/
	int num_images = 100;    //图像数量,可修改
	vector<string> image_names; // image_names[i]表示第i个图像的名称
	string name;
	ifstream f("D:\\list低分辨率.txt");
		assert(f.is_open());
		for(int i=0;i<num_images;i++)
		{
			getline(f, name);
			name = "D:\\低分辨率截图\\" + name;
			cout << name;
			image_names.push_back(name);
		}


	vector<vector<DMatch> > image_matches; // image_matches[i]表示第i幅图像和第i+1幅图像特征点匹配的结果
	 // 提取特征点

	vector<ImageFeatures> features(num_images);    //表示图像特征
	char temp[100];
	double ge[100];//100张图的特征点个数
	Point2f point;
	KeyPoint kp;
	float temp1 = 0, temp2 = 0;
	char ptsname[100];
	char descname[100];
	ifstream g("D:\\特征\\特征点个数.txt");//将100张图的特征点个数导入数组
	assert(g.is_open());
	for (int i = 1; i <= num_images; i++)
	{
		g >> ge[i - 1];
	}
	g.close();
	for (int i = 1; i <= num_images; i++)
	{
		sprintf(ptsname, "D:\\特征\\pts%d.txt", i); //格式化输出文件名
		ifstream infile(ptsname);
		assert(infile.is_open());   //若失败,则输出错误消息,并终止程序运行
		for (int a = 0; !infile.eof(); a++)
		{
			infile >> temp1 >> temp2;
			point.x = temp1;
			point.y = temp2;
			kp = KeyPoint(point, 1.f);
			features[i - 1].keypoints.push_back(kp);
		}
		infile.close();
		//infile.clear();

		sprintf(descname, "D:\\特征\\desc%d.txt", i); //格式化输出文件名
		ifstream des(descname);
		assert(des.is_open());   //若失败,则输出错误消息,并终止程序运行
		cout << ge[i - 1];
		features[i - 1].descriptors= Mat::zeros(ge[i - 1], 256, CV_32FC1);//同理features[0].descriptors
		for (int k = 0; k < ge[i - 1]; k++)
		{
			for (int j = 0; j < 256; j++)
			{
				des >> features[i - 1].descriptors.at<float>(k, j);
			}
		}

		des.close();
		//des.clear();
	}
	//match_features2(features.descriptor, image_matches); // 特征点匹配
	//gms_match_features(image_keypoints,img0.size(),image_matches);
	for (unsigned int i = 0; i < num_images - 1; i++)
	{
		cout << "正在匹配 " << i << " - " << i + 1 << endl;
		vector<DMatch> matches;
		//match_features1 (image_descriptor[i], image_descriptor[i + 1], matches);

		//使用暴力匹配器进行暴力匹配——BruteForceMatcher类的match()方法

        //opencv3.4使用
		//Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce");
		//matcher->match(features[i].descriptors, features[i + 1].descriptors, matches);

		BruteForceMatcher<L2<float> > matcher;//实例化暴力匹配器
		matcher.match(features[i].descriptors, features[i+1].descriptors, matches);

		cout << "有 " << matches.size() << " 个匹配点" << endl;

		image_matches.push_back(matches);
	}

cout<<"单应性过滤特征点"<<endl;
	//单应性过滤特征点
	for (unsigned int i = 0; i < image_matches.size(); i++)
	{
		refineMatchesWithHomography(features[i].keypoints, features[i+1].keypoints, 1.0, image_matches[i]);
	}
	//image_descriptor.swap(vector<Mat>());//匹配完清除内存
cout<<"narrow";
	Mat img0 = imread(image_names[0]);//读出一个图
	img0 = mynarrow(img0);//如果太大缩小一点。(>2400*1200的)

    //查找单应矩阵
	vector<Mat> im_Homography; // im_Homography[i]表示第i+1-->i的单应矩阵

	for (unsigned int i=0;i<image_matches.size ();i++)
	{

		//单应矩阵
		Mat h12 = myfindHomography(image_matches[i],  features[i].keypoints, features[i+1].keypoints );


		Mat h21;
		invert(h12, h21, DECOMP_LU);
		im_Homography.push_back(h21);

	}


    Mat canvas;
	int canvasSize=image_names.size()*1.5;
	unsigned int j=image_names.size();
		j--;
		Mat img2 = imread(image_names[j]);//读出最后的哪个图

	for (unsigned int i=0;i<image_matches.size ();i++)
	{
		//从后到前
		Mat img1;
		Mat h21;
		j--;
		if(j==image_matches.size ()-1){//最右图
			h21=im_Homography[j];
			//使用透视变换
			warpPerspective(img2, canvas, h21, Size(img0.cols*canvasSize, img0.rows));
			img1 = imread(image_names[j]);//读出最后的哪个图
			//拼接
			img1.copyTo(canvas(Range::all(), Range(0, img0.cols)));
		}
		else{//其它
			h21=im_Homography[j];

			Mat temp2=canvas.clone();        //保存拷贝
			warpPerspective(temp2, canvas, h21, Size(img0.cols*canvasSize, img0.rows));//一起透视变换,结果保存在canvas里
			img1 = imread(image_names[j]);//读出当前的哪个图
			img1.copyTo(canvas(Range::all(), Range(0, img0.cols)));//canvas加img1(拼接)

		}
		/*这个是从右到左,也就是从最后一张图到第一张图逐渐拼接的一个动态结果
		imshow("拼接图",canvas);
		char wname[255];
		sprintf(wname,"can%d.jpg",i);
		imwrite(String(wname),canvas);
		waitKey(1);
		*/
		if(i==image_matches.size ()-1)//保存最后一张拼接图
        {
          imshow("1",canvas);
          waitKey();
          imwrite("result.png",canvas);
        }
	}
	return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值