编译 opencv 代码时遇到错误
Facing error with compiling opencv code
我正在使用opencv 2.4,下面是我正在尝试编译的代码。 我使用此命令编译我的代码
g++ -o "match" -ggdb `pkg-config --cflags opencv` match.cpp `pkg-config --libs opencv`
为什么我收到此错误:
match.cpp: In function ‘int main(int, const char**)’:
match.cpp:18:37: error: expected type-specifier before ‘SurfFeatureDetector’
match.cpp:18:37: error: conversion from ‘int*’ to non-scalar type ‘cv::Ptr<cv::FeatureDetector>’ requested
match.cpp:18:37: error: expected ‘,’ or ‘;’ before ‘SurfFeatureDetector’
match.cpp:22:2: error: ‘SurfDescriptorExtractor’ was not declared in this scope
match.cpp:22:26: error: expected ‘;’ before ‘extractor’
match.cpp:26:2: error: ‘extractor’ was not declared in this scope
match.cpp:29:2: error: ‘BruteForceMatcher’ was not declared in this scope
match.cpp:29:30: error: expected primary-expression before ‘>’ token
match.cpp:29:32: error: ‘matcher’ was not declared in this scope
我认为我使用的 opencv 版本存在一些问题,因为相同的代码在 2.2 版本上运行良好,但我不确定它是什么。帮助!!
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <string.h>
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, const char* argv[])
{
cout << argv[1] << endl << argv[2] << endl;
Mat img1 = imread(argv[1] , CV_LOAD_IMAGE_GRAYSCALE );
Mat img2 = imread(argv[2] , CV_LOAD_IMAGE_GRAYSCALE );
vector<KeyPoint> keypoints1;
vector<KeyPoint> keypoints2;
Ptr<FeatureDetector> feature = new SurfFeatureDetector(2500);
feature->detect(img1,keypoints1);
feature->detect(img2,keypoints2);
SurfDescriptorExtractor extractor;
Mat desc1 , desc2;
extractor.compute(img1,keypoints1,desc1);
extractor.compute(img2,keypoints2,desc2);
BruteForceMatcher<L2<float> > matcher;
vector<vector<DMatch> > matches1;
vector<vector<DMatch> > matches2;
vector<DMatch> symMatches;
vector<DMatch> outMatches;
matcher.knnMatch(desc1,desc2,matches1,2);
matcher.knnMatch(desc2,desc1,matches2,2);
int count_inliers = 0 , count_matches = 0;
for(vector<vector<DMatch> >::const_iterator matIt1 = matches1.begin(); matIt1 != matches1.end(); ++matIt1){
count_matches++;
if(matIt1->size() < 2)
continue;
for(vector<vector<DMatch> >::const_iterator matIt2 = matches2.begin(); matIt2 != matches2.end(); ++matIt2){
if(matIt2->size() < 2)
continue;
if((*matIt1)[0].queryIdx == (*matIt2)[0].trainIdx && (*matIt2)[0].queryIdx == (*matIt1)[0].trainIdx){
count_inliers++;
symMatches.push_back(DMatch((*matIt1)[0].queryIdx,(*matIt1)[0].trainIdx,(*matIt1)[0].distance));
break;
}
}
}
vector<Point2f> points1, points2;
for(vector<DMatch>::const_iterator it = symMatches.begin(); it!=symMatches.end(); ++it){
float x = keypoints1[it->queryIdx].pt.x;
float y = keypoints1[it->queryIdx].pt.y;
points1.push_back(Point2f(x,y));
x = keypoints2[it->trainIdx].pt.x;
y = keypoints2[it->trainIdx].pt.y;
points2.push_back(Point2f(x,y));
}
vector<uchar> inliers(points1.size(),0);
Mat fundamental;
fundamental = findFundamentalMat(Mat(points2),Mat(points1),inliers,CV_FM_RANSAC,2,0.8);
vector<uchar>::const_iterator itIn = inliers.begin();
vector<DMatch>::const_iterator itM = symMatches.begin();
for(;itIn!=inliers.end();++itIn,++itM){
if(*itIn){
outMatches.push_back(*itM);
}
}
cout << count_inliers << endl;
cout << count_matches << endl;
cout << (float) count_inliers/(float) count_matches << endl;
float diff = (float) count_inliers/(float) count_matches;
// if(diff > 0.30){
// cout << "Similar Images " << endl << "-----------------" << endl;
// exit(1);
// }
// vector<uchar> inliers(points1.size(),0);
Mat homography = findHomography(Mat(points2),Mat(points1),inliers,CV_RANSAC,1);
vector<Point2f>::const_iterator itPts = points1.begin();
// vector<uchar>::const_iterator itIn = inliers.begin();
/* while(itPts != points1.end()){
if(*itIn)
circle(img1,*itPts,3,Scalar(255,255,255),2);
++itPts;
++itIn;
}
itPts = points2.begin();
itIn = inliers.begin();
while(itPts != points2.end()){
if(*itIn)
circle(img2,*itPts,3,Scalar(255,255,255),2);
++itPts;
++itIn;
}
*/
Mat result;
warpPerspective(img2,result,homography,Size(2*img2.cols,img2.rows));
Mat half(result,Rect(0,0,img1.cols,img1.rows));
img1.copyTo(half);
// Add results to image and save.
char name[1000];
// strcpy(name,"./surf/surf");
// strcat(name,argv[1]);
cv::Mat output1;
cv::Mat output2;
cv::drawKeypoints(img1, keypoints1, output1);
cv::drawKeypoints(img2, keypoints2, output2);
cv::imwrite("./surf/img11.png", img1);
cv::imwrite("./surf/img21.png", img2);
cv::imwrite("./surf/img31.png", result);
cv::imwrite("./surf/tt.png", result);
cv::imwrite("./surf/img41.png", half);
cv::imwrite("./surf/img51.png", output1);
cv::imwrite("./surf/img61.png", output2);
return 0;
}
BruteForceMatcher
现在称为
cv::BFMatcher
请参阅文档。
您可以像这样定义匹配器:
DescriptorMatcher* hammingMatcher = new BFMatcher(NORM_HAMMING,false);
//or
DescriptorMatcher* hammingMatcher = new BFMatcher(NORM_L2,false);
编辑
此外,在此示例代码中,您可以通过包含标头来了解如何使用旧版本的匹配器
#include "hammingseg.h"
阅读有关 SURF 和 SIFT 探测器的讨论 - 它们已被移动为非自由。
还添加为动态库以链接libopencv_nonfree.so
和libopencv_features2d.so
。
对于BruteForceMatcher
来说,它看起来仍然是一个悬而未决的问题,但我很确定在其中一个.so
内,我希望他们也确实改变了标题。如果您发现有关BruteForceMatcher
的内容,我将不胜感激。
包括以下内容
#"opencv2/features2d/features2d.hpp"
#"opencv2/highgui/highgui.hpp"
#"opencv2/core/core.hpp"
#"opencv2/legacy/legacy.hpp" (BruteForceMatcher is defined here!)
并链接到以下内容
#opencv_core.so
#opencv_flann.soo (if youre using the FLANN matcher)
#opencv_highgui.so
#opencv_features2d.so
#opencv_nonfree.so
似乎为我做了这个把戏。希望这有帮助。
看起来你需要包含SurfFeatureDetector的头文件
这是 API,他们在其中提到了以下内容:
#include <features2d.hpp>
不要将文件包含整个路径,而应仅包含名称,并让编译器参数 (-I) 指定其路径。这更便携。
如果它没有在那里定义,那么寻找它。 在 Linux 中,您可以执行以下操作:
# find . -name "*.h*" | xargs grep SurfFeatureDetector | grep class
# find . -name "*.h*" | xargs grep BruteForceMatcher | grep class
这应该得到所有的 *.h 和 *.hpp 文件以及 SurfFeatureDetector 的 grep,对于这些结果,grep 用于类。
它非常简单:
//matching descriptors
cv::BFMatcher matcher(cv::NORM_L2, true);
std::vector<cv::DMatch> matches;
matcher.match(descriptor1, descriptor2, matches);
如果标志设置为 true,您已经进行了交叉检查。
#include <opencv2/legacy/legacy.hpp>
添加此行,它将像以前一样工作。
相关文章:
- 我在 C++ 代码中遇到错误警告:控制到达非空函数 [-Wreturn 类型] 的末尾
- 我在 ndk 中构建文件时遇到错误 android.mk
- 我们在批处理模式下使用 G++ 时遇到错误
- 在visual Studio中构建代码时,我遇到错误,.h文件丢失
- 学习C++并在早期示例中遇到错误(在非静态数据成员之前需要构造函数)
- 可中断线程类 C++11 - 遇到错误?
- 在尝试在同一类内部进行课程矢量时,我会遇到错误
- 使用向量和结构在C 中遇到错误
- 我正在尝试安装游戏引擎,但遇到错误
- 使用 bazel 从源代码构建张量流服务遇到错误:C++规则'@org_tensorflow//…'编译失败(出口 4)
- 为什么尝试这样的类代码时会遇到错误?我初学者对不起
- 当 Mingw 为 C++ 编译 OpenCV 时,我遇到错误?
- 遇到错误试图在功能型参数中使用void函数的错误
- 尝试使用Boost和Ncurses库进行编译程序时会遇到错误
- 我在使用C 的Hangman游戏中遇到错误
- 为什么我在加载共享库时遇到错误
- 我在 c++ 中遇到错误'PTHREAD_START_ROUTINE'未在此范围内声明
- 学习如何在遇到错误'variable not declared in scope'的 c++ 中使用指针
- 如果遇到错误,如何重复代码
- 当我尝试将UDP套接字绑定到带有SFML的端口时,为什么会遇到错误