OpenCv3 VS C++ 图像识别(下)

总结一下:

        cv::KeyPoint——关键点

        cv::Feature2D——找到关键点或计算描述符的抽象类,如上一节的FastFeatureDetector即派生于Feature2D,定义了detect、compute、detectAndCompute等方法

        cv::DMatch——匹配器

        cv::DescriptorMatcher——关键点匹配的抽象类,在这一节我们将在代码中具体使用它们,它定义了match、knnMatch、radiusMatch等方法

#include <opencv2/features2d.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/opencv.hpp>
#include <vector>
#include <iostream>

using namespace std;
using namespace cv;

const float inlier_threshold = 2.5f; // Distance threshold to identify inliers
const float nn_match_ratio = 0.8f;   // Nearest neighbor matching ratio

int main(void)
{
	//1.加载图片和homography矩阵
	Mat img1 = imread("C:\\Users\\ttp\\Desktop\\5.jpg", IMREAD_GRAYSCALE);
	Mat img2 = imread("C:\\Users\\ttp\\Desktop\\4.jpg", IMREAD_GRAYSCALE);

	Mat homography;
	/*FileStorage fs("../data/H1to3p.xml", FileStorage::READ);
	fs.getFirstTopLevelNode() >> homography;*/
	homography = (Mat_<double>(3, 3) << 7.6285898e-01, -2.9922929e-01, 2.2567123e+02,
		3.3443473e-01, 1.0143901e+00, -7.6999973e+01,
		3.4663091e-04, -1.4364524e-05, 1.0000000e+00);

	//2.使用AKAZE检测关键点(keypoints)和计算描述符(descriptors)
	vector<KeyPoint> kpts1, kpts2;		//关键点
	Mat desc1, desc2;					//描述符

	Ptr<AKAZE> akaze = AKAZE::create();
	akaze->detectAndCompute(img1, noArray(), kpts1, desc1);
	akaze->detectAndCompute(img2, noArray(), kpts2, desc2);


	//3.使用brute-force适配器来找到 2-nn 匹配
	BFMatcher matcher(NORM_HAMMING);			//暴力匹配
	vector< vector<DMatch> > nn_matches;
	matcher.knnMatch(desc1, desc2, nn_matches, 2);

	//4.Use 2-nn matches to find correct keypoint matches
	vector<KeyPoint> matched1, matched2, inliers1, inliers2;
	vector<DMatch> good_matches;
	for (size_t i = 0; i < nn_matches.size(); i++) {
		DMatch first = nn_matches[i][0];
		float dist1 = nn_matches[i][0].distance;
		float dist2 = nn_matches[i][1].distance;

		if (dist1 < nn_match_ratio * dist2) {
			matched1.push_back(kpts1[first.queryIdx]);
			matched2.push_back(kpts2[first.trainIdx]);
		}
	}
	//5.Check if our matches fit in the homography model
	for (unsigned i = 0; i < matched1.size(); i++) {
		Mat col = Mat::ones(3, 1, CV_64F);
		col.at<double>(0) = matched1[i].pt.x;
		col.at<double>(1) = matched1[i].pt.y;

		col = homography * col;
		col /= col.at<double>(2);
		double dist = sqrt(pow(col.at<double>(0) - matched2[i].pt.x, 2) +
			pow(col.at<double>(1) - matched2[i].pt.y, 2));

		if (dist < inlier_threshold) {
			int new_i = static_cast<int>(inliers1.size());
			inliers1.push_back(matched1[i]);
			inliers2.push_back(matched2[i]);
			good_matches.push_back(DMatch(new_i, new_i, 0));
		}
	}

	Mat res;
	drawMatches(img1, inliers1, img2, inliers2, good_matches, res);
	imshow("res", res);

	double inlier_ratio = inliers1.size() * 1.0 / matched1.size();
	cout << "A-KAZE Matching Results" << endl;
	cout << "*******************************" << endl;
	cout << "# Keypoints 1:                        \t" << kpts1.size() << endl;
	cout << "# Keypoints 2:                        \t" << kpts2.size() << endl;
	cout << "# Matches:                            \t" << matched1.size() << endl;
	cout << "# Inliers:                            \t" << inliers1.size() << endl;
	cout << "# Inliers Ratio:                      \t" << inlier_ratio << endl;
	cout << endl;
	waitKey(0);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40515692/article/details/86410549