Features2D + Homography to find a known object
——使用二维特征点(Features2D)和单映射(Homography)寻找已知物体
一.主要函数
1.findHomography() [1/2]
Mat cv::findHomography | ( | InputArray | srcPoints, |
---|---|---|---|
InputArray | dstPoints, | ||
int | method = 0 , |
||
double | ransacReprojThreshold = 3 , |
||
OutputArray | mask = noArray() , |
||
const int | maxIters = 2000 , |
||
const double | confidence = 0.995 |
||
) |
参数详解:
srcPoints
源平面中点的坐标矩阵,可以是CV_32FC2类型,也可以是vector类型
dstPoints
目标平面中点的坐标矩阵,可以是CV_32FC2类型,也可以是vector类型
method
计算单应矩阵所使用的方法。不同的方法对应不同的参数,具体如下:
0 - 利用所有点的常规方法
RANSAC - RANSAC-基于RANSAC的鲁棒算法
LMEDS - 最小中值鲁棒算法
RHO - PROSAC-基于PROSAC的鲁棒算法
ransacReprojThreshold
将点对视为内点的最大允许重投影错误阈值(仅用于RANSAC和RHO方法)。如果
则点被认为是个外点(即错误匹配点对)。若srcPoints和dstPoints是以像素为单位的,则该参数通常设置在1到10的范围内。
mask
可选输出掩码矩阵,通常由鲁棒算法(RANSAC或LMEDS)设置。 请注意,输入掩码矩阵是不需要设置的。
maxIters
RANSAC算法的最大迭代次数,默认值为2000。
confidence
置信度0~1
参考博客:
https://blog.csdn.net/fengyeer20120/article/details/87798638
2.perspectiveTransform()
void cv::perspectiveTransform | ( | InputArray | src, |
---|---|---|---|
OutputArray | dst, | ||
InputArray | m | ||
) |
参考博客:
https://blog.csdn.net/qxd12345678/article/details/43056185
二.代码实现
#include <iostream>
#include "opencv2/core.hpp"
#ifdef HAVE_OPENCV_XFEATURES2D
#include "opencv2/calib3d.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/xfeatures2d.hpp"
using namespace cv;
using namespace cv::xfeatures2d;
using std::cout;
using std::endl;
const char* keys =
"{ help h | | Print help message. }"
"{ input1 | ../data/box.png | Path to input image 1. }"
"{ input2 | ../data/box_in_scene.png | Path to input image 2. }";
int main( int argc, char* argv[] )
{
CommandLineParser parser