1、特征点检测器与描述符
这些方法中基本都包括检测器和相应的描述符两个部分,以下为HOG类描述符SIFT和binary类的描述符BRISK的代码实现与对比。不过由于版权问题,HOG类在OpenCV中不能免费使用。
#include <iostream>
#include <numeric>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/xfeatures2d/nonfree.hpp>
using namespace std;
void descKeypoints1()
{
// load image from file and convert to grayscale
cv::Mat imgGray;
cv::Mat img = cv::imread("E:/传感器融合/SensorFusion/所有代码/所有代码/3_Camera/04_Tracking Image Features/gradient_filtering/images/img1.png");
cv::cvtColor(img, imgGray, cv::COLOR_BGR2GRAY);
// BRISK detector / descriptor
cv::Ptr<cv::FeatureDetector> detector = cv::BRISK::create();
vector<cv::KeyPoint> kptsBRISK;
double t = (double)cv::getTickCount();
detector->detect(imgGray, kptsBRISK);
t = ((double)cv::getTickCount() - t) / cv::getTickFrequency();
cout << "BRISK detector with n= " << kptsBRISK.size() << " keypoints in " << 1000 * t / 1.0 << " ms" << endl;
cv::Ptr<cv::DescriptorExtractor> descriptor = cv::BRISK::create();
cv::Mat descBRISK;
t = (double)cv::getTickCount();
descriptor->compute(imgGray, kptsBRISK, descBRISK);
t = ((double)cv::getTickCount() - t) / cv::getTickFrequency();
cout << "BRISK descriptor in " << 1000 * t / 1.0 << " ms" << endl;
// visualize results
cv::Mat visImage = img.clone();
cv::drawKeypoints(img, kptsBRISK, visImage, cv::Scalar::all(-1), cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
string windowName = "BRISK Results";
cv::namedWindow(windowName, 1);
imshow(windowName, visImage);
// STUDENT CODE
detector = cv::xfeatures2d::SIFT::create();
vector<cv::KeyPoint> kptsSIFT;
t = (double)cv::getTickCount();
detector->detect(imgGray, kptsSIFT);
t = ((double)cv::getTickCount() - t) / cv::getTickFrequency();
cout << "SIFT detector with n= " << kptsSIFT.size() << " keypoints in " << 1000 * t / 1.0 << " ms" << endl;
descriptor = cv::xfeatures2d::SiftDescriptorExtractor::create();
cv::Mat descSIFT;
t = (double)cv::getTickCount();
descriptor->compute(imgGray, kptsSIFT, descSIFT);
t = ((double)cv::getTickCount() - t) / cv::getTickFrequency();
cout << "SIFT descriptor in " << 1000 * t / 1.0 << " ms" << endl;
visImage = img.clone();
cv::drawKeypoints(img, kptsSIFT, visImage, cv::Scalar::all(-1), cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
windowName = "SIFT Results";
cv::namedWindow(windowName, 2);
imshow(windowName, visImage);
cv::waitKey(0);
// EOF STUDENT CODE
}
int main()
{
descKeypoints1();
}