有人知道使用OpenCV 2.4的Brief实现的链接吗?问候。
PS:我知道这些问题一般不受欢迎,因为主要关注的是你所做的工作。但是有一个类似的question非常受欢迎。该问题的答案之一表明了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 ..\..\..\..opencv\modules\features2d\src\draw.cpp, line 115
。代码将恢复的下一个语句保持不变,因为drawKepoints
就在它之前调用。
答案 0 :(得分:6)
在OpenCV中, Brief 是 DescriptorExtractor ,而不是 FeatureDetector 。根据{{3}},此工厂方法不支持"BRIEF"
算法。换句话说,FeatureDetector::create("BRIEF")
返回一个空指针,程序崩溃。
功能匹配的一般步骤是:
FeatureDetector
DescriptorExtractor
DescriptorMatcher
简要是仅适用于第2步的算法。您可以在步骤中使用其他一些方法, HARRIS , ORB ,... 1并使用 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::create而不是"HARRIS"
。下一步,从关键点计算描述符:
Mat ComputeDescriptors(const Mat &image, vector<KeyPoint> &keyPoints)
{
auto featureExtractor = DescriptorExtractor::create("BRIEF");
Mat descriptors;
featureExtractor->compute(image, keyPoints, descriptors);
return descriptors;
}
您也可以使用与"BRIEF"
不同的FeatureDetector algorithm。您可以看到 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"
以外的其他algorithm。最后回到主程序,您可以从这些函数构建应用程序:
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);
并使用matches
向量来完成您的申请。如果您想检查结果,OpenCV还提供matching algorithm来绘制步骤1和结果的结果。 3在图像中。例如,在最后一步中绘制匹配项:
Mat result;
drawMatches(img1, keyPoints1, img2, keyPoints2, matches, result);
imshow("result", result);
waitKey(0);