使用OpenCV 2.4.10实现BRIEF
BRIEF implementation with OpenCV 2.4.10
有人知道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")
返回一个空指针,程序就会崩溃。
特征匹配的一般步骤是:
- 在图像中查找一些有趣的(特征)点:
FeatureDetector
- 找到描述这些点的方法:
DescriptorExtractor
- 尝试匹配两个图像中的描述符(特征向量):
DescriptorMatcher
BRIEF是仅用于步骤2的算法。您可以使用其他一些方法,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;
}
您可以尝试任何FeatureDetector算法,而不是"HARRIS"
。下一步,从关键点计算描述符:
Mat ComputeDescriptors(const Mat &image, vector<KeyPoint> &keyPoints)
{
auto featureExtractor = DescriptorExtractor::create("BRIEF");
Mat descriptors;
featureExtractor->compute(image, keyPoints, descriptors);
return descriptors;
}
您也可以使用不同于 同样,您可以尝试 并使用CCD_ 13矢量完成您的应用程序。如果你想检查结果,OpenCV还提供了绘制步骤1&3。例如,在最后一步中绘制匹配项:"BRIEF"
的算法。您可以看到DescriptorExtractor中的算法与FeatureDetectorvector<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);
Mat result;
drawMatches(img1, keyPoints1, img2, keyPoints2, matches, result);
imshow("result", result);
waitKey(0);
- 如果没有malloc,链表实现将失败
- 如何在c++中实现处理器调度模拟器
- 如何在c++中使用引用实现类似python的行为
- 实现无开销push_back的最佳方法是什么
- 使用简单类型列表实现的指数编译时间.为什么
- 如何在BST的这个简单递归实现中消除警告
- 实现一个在集合上迭代的模板函数
- 我应该实现右值推送功能吗?我应该使用std::move吗
- 如何正确实现和访问运算符的各种自定义枚举器
- C++Union/Struct位域的实现和可移植性
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 在c++中实现LinkedList时,应出现未处理的错误
- 为左值和右值的包装器实现C++范围
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- 使用GSoap实现ONVIF
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 用于AVX的ln(x)的实现,m256
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 在C++中,如何在类和函数(可能是模板化的)的头中编写完整的实现
- 使用OpenCV 2.4.10实现BRIEF