使用OpenCV 2.4.10简要实现

the*_*ker 1 c++ opencv

有人知道使用OpenCV 2.4的Brief实现的链接吗?问候.

PS:我知道这些问题一般不受欢迎,因为主要关注的是你所做的工作.但是有一个类似的问题很受欢迎.

这些问题的答案之一表明了SIFT的通用方式,可以扩展到Brief.这是我稍微修改过的代码.

#include <opencv2/nonfree/nonfree.hpp> 
#include <opencv2/highgui/highgui.hpp>

//using namespace std;
using namespace cv;

int main(int argc, char *argv[])
{        
  Mat image = imread("load02.jpg", CV_LOAD_IMAGE_GRAYSCALE);
  cv::initModule_nonfree();
  // Create smart pointer for SIFT feature detector.
  Ptr<FeatureDetector> featureDetector = FeatureDetector::create("HARRIS"); // "BRIEF was initially written. Changed after answer."
  vector<KeyPoint> keypoints;

  // Detect the keypoints
  featureDetector->detect(image, keypoints); // NOTE: featureDetector is a pointer hence the '->'.

  //Similarly, we create a smart pointer to the SIFT extractor.
  Ptr<DescriptorExtractor> featureExtractor = DescriptorExtractor::create("BRIEF");

  // Compute the 128 dimension SIFT descriptor at each keypoint.
  // Each row in "descriptors" correspond to the SIFT descriptor for each keypoint
  Mat descriptors;
  featureExtractor->compute(image, keypoints, descriptors);

  // If you would like to draw the detected keypoint just to check
  Mat outputImage;
  Scalar keypointColor = Scalar(255, 0, 0);     // Blue keypoints.
  drawKeypoints(image, keypoints, outputImage, keypointColor, DrawMatchesFlags::DEFAULT);

  namedWindow("Output");
  imshow("Output", outputImage);

  char c = ' ';
  while ((c = waitKey(0)) != 'q');  // Keep window there until user presses 'q' to quit.

  return 0;

}
Run Code Online (Sandbox Code Playgroud)

此代码的问题是它出错: First-chance exception at 0x00007FFB84698B9C in Project2.exe: Microsoft C++ exception: cv::Exception at memory location 0x00000071F4FBF8E0.

该错误导致函数执行中断.标签表示执行将在该namedWindow("Output");行恢复.

有人可以帮忙解决这个问题,或者完全建议一个新的代码吗?谢谢.

编辑:终端现在显示错误:Assertion failed (!outImage.empty()) in cv::drawKeypoints, file ..\..\..\..opencv\modules\features2d\src\draw.cpp, line 115.代码将从中恢复的下一个语句保持不变,drawKepoints就像之前调用的那样.

mcc*_*chu 6

在OpenCV中,BRIEFDescriptorExtractor,而不是FeatureDetector.根据FeatureDetector :: create,此工厂方法不支持"BRIEF"算法.换句话说,FeatureDetector::create("BRIEF")返回一个空指针,程序崩溃.

功能匹配的一般步骤是:

  1. 在图像中找到一些有趣的(特征)点: FeatureDetector
  2. 找到一种方法来描述这些点: DescriptorExtractor
  3. 尝试在两个图像中匹配描述符(特征向量): DescriptorMatcher

BRIEF是仅适用于步骤2的算法.您可以在步骤1中使用其他一些方法,HARRIS,ORB,...,并使用BRIEF将结果传递给步骤2 .此外,SIFT可以在步骤1和2中使用,因为该算法提供了两个步骤的方法.


这是在OpenCV中使用BRIEF的简单示例.第一步,找到图像中看起来很有趣的点(关键点):

vector<KeyPoint> DetectKeyPoints(const Mat &image) 
{
    auto featureDetector = FeatureDetector::create("HARRIS");
    vector<KeyPoint> keyPoints;
    featureDetector->detect(image, keyPoints);
    return keyPoints;
}
Run Code Online (Sandbox Code Playgroud)

您可以尝试任何FeatureDetector算法而不是"HARRIS".下一步,从关键点计算描述符:

Mat ComputeDescriptors(const Mat &image, vector<KeyPoint> &keyPoints)
{
    auto featureExtractor = DescriptorExtractor::create("BRIEF");
    Mat descriptors;
    featureExtractor->compute(image, keyPoints, descriptors);
    return descriptors;
}
Run Code Online (Sandbox Code Playgroud)

您也可以使用不同的算法"BRIEF".您可以看到DescriptorExtractor中的算法与FeatureDetector中的算法不同.最后一步,匹配两个描述符:

vector<DMatch> MatchTwoImage(const Mat &descriptor1, const Mat &descriptor2)
{
    auto matcher = DescriptorMatcher::create("BruteForce");
    vector<DMatch> matches;
    matcher->match(descriptor1, descriptor2, matches);
    return matches;
}
Run Code Online (Sandbox Code Playgroud)

同样,你可以尝试不同的匹配算法比其他"BruteForce".最后回到主程序,您可以从这些函数构建应用程序:

auto img1 = cv::imread("image1.jpg");
auto img2 = cv::imread("image2.jpg");

auto keyPoints1 = DetectKeyPoints(img1);
auto keyPoints2 = DetectKeyPoints(img2);

auto descriptor1 = ComputeDescriptors(img1, keyPoints1);
auto descriptor2 = ComputeDescriptors(img2, keyPoints2);

auto matches = MatchTwoImage(descriptor1, descriptor2);
Run Code Online (Sandbox Code Playgroud)

并使用matches向量来完成您的应用程序.如果要检查结果,OpenCV还提供了在图像中绘制步骤1和3的结果的功能.例如,在最后一步中绘制匹配项:

Mat result;
drawMatches(img1, keyPoints1, img2, keyPoints2, matches, result);
imshow("result", result);
waitKey(0);
Run Code Online (Sandbox Code Playgroud)

  • 最终的结果matches是一个cv::DMatch的向量,它有一个成员变量distance。您可以将这些“距离”应用于任何数学公式。例如,使用平均距离来获得匹配精度。 (2认同)