欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

orb特征描述符 物体匹配

程序员文章站 2022-03-16 21:41:41
...
#include <iostream>  
#include <opencv2/core/core.hpp>   
#include <opencv2/highgui/highgui.hpp>   
#include <opencv2/imgproc/imgproc.hpp>   
#include <opencv2/features2d/features2d.hpp>  

using namespace cv;
using namespace std;


/********************************************************************************************************
函数描述:
计算图像中的ORB特征及其特征点的匹配
*********************************************************************************************************/
void colorReduce(Mat& inputImage, Mat& outputImage, int div)
{
	//参数准备
	outputImage = inputImage.clone();  //拷贝实参到临时变量
	int rowNumber = outputImage.rows;  //行数
	int colNumber = outputImage.cols*outputImage.channels();  //列数 x 通道数=每一行元素的个数

	//双重循环,遍历所有的像素值
	for (int i = 0; i < rowNumber; i++)  //行循环
	{
		uchar* data = outputImage.ptr<uchar>(i);  //获取第i行的首地址
		for (int j = 0; j < colNumber; j++)   //列循环
		{
			// ---------【开始处理每个像素】-------------     
			data[j] = data[j] / div*div + div / 2;
			// ----------【处理结束】---------------------
		}  //行处理结束
	}
}
void split_orb(Mat &sorse, vector<KeyPoint>&orb_vec)
{

	ORB orb(/*int nfeatures = */30,/* float scaleFactor = */2.0f,/* int nlevels = */8,/* int edgeThreshold = */31,
		/*int firstLevel = */0,/* int WTA_K = */2,/* int scoreType = */ORB::HARRIS_SCORE,/* int patchSize = */31);
	if (sorse.data == NULL)
	{
		cout << "no data";
		return;
	}
	for (int i = 0; i < sorse.cols; i += 100)
	{
		if (i + 100 >= sorse.cols)continue;
		for (int j = 0; j < sorse.rows; j += 100)
		{
			vector<KeyPoint>temp_vec;
			if (j + 100 >= sorse.rows)continue;
			Mat temp(sorse, Rect(i, j, 100, 100));
			orb(temp, Mat(), temp_vec);
			for (int f = 0; f < temp_vec.size(); f++)
			{
				temp_vec[f].pt.x += i;
				temp_vec[f].pt.y += j;
				orb_vec.push_back(temp_vec[f]);
			}
		}
	}

}
void	ORBdetect(Mat& image, std::vector<cv::KeyPoint>&  keyPoints)
{
	Mat orbImage;

	ORB orb;
	Mat outputImage = image.clone();  //拷贝实参到临时变量
	int rowNumber = outputImage.rows;  //行数
	int colNumber = outputImage.cols/**outputImage.channels()*/;  //列数 x 通道数=每一行元素的个数
	//Mat mLeftView = m_mView(cv::Rect(0, 0, 640, 480));

	//双重循环,遍历所有的像素值
	for (int i = 0; i < rowNumber; i += 100)  //行循环
	{
		if (i + 100 >= rowNumber) continue/*i = rowNumber - 100*/;
		for (int j = 0; j < colNumber; j += 100)   //列循环
		{
			if (j + 100 >= colNumber) continue/*j = colNumber - 100*/;
			//Mat mLeftView = outputImage(cv::Rect(i, j, 100, 100));
			Mat mLeftView(outputImage, cv::Rect(i, j, 100, 100));
			//orb.operator()(mLeftView, Mat(), keyPoints1, orbImage);

			std::vector<cv::KeyPoint>  keyPoints1;
			orb(mLeftView, Mat(), keyPoints1);
			for (int k = 0; k < keyPoints1.size(); k++)
			{
				keyPoints1[k].pt.x += i;
				keyPoints1[k].pt.y += j;
				keyPoints.push_back(keyPoints1[k]);
 			}
	
		}  //行处理结束
	}

}

bool cacORBFeatureAndCompare(cv::Mat srcImg_1, cv::Mat srcImg_2)
{
	//【1】图像中ORB关键点的检测  
	std::vector<cv::KeyPoint>  keyPoints_1;
	std::vector<cv::KeyPoint>  keyPoints_2;
	std::vector<cv::KeyPoint>  keyPoints_3;


	cv::ORB  orb;
	cv::Mat descriptorMat_1;                                   //【1】图像1的特征点描述子  

	orb.operator()(srcImg_1, Mat(), keyPoints_1, descriptorMat_1);
	//orb.detect(srcImg_1, keyPoints_1);                          //【1】图像1中ORB关键点的检测  
	//orb.detect(srcImg_2, keyPoints_2);                          //【2】图像2中ORB关键点的检测  

	//ORBdetect(srcImg_2, keyPoints_2);
	split_orb(srcImg_2, keyPoints_2);

	//【2】计算特征向量(特征点的描述子)  
	//cv::Mat descriptorMat_1;                                   //【1】图像1的特征点描述子  
	cv::Mat descriptorMat_2;                                   //【2】图像1的特征点描述子  

	//orb.compute(srcImg_1, keyPoints_1, descriptorMat_1);         //【1】计算图像1的特征向量  
	orb.compute(srcImg_2, keyPoints_2, descriptorMat_2);         //【2】计算图像2的特征向量  

	//【3】特征点的匹配  
	cv::BFMatcher        matcher(NORM_HAMMING);
	std::vector<DMatch>  matches;
	matcher.match(descriptorMat_1, descriptorMat_2, matches);

	//【4】绘制匹配点集  
	cv::Mat matchMat;
	cv::drawMatches(srcImg_1, keyPoints_1, srcImg_2, keyPoints_2, matches, matchMat);
	Mat srcI;
	cv::drawKeypoints(srcImg_1, keyPoints_1, srcI, cvScalar(0,0,255));
	cv::imshow("src1", srcI);
	cv::imshow("matchMat", matchMat);
	cv::waitKey(0);
	return true;
}

//int main17063000(int argc, char** argv)
int main(int argc, char** argv)
{
	cv::Mat srcImg_1 = imread("4.jpg");
	if (srcImg_1.empty())
	{
		std::cout << "【NOTICE】NO valid inout image_1 was given,please check the inoput image! " << std::endl;
		std::system("pause");
		return -1;
	}
	cv::Mat srcImg_2 = imread("5.jpg");
	if (srcImg_1.empty())
	{
		std::cout << "【NOTICE】NO valid inout image_2 was given,please check the inoput image! " << std::endl;
		std::system("pause");
		return -1;
	}
	//ORB特征点的检测和匹配  
	cacORBFeatureAndCompare(srcImg_1, srcImg_2);
	return 0;
}

相关标签: opencv orb