使用OpenCV 2.4.10实现BRIEF

BRIEF implementation with OpenCV 2.4.10

本文关键字:实现 BRIEF OpenCV 使用      更新时间:2023-10-16

有人知道OpenCV 2.4实现BRIEF的链接吗?当做

附言:我知道这样的问题在SO上通常不受欢迎,因为主要关注的是你做了什么工作。但也有一个类似的问题受到了广泛的欢迎。

其中一个问题的答案提出了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;
}

此代码的问题在于它给出了一个错误: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 ........opencvmodulesfeatures2dsrcdraw.cpp, line 115。代码将从中恢复的下一个语句保持不变,因为drawKepoints在它之前被调用

在OpenCV中,BRIEF描述符提取器,而不是功能检测器。根据FeatureDetector::create,此工厂方法不支持"BRIEF"算法。换句话说,FeatureDetector::create("BRIEF")返回一个空指针,程序就会崩溃。

特征匹配的一般步骤是:

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

BRIEF是仅用于步骤2的算法。您可以使用其他一些方法,HARRISORB。。。,并使用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;
}

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

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

您也可以使用不同于"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;
}

同样,您可以尝试"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);

并使用CCD_ 13矢量完成您的应用程序。如果你想检查结果,OpenCV还提供了绘制步骤1&3。例如,在最后一步中绘制匹配项:

Mat result;
drawMatches(img1, keyPoints1, img2, keyPoints2, matches, result);
imshow("result", result);
waitKey(0);