Shi-Tomas角点检测、亚像素级别角点位置优化、ORB特征点、特征点匹配、RANSAC优化特征点匹配、相机模型与投影

目录

1、Shi-Tomas角点检测

2、亚像素级别角点位置优化

3、ORB特征点

4、特征点匹配

5、RANSAC优化特征点匹配

6、相机模型与投影


1、Shi-Tomas角点检测

Shi-Tomas角点检测、亚像素级别角点位置优化、ORB特征点、特征点匹配、RANSAC优化特征点匹配、相机模型与投影_第1张图片

 Shi-Tomas角点检测、亚像素级别角点位置优化、ORB特征点、特征点匹配、RANSAC优化特征点匹配、相机模型与投影_第2张图片

//Shi-Tomas角点检测
int test1()
{
	Mat img = imread("F:/testMap/lena.png");
	if (!img.data)
	{
		cout << "读取图像错误, 请确认图像文件是否正确" << endl; 
		return -1;
	}
		
	Mat gray;
	cvtColor(img, gray,COLOR_BGR2GRAY);
	//Detector parametersl
	
	//提取角点
	int maxCorners = 100;//检测角点数目
	double quality_level = 0.01;//质量等级,或者说阈值与最佳角点的比例关系
	double minDistance = 0.04;//两个角点之间的最小欧式距离
	vector corners;
	goodFeaturesToTrack(gray,corners,maxCorners,quality_level, minDistance,Mat(),3,false);
	
	//绘制角点
	vector keyPoints;//存放角点的KeyPoint类,用于后期绘制角点时用

	for (int i = 0; i < corners.size(); i++)
	{
		//将角点存放在KeyPoint类中
		KeyPoint keyPoint;
		keyPoint.pt = corners[i];
		keyPoints.push_back(keyPoint);
	}

	//用drawKeypoints()函数绘制角点坐标
	drawKeypoints(img, keyPoints, img);
	imshow("角点结果", img); 
	waitKey(0);
	return 0;
}

 2、亚像素级别角点位置优化

Shi-Tomas角点检测、亚像素级别角点位置优化、ORB特征点、特征点匹配、RANSAC优化特征点匹配、相机模型与投影_第3张图片

 Shi-Tomas角点检测、亚像素级别角点位置优化、ORB特征点、特征点匹配、RANSAC优化特征点匹配、相机模型与投影_第4张图片

//亚像素级别角点位置优化
int test2()
{
	system("color 02");//改变DOS界面颜色
	Mat img = imread("F:/testMap/lena.png", IMREAD_COLOR);
	if (!img.data)
	{
		cout << "读取图像错误,请确认图像文件是否正确" << endl;
		return -1;
	}
	//彩色图像转成灰度图像
	Mat gray;
	cvtColor(img, gray, COLOR_BGR2GRAY);

	//提取角点
	int maxCorners = 100;//检测角点数目
	double quality_level = 0.01;//质量等级,或者说阈值与最佳角点的比例关系
	double minDistance = 0.04;//两个角点之间的最小欧式距离

	vector corners;
	goodFeaturesToTrack(gray, corners, maxCorners, quality_level, minDistance, Mat(), 3, false);

	//计算亚像素级别角点坐标
	vector cornersSub = corners;//角点备份,防止被函数修改
	Size winSize = Size(5, 5);
	Size zeroZone = Size(-1, -1);
	TermCriteria criteria = TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 40, 0.001);
	cornerSubPix(gray, cornersSub, winSize, zeroZone, criteria);

	//输出初始坐标和精细坐标
	for (size_t i = 0; i < corners.size(); i++)
	{
		string str = to_string(i);
		str = "第" + str + "个角点点初始坐标:";
		cout << str << corners[i] << "精细后坐标:" << cornersSub[i] << endl;
	}

	waitKey(0);
	return 0;
}

3、ORB特征点

Shi-Tomas角点检测、亚像素级别角点位置优化、ORB特征点、特征点匹配、RANSAC优化特征点匹配、相机模型与投影_第5张图片

 Shi-Tomas角点检测、亚像素级别角点位置优化、ORB特征点、特征点匹配、RANSAC优化特征点匹配、相机模型与投影_第6张图片

 Shi-Tomas角点检测、亚像素级别角点位置优化、ORB特征点、特征点匹配、RANSAC优化特征点匹配、相机模型与投影_第7张图片

 

//ORB特征点
int test3()
{
	Mat img = imread("F:/testMap/lena.png");
	if (!img.data)
	{
		cout << "请确认图像文件名称是否正确" << endl;
		return -1;
	}

	//创建ORB特征点类变量
	Ptr orb = ORB::create(500,//特征点数目
		1.2f,//金字塔层级之间的缩放比例
		8,//金字塔图像层数系数
		31,//边缘阈值
		0,//原图在金字塔中的层数
		2,//生成描述子时需要用的像素点数目
		ORB::HARRIS_SCORE,//使用 Harris方法评价特征点
		31,//生成描述子时关键点周围邻域的尺寸
		20//计算 FAST角点时像素值差值的阈值
		);

	//计算ORB关键点
	vector Keypoints;
	orb->detect(img, Keypoints);//确定关键点

	//计算ORB描述子
	Mat descriptions;
	orb->compute(img, Keypoints, descriptions);//计算描述子

	//绘制特征点
	Mat imgAngel;
	img.copyTo(imgAngel);
	//绘制不含角度和大小的结果
	drawKeypoints(img, Keypoints, img, Scalar(255, 255, 255));//绘制含有角度和大小的结果
	drawKeypoints(img, Keypoints, imgAngel, Scalar(255, 255, 255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);

	//显示结果
	imshow("不含角度和大小的结果", img);
	imshow("含有角度和大小的结果", imgAngel);

	waitKey(0);
	return 0;
}

4、特征点匹配

Shi-Tomas角点检测、亚像素级别角点位置优化、ORB特征点、特征点匹配、RANSAC优化特征点匹配、相机模型与投影_第8张图片

Shi-Tomas角点检测、亚像素级别角点位置优化、ORB特征点、特征点匹配、RANSAC优化特征点匹配、相机模型与投影_第9张图片

 Shi-Tomas角点检测、亚像素级别角点位置优化、ORB特征点、特征点匹配、RANSAC优化特征点匹配、相机模型与投影_第10张图片

Shi-Tomas角点检测、亚像素级别角点位置优化、ORB特征点、特征点匹配、RANSAC优化特征点匹配、相机模型与投影_第11张图片

 Shi-Tomas角点检测、亚像素级别角点位置优化、ORB特征点、特征点匹配、RANSAC优化特征点匹配、相机模型与投影_第12张图片

//特征点匹配
void orb_features(Mat &gray,vector &keypionts,Mat &descriptions)
{
	Ptr orb = ORB::create(1000,1.2f); 
	orb->detect(gray,keypionts);
	orb->compute(gray,keypionts,descriptions);
}

int test1()
{
	Mat img1, img2;
	img1 = imread("F:/testMap/box.png");
	img2 = imread("F:/testMap/box_in_scen.png");
	if (!(img1.data && img2.dataend))
	{
		cout << "读取图像错误,请确认图像文件是否正确" << endl;
		return -1;
	}
	//提取ORB特征点
	vector Keypoints1, Keypoints2;
	Mat descriptions1, descriptions2;
	//计算特征点
	orb_features(img1, Keypoints1, descriptions1);
	orb_features(img2, Keypoints2, descriptions2);

	//特征点匹配
	vector matches;//定义存放匹配结果的变最
	BFMatcher matcher(NORM_HAMMING);//定义特征点匹配的类,使用汉明距离
	matcher.match(descriptions1, descriptions2, matches);//进行特征点匹配
	cout << "matches=" << matches.size() << endl;//匹配成功特征点数目

	//通过汉明距离删选匹配结果
	double min_dist = 0, max_dist = 10000;
	for (int i = 0; i < matches.size(); i++)
	{
		double dist = matches[i].distance;
		if (dist < min_dist) min_dist = dist;
		if (dist > max_dist) max_dist = dist;
	}
	//输出所有匹配结果中最大韩明距离和最小汉明距离
	cout << "min_dist=" << min_dist << endl;
	cout << "max_dist=" << max_dist << endl;

	//将汉明距离较大的匹配点对删除
	vectorgood_matches;
	for (int i = 0; i < matches.size(); i++)
	{
		if (matches[i].distance <= max(2 * min_dist, 20.0))
		{
			good_matches.push_back(matches[i]);
		}
	}
	cout << "good_min=" << good_matches.size() << endl;//剩余特征点数目

	//绘制匹配结果
	Mat outimg, outimg1;
	drawMatches(img1, Keypoints1, img2, Keypoints2, matches, outimg);
	drawMatches(img1, Keypoints1, img2, Keypoints2, good_matches, outimg1);

	imshow("未筛选结果", outimg);
	imshow("最小汉明距离筛选", outimg1);

	waitKey(0);
	return 0;
}


5、RANSAC优化特征点匹配

Shi-Tomas角点检测、亚像素级别角点位置优化、ORB特征点、特征点匹配、RANSAC优化特征点匹配、相机模型与投影_第13张图片

 Shi-Tomas角点检测、亚像素级别角点位置优化、ORB特征点、特征点匹配、RANSAC优化特征点匹配、相机模型与投影_第14张图片

 

void match_min(vector matches, vector & good_matches)//最小汉明距离
{
	double min_dist = 10000, max_dist = 0;
	for (int i = 0; i < matches.size(); i++)
	{
		double dist = matches[i].distance;
		if (dist < min_dist)  min_dist = dist;
		if (dist > max_dist)  max_dist = dist;
	}
	cout << "min_dist=" << min_dist << endl;
	cout << "max_dist=" << max_dist << endl;
	for (int i = 0; i < matches.size(); i++)
	{
		if (matches[i].distance <= max(2 * min_dist, 20.0))
		{
			good_matches.push_back(matches[i]);
		}
	}
}
//RANSAC算法实现
void ransac(vectormatches, vector  queryifeyPoint, vector trainieyPoint, vector &kmatches_ransac)
{
	//定义保存匹配点对坐标
	vector srcPoints(matches.size()), dstPoints(matches.size());
	//保存从关键点中提取到的匹配点对的坐标
	for (int i = 0; i < matches.size(); i++)
	{
		srcPoints[i] = queryifeyPoint[matches[i].queryIdx].pt;
		dstPoints[i] = trainieyPoint[matches[i].trainIdx].pt;
	}

	//匹配点对进行RANSAC过滤
	vector inliersMask(srcPoints.size());
	// Mat homography;
	//homography = findHomography(srcPoints, dstPoints, RANSAC, 5, inliersMask); 
	findHomography(srcPoints, dstPoints, RANSAC, 5, inliersMask);
	//手动的保留RANSAC过滤后的匹配点对
	for (int i = 0; i < inliersMask.size(); i++)
	{
		if (inliersMask[i])
		{
			kmatches_ransac.push_back(matches[i]);
		}
	}
}
int test5()
{
	Mat img1, img2;
	img1 = imread("F:/testMap/box.png");
	img2 = imread("F:/testMap/box_in_scen.png");
	if (!(img1.data && img2.dataend))
	{
		cout << "读取图像错误,请确认图像文件是否正确" << endl;
		return -1;
	}
	//提取ORB特征点
	vector Keypoints1, Keypoints2;
	Mat descriptions1, descriptions2;
	//计算特征点
	orb_features(img1, Keypoints1, descriptions1);
	orb_features(img2, Keypoints2, descriptions2);

	//特征点匹配
	vector matches, good_min, good_ransac;
	BFMatcher matcher(NORM_HAMMING);
	matcher.match(descriptions1, descriptions2, matches);
	cout << "matches=" << matches.size() << endl;

	//最小汉明距离
	match_min(matches, good_min);
	cout << "good_min=" << good_min.size() << endl;

	//用ransac算法筛选匹配结果
	ransac(good_min, Keypoints1, Keypoints2, good_ransac);
	cout << "good_matches.size=" << good_ransac.size() << endl;

	//绘制匹配结果
	Mat outimg, outimg1, outimg2;
	drawMatches(img1,Keypoints1,img2,Keypoints2, matches,outimg); 
	drawMatches(img1, Keypoints1, img2, Keypoints2, good_min, outimg1);
	drawMatches(img1,Keypoints1,img2,Keypoints2, good_ransac,outimg2); 
	imshow("未筛选结果", outimg);
	imshow("最小汉明距离筛选", outimg1) ;
	imshow("ransac筛选",outimg2);

	waitKey(0);
	return 0;
}

6、相机模型与投影

Shi-Tomas角点检测、亚像素级别角点位置优化、ORB特征点、特征点匹配、RANSAC优化特征点匹配、相机模型与投影_第15张图片

 Shi-Tomas角点检测、亚像素级别角点位置优化、ORB特征点、特征点匹配、RANSAC优化特征点匹配、相机模型与投影_第16张图片

 Shi-Tomas角点检测、亚像素级别角点位置优化、ORB特征点、特征点匹配、RANSAC优化特征点匹配、相机模型与投影_第17张图片Shi-Tomas角点检测、亚像素级别角点位置优化、ORB特征点、特征点匹配、RANSAC优化特征点匹配、相机模型与投影_第18张图片

Shi-Tomas角点检测、亚像素级别角点位置优化、ORB特征点、特征点匹配、RANSAC优化特征点匹配、相机模型与投影_第19张图片

 Shi-Tomas角点检测、亚像素级别角点位置优化、ORB特征点、特征点匹配、RANSAC优化特征点匹配、相机模型与投影_第20张图片

//相机模型与投影
int test4()
{
	//输入计算得到的内参矩阵和畸变矩阵
	Mat cameraMatrix = (Mat_(3,3) << 532.016297,0,332.172519,0,531.565159,233.388075,0, 0,1);
	Mat distCoeffs = (Mat_(1,5) << -0.285188,0.080097,0.001274,-0.002415,0.106579);
	
	//代码清单10-10中计算的第一张图像相机坐标系与世界坐标系之间的关系
	Mat rvec = (Mat_(1,3)<<-1.977853,-2.002220,0.130029);
	Mat tvec = (Mat_(1,3) <<-26.88155,-42.79936,159.19703);
	
	//生成第一张图像中内角点的三维世界坐标
	Size boardSize = Size(9, 6);
	Size squareSize = Size(10,10);//棋盘格每个方格的真实尺寸
	vector PointSets;
	
	for (int j = 0; j < boardSize.height; j++)
	{
		for (int k = 0; k < boardSize.width; k++)
		{
			Point3f realPoint;
			//假设标定板为世界坐标系的z平面,即z=0
			realPoint.x = j*squareSize.width;
			realPoint.y = k*squareSize.height; 
			realPoint.z = 0;
			PointSets.push_back(realPoint);
		}
	}

	//根据三维坐标和相机与世界坐标系时间的关系估计内角点像素坐标
	vectorimagePoints;
	projectPoints(PointSets,rvec,tvec,cameraMatrix, distCoeffs, imagePoints);
	for(int i = 0; i < imagePoints.size(); i++)
	{
		cout << "第" << to_string(i) << "个点的坐标" << imagePoints[i] << endl;
	}
		


	waitKey(0);
	return 0;
}

你可能感兴趣的:(OpenCV学习笔记,opencv,图像处理,算法)