opencv surf特征点检测(一),基本使用,cv::KeyPoint 属性分析,surf属性分析
本章内容:
1. surf特征点检测
2. cv::KeyPoint 属性分析
3.surf属性分析
1. surf特征点检测
输出结果
2. cv::KeyPoint 属性分析
输出结果:
3.surf属性分析
输出结果
源码
#include <ostream>
#include <opencv.hpp>
#include<opencv2/opencv.hpp>
#include "opencv2/xfeatures2d.hpp"int main(int argc, char *argv[])
{
/*
本章内容:
1. 初步认识 cv::xfeatures2d::SURF api接口
2. cv::KeyPoint的属性
3. cv::xfeatures2d::SURF 属性分析
*/
cv::String fileName = "/home/wang/dev/Image/heihei.jpeg";
cv::Mat src = cv::imread(fileName);if(src.data == NULL){
printf("图像读入失败\n");
return -1;
}
/*
* API接口: CV_WRAP static Ptr<SURF> create(double hessianThreshold=100,
int nOctaves = 4, int nOctaveLayers = 3,
bool extended = false, bool upright = false);*/
cv::imshow("src",src);
cv::Ptr<cv::xfeatures2d::SURF> surf= cv::xfeatures2d::SURF::create();
/* surf特征点检测
* api接口:
* CV_WRAP virtual void detect( InputArray image,
CV_OUT std::vector<KeyPoint>& keypoints,
InputArray mask=noArray() );
参数分析:
@param image Image.
@param keypoints The detected keypoints.
*/
std::vector<cv::KeyPoint> KeyPs;
surf->detect(src,KeyPs);
cv::Mat dst;
/* 绘图接口
* CV_EXPORTS_W void drawKeypoints( InputArray image, const std::vector<KeyPoint>& keypoints, InputOutputArray outImage,
const Scalar& color=Scalar::all(-1), DrawMatchesFlags flags=DrawMatchesFlags::DEFAULT );
*/
cv::drawKeypoints(src,KeyPs,dst);
cv::imshow("dst",dst);/* cv::KeyPoint的属性:
CV_PROP_RW Point2f pt; // 位置(x,y)
CV_PROP_RW float size; // 关键点的领域大小
CV_PROP_RW float angle; // 旋转角度
CV_PROP_RW float response; //
CV_PROP_RW int octave; // 金字塔的octave
CV_PROP_RW int class_id; // 关键点聚类时的id
*/
// 打印输出结果
std::cout << "KeyPs.size():= " << KeyPs.size() << std::endl;
std::cout << "KeyPs[0].pt:= " << KeyPs[0].pt << std::endl;
std::cout << "KeyPs[0].size:= " << KeyPs[0].size << std::endl;
std::cout << "KeyPs[0].angle:= " << KeyPs[0].angle << std::endl;
std::cout << "KeyPs[0].octave:= " << KeyPs[0].octave << std::endl;
std::cout << "KeyPs[0].class_id:= " << KeyPs[0].class_id << std::endl;/*3.surf属性分析
属性介绍:
hessianThreshold 特征点检测阈值
nOctaves 图像金字塔层的Octave数目
nOctaveLayers 每组Octave中金字塔的层数
Extended 0表示64维特征,1表示128维特征
upright 是否计算旋转角度
属性设置与获取API接口:
CV_WRAP virtual void setHessianThreshold(double hessianThreshold) = 0;
CV_WRAP virtual double getHessianThreshold() const = 0;CV_WRAP virtual void setNOctaves(int nOctaves) = 0;
CV_WRAP virtual int getNOctaves() const = 0;CV_WRAP virtual void setNOctaveLayers(int nOctaveLayers) = 0;
CV_WRAP virtual int getNOctaveLayers() const = 0;CV_WRAP virtual void setExtended(bool extended) = 0;
CV_WRAP virtual bool getExtended() const = 0;CV_WRAP virtual void setUpright(bool upright) = 0;
CV_WRAP virtual bool getUpright() const = 0;
*/
std::cout << "默认 hessianThreshold= " << surf->getHessianThreshold() << std::endl;
surf->setHessianThreshold(50);
std::cout << "设置后 hessianThreshold= " << surf->getHessianThreshold() << std::endl;
std::cout << "默认 nOctaves= " << surf->getNOctaves() << std::endl;
surf->setNOctaves(5);
std::cout << "设置后 nOctaves= " << surf->getNOctaves() << std::endl;
std::cout << "默认 nOctaveLayers= " << surf->getNOctaveLayers() << std::endl;
surf->setNOctaveLayers(5);
std::cout << "设置后 nOctaveLayers= " << surf->getNOctaveLayers() << std::endl;
std::cout << "默认 extended= " << surf->getExtended() << std::endl;
surf->setExtended(1);
std::cout << "设置后 extended= " << surf->getExtended() << std::endl;
std::cout << "默认 upright= " << surf->getUpright() << std::endl;
surf->setUpright(1);
std::cout << "设置后 upright= " << surf->getUpright() << std::endl;
cv::waitKey(0);
return 1;
}