The first section feature detection and description
OpenCV provides a wealth of feature detection algorithms, such as SIFT (Scale Invariant Feature Transform), AffineFeature, AgastFeatureDetector, AKAZE, BRISK, FastFeatureDetector, GFTTDetector, KAZE, MSER, ORB, SimpleBlobDetector, etc.
1、SIFT
Scale-invariant feature transformation algorithm to extract image features
The SIFT class inherits the cv::Feature2D class and is created by the create static method.
static Ptr cv::SIFT::create(int nfeatures = 0,int nOctaveLayers = 3,double contrastThreshold = 0.04,double edgeThreshold = 10,double sigma = 1.6)
The parameters are as follows:
parameter name | Parameter Description |
---|---|
nfeatures | The number of best features retained; features are ranked by their scores |
nOctaveLayers | The number of layers in each octave. 3 is the value used in D.Lowe. The number of octaves is automatically calculated based on the image resolution. |
contrastThreshold | Contrast threshold, used to filter weak points in semi-uniform (low contrast) areas. The larger the threshold, the fewer features produced by the detector. |
edgeThreshold | The threshold used to filter edge features. Please note that its meaning is different from contrastThreshold, that is, the larger the edgeThreshold, the fewer features are filtered out (the more features are retained). |
sigma | Gaussian sigma is applied to the input image with octave #0. If you use a weak camera with a soft lens to capture images, you may need to reduce the number. |
Note: When applying filtering, the contrast threshold will be divided by nOctaveLayers. When nOctaveLayers is set to the default value and if you want to use the value 0.03 used in the D.Lowe paper, set this parameter to 0.09.
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
int main()
{
// 读取图像
cv::Mat src = cv::imread("images/f1.jpg");
if(src.empty()){
cerr << "cannot read image.\n";
return EXIT_FAILURE;
}
// 转换成灰度图像
cv::Mat gray;
cv::cvtColor(src,gray,cv::COLOR_BGR2GRAY);
// 创建SIFT特征提取器
cv::Ptr<cv::SIFT> feature = cv::SIFT::create(1024,3,0.04,120,1.5);
// 检测特征点
vector<cv::KeyPoint> keypoints;
cv::Mat descriptor;
feature->detectAndCompute(gray,cv::Mat(),keypoints,descriptor);
// 绘制特征点
cv::Mat output;
cv::drawKeypoints(src,keypoints,output,cv::Scalar(0,0,255));
cv::imshow("src",src);
cv::imshow("output",output);
cv::waitKey();
return 0;
}
2、 AffineFeature
The class used to implement wrappers that make detectors and extractors have affine invariants, in [Guoshen Yu and Jean-Michel Morel. Asift: An algorithm for fully affine invariant comparison . Image Processing On Line , 1:11–38 , 2011.] described as ASIFT.
The AffineFeature class inherits the cv::Feature2D class and is created through the create static method.
static Ptr cv::AffineFeature::create(const Ptr< Feature2D > & backend,int maxTilt = 5,int minTilt = 0,float tiltStep = 1.4142135623730951f,float rotateStepBase = 72)
The parameters are as follows:
parameter | Parameter Description |
---|---|
backend | Used as a back-end detector/extractor. |
maxTilt | The highest power index of the tilt factor. Use 5 as the slope sampling range n in the paper. |
minTilt | The lowest tilt coefficient power index. This article uses 0. |
tiltStep | Tilt sampling step δt. |
rotateStepBase | Rotating sampling step coefficient b |
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
int main()
{
// 读取图像
cv::Mat src = cv::imread("images/f1.jpg");
if(src.empty()){
cerr << "cannot read image.\n";
return EXIT_FAILURE;
}
// 转换成灰度图像
cv::Mat gray;
cv::cvtColor(src,gray,cv::COLOR_BGR2GRAY);
// 使用SIFT特征检测算法创建
cv::Ptr<cv::AffineFeature> feature = cv::AffineFeature::create(cv::SIFT::create(128));
// 检测特征点
vector<cv::KeyPoint> keypoints;
cv::Mat descriptor;
feature->detectAndCompute(gray,cv::Mat(),keypoints,descriptor);
// 绘制特征点
cv::Mat output;
cv::drawKeypoints(src,keypoints,output,cv::Scalar(0,0,255));
cv::imshow("src",src);
cv::imshow("output",output);
cv::waitKey();
return 0;
}
3、AgastFeatureDetector
Packages that use AGAST method for feature detection.
The AgastFeatureDetector class inherits the cv::Feature2D class and is created through the create static method.
static Ptr cv::AgastFeatureDetector::create(int threshold = 10,bool nonmaxSuppression = true,AgastFeatureDetector::DetectorType type = AgastFeatureDetector::OAST_9_16)
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
int main()
{
// 读取图像
cv::Mat src = cv::imread("images/f1.jpg");
if(src.empty()){
cerr << "cannot read image.\n";
return EXIT_FAILURE;
}
// 转换成灰度图像
cv::Mat gray;
cv::cvtColor(src,gray,cv::COLOR_BGR2GRAY);
// 创建AgastFeatureDetector特征提取器
cv::Ptr<cv::AgastFeatureDetector> feature = cv::AgastFeatureDetector::create();
// 检测特征点
vector<cv::KeyPoint> keypoints;
cv::Mat descriptor;
feature->detect(gray,keypoints);
// 绘制特征点
cv::Mat output;
cv::drawKeypoints(src,keypoints,output,cv::Scalar(0,0,255));
cv::imshow("src",src);
cv::imshow("output",output);
cv::waitKey();
return 0;
}
4 、 AKAZE
Realize the class of AKAZE key point detector and descriptor extractor, in [Pablo F Alcantarilla, Jesús Nuevo, and Adrien Bartoli. Fast explicit diffusion for accelerated features in nonlinear scale spaces . Trans. Pattern Anal. Machine Intell , 34(7) :1281–1298, 2011.].
The AKAZE class inherits the cv::Feature2D class and is created through the create static method.
static Ptr cv::AKAZE::create (AKAZE::DescriptorType descriptor_type = AKAZE::DESCRIPTOR_MLDB,int descriptor_size = 0,int descriptor_channels = 3,float threshold = 0.001f,int nOctaves = 4,int nOctaveLayers = 4,KAZE::DiffusivityType diffusivity = KAZE::DIFF_PM_G2)
parameter name | Parameter Description |
---|---|
descriptor_type | The type of the extracted descriptor: DESCRIPTOR_KAZE, DESCRIPTOR_KAZE_UPRIGHT, DESCRIPTOR_MLDB or DESCRIPTOR_MLDB_UPRIGHT. |
descriptor_size | The size of the descriptor (in bits). 0->full size |
descriptor_channels | Number of channels in the descriptor (1, 2, 3) |
threshold | Detector response threshold to accept points |
nOctaves | Maximum octave evolution of the image |
nOctaveLayers | Default number of sub-levels for each scale level |
diffusivity | Diffusion type. DIFF_PM_G1, DIFF_PM_G2, DIFF_WEICKERT or DIFF_CHARBONNIER |
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
int main()
{
// 读取图像
cv::Mat src = cv::imread("images/f1.jpg");
if(src.empty()){
cerr << "cannot read image.\n";
return EXIT_FAILURE;
}
// 转换成灰度图像
cv::Mat gray;
cv::cvtColor(src,gray,cv::COLOR_BGR2GRAY);
// 创建AKAZE特征提取器
cv::Ptr<cv::AKAZE> feature = cv::AKAZE::create();
// 检测特征点
vector<cv::KeyPoint> keypoints;
cv::Mat descriptor;
feature->detect(gray,keypoints);
// 绘制特征点
cv::Mat output;
cv::drawKeypoints(src,keypoints,output,cv::Scalar(0,0,255));
cv::imshow("src",src);
cv::imshow("output",output);
cv::waitKey();
return 0;
}
5、BRISK
The class that implements the BRISK keypoint detector and descriptor extractor is described in [Stefan Leutenegger, Margarita Chli, and Roland Yves Siegwart. Brisk: Binary robust invariant scalable keypoints. In Computer Vision (ICCV), 2011 IEEE International Conference on , pages 2548 -2555. IEEE, 2011.].
The BRISK class inherits the cv::Feature2D class and is created through the create static method.
static Ptr cv::BRISK::create(int thresh = 30,int octaves=3,float patternScale=1.0f)
parameter name | Parameter Description |
---|---|
thresh | The AGAST detection threshold is worth points. |
octaves | Detect octave. Use 0 for single scale. |
patternScale | This ratio is applied to the pattern used to sample the neighborhood of key points. |
Other function overloading forms:
BRISK constructor for custom mode.
static Ptr cv::BRISK::create (const std::vector< float > & radiusList,const std::vector< int > & numberList,float dMax = 5.85f,float dMin = 8.2f,const std::vector< int > & indexChange=std::vector< int >())
parameter name | Parameter Description |
---|---|
numberList | Define the number of sampling points on the sampling circle. Must be the same size as radiusList. |
radiusList | Define the radius (in pixels) sampled around the keypoint (for keypoint scale 1). |
dMax | Threshold for short pairing used for descriptor formation (for keypoint ratio 1, in pixels). |
dMin | Threshold for long pairing used for orientation determination (for keypoint ratio 1, in pixels). |
indexChange | The index of the bit is remapped. |
static Ptr cv::BRISK::create (int thresh,int octaves,const std::vector< float > & radiusList,const std::vector< int > & numberList,float dMax = 5.85f,float dMin = 8.2f,const std::vector< int > & indexChange = std::vector< int >())
parameter name | Parameter Description |
---|---|
thresh | The AGAST detection threshold is worth points. |
octaves | Detect octave. Use 0 for single scale. |
radiusList | Define the radius (in pixels) sampled around the keypoint (for keypoint scale 1). |
numberList | Define the number of sampling points on the sampling circle. Must be the same size as radiusList. |
dMax | Threshold for short pairing used for descriptor formation (for keypoint ratio 1, in pixels). |
dMin | Threshold for long pairing used for orientation determination (for keypoint ratio 1, in pixels). |
indexChange | The index of the bit is remapped. |
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
int main()
{
// 读取图像
cv::Mat src = cv::imread("images/f1.jpg");
if(src.empty()){
cerr << "cannot read image.\n";
return EXIT_FAILURE;
}
// 转换成灰度图像
cv::Mat gray;
cv::cvtColor(src,gray,cv::COLOR_BGR2GRAY);
// 创建BRISK特征提取器
cv::Ptr<cv::BRISK> feature = cv::BRISK::create();
// 检测特征点
vector<cv::KeyPoint> keypoints;
cv::Mat descriptor;
feature->detect(gray,keypoints);
// 绘制特征点
cv::Mat output;
cv::drawKeypoints(src,keypoints,output,cv::Scalar(0,0,255));
cv::imshow("src",src);
cv::imshow("output",output);
cv::waitKey();
return 0;
}
6、FastFeatureDetector
Packages that use FAST method for feature detection. The FastFeatureDetector class inherits the cv::Feature2D class and is created by the create static method.
static Ptr cv::FastFeatureDetector::create(int threshold = 10,bool nonmaxSuppression = true,FastFeatureDetector::DetectorType type = FastFeatureDetector::TYPE_9_16)
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
int main()
{
// 读取图像
cv::Mat src = cv::imread("images/f1.jpg");
if(src.empty()){
cerr << "cannot read image.\n";
return EXIT_FAILURE;
}
// 转换成灰度图像
cv::Mat gray;
cv::cvtColor(src,gray,cv::COLOR_BGR2GRAY);
// 创建FastFeatureDetector特征提取器
cv::Ptr<cv::FastFeatureDetector> feature = cv::FastFeatureDetector::create();
// 检测特征点
vector<cv::KeyPoint> keypoints;
cv::Mat descriptor;
feature->detect(gray,keypoints);
// 绘制特征点
cv::Mat output;
cv::drawKeypoints(src,keypoints,output,cv::Scalar(0,0,255));
cv::imshow("src",src);
cv::imshow("output",output);
cv::waitKey();
return 0;
}
7、GFTTDetector
使用goodFeaturesToTrack函数包装特征的包装类。GFTTDetector类继承了cv::Feature2D类,通过create静态方法创建。
static Ptr cv::GFTTDetector::create(int maxCorners=1000,double qualityLevel = 0.01,double minDistance = 1,int blockSize = 3,bool useHarrisDetector = false,double k = 0.04)
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
int main()
{
// 读取图像
cv::Mat src = cv::imread("images/f1.jpg");
if(src.empty()){
cerr << "cannot read image.\n";
return EXIT_FAILURE;
}
// 转换成灰度图像
cv::Mat gray;
cv::cvtColor(src,gray,cv::COLOR_BGR2GRAY);
// 创建FastFeatureDetector特征提取器
cv::Ptr<cv::GFTTDetector> feature = cv::GFTTDetector::create();
// 检测特征点
vector<cv::KeyPoint> keypoints;
cv::Mat descriptor;
feature->detect(gray,keypoints);
// 绘制特征点
cv::Mat output;
cv::drawKeypoints(src,keypoints,output,cv::Scalar(0,0,255));
cv::imshow("src",src);
cv::imshow("output",output);
cv::waitKey();
return 0;
}
8、KAZE
实现KAZE关键点检测器和描述符提取器的类,在[Pablo Fernández Alcantarilla, Adrien Bartoli, and Andrew J Davison. Kaze features. In Computer Vision–ECCV 2012, pages 214–227. Springer, 2012.]中进行了描述。
AKAZE描述符只能与KAZE或AKAZE关键点一起使用。AKAZE类继承了cv::Feature2D类,通过create静态方法创建。参数如下:
static Ptr cv::KAZE::create(bool extended = false,bool upright = false,float threshold = 0.001f,int nOctaves = 4,int nOctaveLayers = 4,KAZE::DiffusivityType diffusivity = KAZE::DIFF_PM_G2)
参数 | 参数名称 |
---|---|
extended | 设置为启用扩展(128字节)描述符的提取。 |
upright | 设置为启用直立描述符(非旋转不变)。 |
threshold | 检测器响应阈值以接受点 |
nOctaves | 图像的最大八度演变 |
nOctaveLayers | 每个比例级别的默认子级别数 |
diffusivity | 扩散类型。 DIFF_PM_G1,DIFF_PM_G2,DIFF_WEICKERT或DIFF_CHARBONNIER |
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
int main()
{
// 读取图像
cv::Mat src = cv::imread("images/f1.jpg");
if(src.empty()){
cerr << "cannot read image.\n";
return EXIT_FAILURE;
}
// 转换成灰度图像
cv::Mat gray;
cv::cvtColor(src,gray,cv::COLOR_BGR2GRAY);
// 创建FastFeatureDetector特征提取器
cv::Ptr<cv::KAZE> feature = cv::KAZE::create();
// 检测特征点
vector<cv::KeyPoint> keypoints;
cv::Mat descriptor;
feature->detect(gray,keypoints);
// 绘制特征点
cv::Mat output;
cv::drawKeypoints(src,keypoints,output,cv::Scalar(0,0,255));
cv::imshow("src",src);
cv::imshow("output",output);
cv::waitKey();
return 0;
}
9、MSER
最大限度地稳定的极值区域提取。该类封装了MSER提取算法的所有参数。MSER类继承了cv::Feature2D类,通过create静态方法创建。参数如下:
static Ptr cv::MSER::create(int _delta = 5,int _min_area = 60,int _max_area = 14400,double _max_variation = 0.25,double _min_diversity = .2,int _max_evolution = 200,double _area_threshold = 1.01,double _min_margin = 0.003,int _edge_blur_size = 5)
参数名称 | 参数描述 |
---|---|
_delta | 它比较(sizei-sizei-delta)/ sizei-delta |
_min_area | 修剪小于minArea的区域 |
_max_area | 修剪大于maxArea的区域 |
_max_variation | 修剪该区域的大小与其子面积相似 |
_min_diversity | 对于彩色图像,回溯以切断多样性小于min_diversity的mser |
_max_evolution | 对于彩色图像,演变步骤 |
_area_threshold | 对于彩色图像,导致重新初始化的面积阈值 |
_min_margin | 对于彩色图像,请忽略过小的边距 |
_edge_blur_size | 对于彩色图像,边缘模糊的光圈大小 |
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
int main()
{
// 读取图像
cv::Mat src = cv::imread("images/f1.jpg");
if(src.empty()){
cerr << "cannot read image.\n";
return EXIT_FAILURE;
}
// 转换成灰度图像
cv::Mat gray;
cv::cvtColor(src,gray,cv::COLOR_BGR2GRAY);
// 创建FastFeatureDetector特征提取器
cv::Ptr<cv::MSER> feature = cv::MSER::create();
// 检测特征点
vector<cv::KeyPoint> keypoints;
cv::Mat descriptor;
feature->detect(gray,keypoints);
// 绘制特征点
cv::Mat output;
cv::drawKeypoints(src,keypoints,output,cv::Scalar(0,0,255));
cv::imshow("src",src);
cv::imshow("output",output);
cv::waitKey();
return 0;
}
10、ORB
实现ORB(面向Brief)关键点检测器和描述符提取器的类。
在[Ethan Rublee, Vincent Rabaud, Kurt Konolige, and Gary Bradski. Orb: an efficient alternative to sift or surf. In Computer Vision (ICCV), 2011 IEEE International Conference on, pages 2564–2571. IEEE, 2011.]中描述。 该算法在金字塔中使用FAST来检测稳定的关键点,使用FAST或Harris响应选择最强的特征,使用一阶矩找到它们的方向,并使用Brief来计算描述符(其中随机点对(或k元组)的坐标为 根据测量的方向旋转)。
ORB类继承了cv::Feature2D类,通过create静态方法创建。参数如下:
static Ptr cv::ORB::create(int nfeatures=500,float scaleFactor = 1.2f,int nlevels = 8,int edgeThreshold = 31,int firstLevel = 0,int WTA_K = 2,ORB::ScoreType scoreType = ORB::HARRIS_SCORE,int patchSize = 31,int fastThreshold = 20)
参数 | 参数描述 |
---|---|
nfeatures | 保留的最大特征数量。 |
scaleFactor | 金字塔抽取率大于1。scaleFactor == 2表示经典金字塔,其中每个下一个级别的像素比上一个少4倍,但是如此大的比例因子将大大降低特征匹配分数。 另一方面,太接近1的比例因子意味着要覆盖一定的比例范围,您将需要更多的金字塔等级,因此速度会受到影响。 |
nlevels | 金字塔等级的数量。 最小级别的线性大小等于input_image_linear_size / pow(scaleFactor,nlevels-firstLevel)。 |
edgeThreshold | 未检测到特征的边框的大小。 它应该与patchSize参数大致匹配。 |
firstLevel | 要放置源图像的金字塔等级。 先前的层填充有放大的源图像。 |
WTA_K | 产生定向的Brief描述符的每个元素的点数。 默认值2表示“ BRIEF”,我们采用一个随机点对并比较它们的亮度,因此得到0/1响应。 其他可能的值是3和4。例如,3表示我们取3个随机点(当然,这些点坐标是随机的,但它们是从预定义的种子生成的,因此,BRIEF描述符的每个元素都是从确定的 像素矩形),找到最大亮度的点和获胜者的输出指数(0、1或2)。 这样的输出将占用2位,因此将需要Hamming距离的特殊变体,表示为NORM_HAMMING2(每个bin 2位)。 当WTA_K = 4时,我们取4个随机点来计算每个bin(也将占用2位,可能的值为0、1、2或3)。 |
scoreType | The default HARRIS_SCORE means the Harris algorithm is used to rank the features (the score is written into KeyPoint::score and used to retain the best features); FAST_SCORE is a substitute value for this parameter, and it produces slightly less stable key points. But the calculation speed is slightly faster. |
patchSize | The patch size used by the directed Brief descriptor. Of course, on a smaller pyramid level, the area of the perceived image covered by the feature will be larger. |
fastThreshold | Fast threshold |
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
int main()
{
// 读取图像
cv::Mat src = cv::imread("images/f1.jpg");
if(src.empty()){
cerr << "cannot read image.\n";
return EXIT_FAILURE;
}
// 转换成灰度图像
cv::Mat gray;
cv::cvtColor(src,gray,cv::COLOR_BGR2GRAY);
// 创建FastFeatureDetector特征提取器
cv::Ptr<cv::ORB> feature = cv::ORB::create();
// 检测特征点
vector<cv::KeyPoint> keypoints;
cv::Mat descriptor;
feature->detect(gray,keypoints);
// 绘制特征点
cv::Mat output;
cv::drawKeypoints(src,keypoints,output,cv::Scalar(0,0,255));
cv::imshow("src",src);
cv::imshow("output",output);
cv::waitKey();
return 0;
}
11 、 SimpleBlobDetector
The class used to extract blobs from the image. The SimpleBlobDetector class inherits the cv::Feature2D class and is created through the create static method. The parameters are as follows:
static Ptr cv::SimpleBlobDetector::create(const SimpleBlobDetector::Params & parameters = SimpleBlobDetector::Params())
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
int main()
{
// 读取图像
cv::Mat src = cv::imread("images/f1.jpg");
if(src.empty()){
cerr << "cannot read image.\n";
return EXIT_FAILURE;
}
// 转换成灰度图像
cv::Mat gray;
cv::cvtColor(src,gray,cv::COLOR_BGR2GRAY);
// 创建FastFeatureDetector特征提取器
cv::Ptr<cv::SimpleBlobDetector> feature = cv::SimpleBlobDetector::create();
// 检测特征点
vector<cv::KeyPoint> keypoints;
cv::Mat descriptor;
feature->detect(gray,keypoints);
// 绘制特征点
cv::Mat output;
cv::drawKeypoints(src,keypoints,output,cv::Scalar(0,0,255));
cv::imshow("src",src);
cv::imshow("output",output);
cv::waitKey();
return 0;
}