OpenCV-FAST + BRIEF:如何使用DrawMatchesFlags :: DRAW_RICH_KEYPOINTS绘制关键点?

时间:2019-05-21 15:21:20

标签: c++ opencv feature-detection

我必须使用FAST + BRIEF(如果我理解正确的话,这是ORB的手动实现)来实现特征检测器。

所以,这是我到目前为止的代码:

printf("Calculating FAST+BRIEF features...\n");

Ptr<FastFeatureDetector> FASTdetector = FastFeatureDetector::create();
Ptr<BriefDescriptorExtractor> BRIEFdescriptor = BriefDescriptorExtractor::create();
std::vector<cv::KeyPoint> FASTkeypoints_1, FASTkeypoints_2, FASTkeypoints_3;
Mat BRIEFdescriptors_1, BRIEFdescriptors_2, BRIEFdescriptors_3;

FASTdetector->detect(left08, FASTkeypoints_1);
FASTdetector->detect(right08, FASTkeypoints_2);
FASTdetector->detect(left10, FASTkeypoints_3);

BRIEFdescriptor->compute(left08, FASTkeypoints_1, BRIEFdescriptors_1);
BRIEFdescriptor->compute(right08, FASTkeypoints_2, BRIEFdescriptors_2);
BRIEFdescriptor->compute(left10, FASTkeypoints_3, BRIEFdescriptors_3);

Mat FAST_left08, FAST_right08, FAST_left10;

drawKeypoints(left08, FASTkeypoints_1, FAST_left08, FASTBRIEFfeatcol_YELLOW, DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
imwrite("../Results/FASTBRIEF_left08.png", FAST_left08);
drawKeypoints(right08, FASTkeypoints_2, FAST_right08, FASTBRIEFfeatcol_YELLOW, DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
imwrite("../Results/FASTBRIEF_right08.png", FAST_right08);
drawKeypoints(left10, FASTkeypoints_3, FAST_left10, FASTBRIEFfeatcol_YELLOW, DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
imwrite("../Results/FASTBRIEF_left10.png", FAST_left10);
printf("FAST+BRIEF done. \n");

到目前为止,该代码可以正常工作,但是我没有得到丰富的关键点,但只有标准的关键点。如果我理解正确,那是因为我需要以某种方式首先将描述符信息获取到关键点,对吧?

在此之前,我已经使用SIFT,SURF和ORB完成了相同的实现,但是我在那里直接使用computeanddetect函数,这为我提供了关键点,可以在其中使用DrawMatchesFlags::DRAW_RICH_KEYPOINTS标志进行绘制。

1 个答案:

答案 0 :(得分:1)

  

我必须使用FAST + BRIEF(如果我理解正确的话,这是ORB的手动实现)来实现特征检测器。

是的,这是正确的。

  

如果我理解正确,那是因为我需要以某种方式首先将描述符信息获取到关键点,对吧?

否,可以通过不同方法检测关键点。您可以使用SIFT,FAST,HarrisDetector,SURF等。首先只能检测关键点。然后有不同的方法来描述检测到的关键点(例如,用于SIFT的128位浮点向量描述符)并随后对其进行匹配。
OpenCV中的关键点可以通过https://docs.opencv.org/3.4.2/d2/d29/classcv_1_1KeyPoint.html
的不同属性来描述,例如角度,大小,八度等。 对于SIFT,每个KeyPoint属性都填充有一个数字,以后可以在DRAW_RICH_KEYPOINTS标志中绘制该数字。对于FAST,仅分配属性的标准值,以便可以使用提到的标记绘制关键点,但大小,八度和角度不变。因此,每个绘制的KeyPoint看起来都很相似。

这里有一个小的代码示例作为证明(我仅使用->detect函数):

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/xfeatures2d/nonfree.hpp>  

int main(int argc, char** argv)
{

    // Load image
    cv::Mat img = cv::imread("MT189.jpg", CV_LOAD_IMAGE_GRAYSCALE);
    if (!img.data) {
        std::cout << "Error reading image" << std::endl;
        return EXIT_FAILURE;
    }
    cv::Mat output;

    // Detect FAST keypoints
    std::vector<cv::KeyPoint> keypoints_fast, keypoints_sift;
    cv::Ptr<cv::FastFeatureDetector> fast = cv::FastFeatureDetector::create();
    fast->detect(img, keypoints_fast);
    for (size_t i = 0; i < 100; ++i) {
        std::cout << "FAST Keypoint #:" << i;
        std::cout << " Size " << keypoints_fast[i].size << " Angle " << keypoints_fast[i].angle << " Response " << keypoints_fast[i].response << " Octave " << keypoints_fast[i].octave << std::endl;
    }

    // Detect SIFT keypoints
    cv::Ptr<cv::xfeatures2d::SiftFeatureDetector> sift = cv::xfeatures2d::SiftFeatureDetector::create();
    sift->detect(img, keypoints_sift);

    for (size_t i = 0; i < 100; ++i) {
        std::cout << "SIFT Keypoint #:" << i;
        std::cout << " Size " << keypoints_sift[i].size << " Angle " << keypoints_sift[i].angle << " Response " << keypoints_sift[i].response << " Octave " << keypoints_sift[i].octave << std::endl;
    }

    // Draw SIFT keypoints
    cv::drawKeypoints(img, keypoints_sift, output, cv::Scalar::all(-1), cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
    cv::imshow("Output", output);
    cv::waitKey(0);


}