OpenCV实现图像拼接C++

代码参考 博客1博客2

理论请看博客3 

头文件和公共函数

#include <iostream>
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/stitching.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/xfeatures2d/nonfree.hpp>

一、库实现

int main()
{
	//step 1. load img
	printff("load images");
	std::vector<Mat> imgs;
	Mat img1 = imread("img/img1.png", IMREAD_COLOR);
	Mat img2 = imread("img/img2.png", IMREAD_COLOR);
	imgs.emplace_back(img1);
	imgs.emplace_back(img2);

	//step 2. stitching
	Mat result;
	Stitcher stitcher = Stitcher::createDefault(false);
	Stitcher::Status status = stitcher.stitch(imgs, result);

	if (status != Stitcher::OK)
	{
		std::string error_msg = "Can't stitch images, error code = " + std::to_string(status);
		printff(error_msg);
	}
	else
	{
		imshow("result", result);
		//imwrite("result.png", result);
	}
	waitKey(0);
	return 0;
}

二、手动实现

1. 加载图像

    //step 1. load img
	printff("load images");
	std::vector<Mat> imgs;
	Mat img1 = imread("img/img1.png", IMREAD_COLOR);
	Mat img2 = imread("img/img2.png", IMREAD_COLOR);

 2. 提取SIFT特征

因为SIFT、SURF在美国已经申请了专利,所以并不是免费开源随便使用的,所以我们需要编译对应版本的opencv_contrib,并将OPENCV_ENABLE_NONFREE勾选上。

    //step 2. sift feature detect
	printff("extract sift features");
	std::vector<KeyPoint> keyPoint1, keyPoint2;
	Ptr<Feature2D> siftFeature = xfeatures2d::SIFT::create(2000); //The number of best features to retain

	siftFeature->detect(img1, keyPoint1);
	siftFeature->detect(img2, keyPoint2);

	Mat descor1, descor2;
	siftFeature->compute(img1, keyPoint1, descor1);
	siftFeature->compute(img2, keyPoint2, descor2);

    Mat feature_img1, feature_img2;
	drawKeypoints(img1, keyPoint1, feature_img1, Scalar(0, 255, 0), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
	drawKeypoints(img2, keyPoint2, feature_img2, Scalar(0, 255, 0), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);

	imshow("img1", feature_img1);
	imshow("img2", feature_img2);

 3.匹配

    //step 3. instantiate mathcher
	FlannBasedMatcher matcher;
	std::vector<DMatch> matches;
	matcher.match(descor1, descor2, matches);
	printff("original match numbers: " + std::to_string(matches.size()));

	Mat oriMatchRes;
	drawMatches(img1, keyPoint1, img2, keyPoint2, matches, oriMatchRes, Scalar(0, 255, 0), Scalar::all(-1));
	imshow("orign match img", oriMatchRes);

4.筛选较好的匹配点

    //step 4. select better match
	double sum = 0;
	double maxDist = 0;
	double minDist = 0;
	for (auto &match : matches)
	{
		double dist = match.distance;
		maxDist = max(maxDist, dist);
		minDist = min(minDist, dist);
	}
	printff("max distance: " + std::to_string(maxDist));
	printff("min distance: " + std::to_string(minDist));

	std::vector<DMatch> goodMatches;
	double threshold = 0.5;
	for (auto &match : matches)
	{
		if (match.distance < threshold * maxDist)
			goodMatches.emplace_back(match);
	}

 5. 消除错误匹配特征点

    //step 5.1 align feature points and convet to float
	std::vector<KeyPoint> R_keypoint01, R_keypoint02;
	for (auto &match : goodMatches)
	{
		R_keypoint01.emplace_back(keyPoint1[match.queryIdx]);
		R_keypoint02.emplace_back(keyPoint2[match.trainIdx]);
	}
	std::vector<Point2f> p01, p02;
	for (int i = 0; i < goodMatches.size(); ++i)
	{
		p01.emplace_back(R_keypoint01[i].pt);
		p02.emplace_back(R_keypoint02[i].pt);
	}

	//step 5.2 compute homography
	std::vector<uchar> RansacStatus;
	Mat fundamental = findHomography(p01, p02, RansacStatus, CV_RANSAC);
	Mat dst;
	warpPerspective(img1, dst, fundamental, Size(img1.cols, img1.rows));
	imshow("epipolar image", dst);

	//step 5.3  delete mismatched points
	std::vector<KeyPoint> RR_keypoint01, RR_keypoint02;
	std::vector<DMatch> RR_matches;
	int idx = 0;
	for (int i = 0; i < goodMatches.size(); ++i)
	{
		if (RansacStatus[i] != 0)
		{
			RR_keypoint01.emplace_back(R_keypoint01[i]);
			RR_keypoint02.emplace_back(R_keypoint02[i]);
			goodMatches[i].queryIdx = idx;
			goodMatches[i].trainIdx = idx;
			RR_matches.emplace_back(goodMatches[i]);
			++idx;
		}
	}
	printff("refine match pairs : " + std::to_string(RR_matches.size()));
	Mat imgRRMatches;
	drawMatches(img1, RR_keypoint01, img2, RR_keypoint02, RR_matches, imgRRMatches, Scalar(0, 255, 0), Scalar::all(-1));
	imshow("final match", imgRRMatches);

6. 图像融合 

    //step 6. stitch
	Mat finalImg = dst.clone();
	img2.copyTo(finalImg(Rect(0, 0, img2.cols, img2.rows)));
	imshow("stitching image", finalImg);

完整代码 

vs2017+opencv343​​​​​​​

  • 1
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
OpenCV是一种开源计算机视觉库,可用于处理和分析图像,包括多图片拼接。多图片拼接是将多幅图片按照一定的规则和算法进行连接,形成一幅更大尺寸或更全面的图像。 在使用OpenCV进行多图片拼接时,首先需要将待拼接的图片加载到内存或者直接从摄像头获取图片。然后,根据需求选择适当的拼接算法,常见的拼接算法有简单的拼接、特征点匹配、全景拼接等。 在进行拼接之前,需要对图片进行一些预处理,例如调整图片的尺寸、均衡化图像的直方图、去噪等。这些预处理操作有助于提高拼接的效果和质量。 拼接过程中,关键的一步是特征点匹配。特征点是图像中具有辨识度和重复性的显著性点,通过对图像中的特征点进行匹配,可以确定图片之间的对应关系,从而进行拼接。OpenCV提供了一些特征点检测和匹配的算法,例如SIFT、SURF、ORB等。 在特征点匹配之后,需要进行图像的几何变换以及重叠区域的融合。常见的图像变换方法有仿射变换、透视变换等,这些变换可以根据特征点的位置和匹配关系将图片进行对齐和变换。融合过程中,可以使用像素级别的混合、渐变融合等技术,将不同图片的重叠区域进行平滑地过渡。 最后,通过OpenCV提供的图像保存函数,将拼接好的图像保存到文件或者显示在屏幕上。 总结来说,使用OpenCV进行多图片拼接需要加载图片、进行预处理、特征点匹配、几何变换、重叠区域融合等步骤。通过合理选择算法和参数,可以得到较好的拼接效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值