2012-05-30 107 views
2

我使用的是opencv 2.4,下面是我正在編譯的代碼。我使用這個命令面向編譯opencv代碼的錯誤

g++ -o "match" -ggdb `pkg-config --cflags opencv` match.cpp `pkg-config --libs opencv` 

編譯我的代碼,爲什麼我收到此錯誤:因爲相同的代碼運行在2.2精細

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的版本版本,但我不知道它是什麼。幫幫我 !!

#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; 
} 

回答

10

BruteForceMatcher現在被稱爲

cv::BFMatcher 

documentation

可以定義這樣的匹配:通過包括頭

在此 sample代碼

DescriptorMatcher* hammingMatcher = new BFMatcher(NORM_HAMMING,false); 
//or 
DescriptorMatcher* hammingMatcher = new BFMatcher(NORM_L2,false); 

編輯

還可以看到如何使用舊版本匹配

#include "hammingseg.h" 
0

看起來你需要包括SurfFeatureDetector

Here的API的頭文件,其中他們提到以下幾點:

#include <features2d.hpp> 

相反的,包括與整個路徑的文件,僅包含名稱並讓編譯器參數(-I)指定其路徑。這更便攜。

如果它沒有在那裏定義,然後尋找它。在Linux中,你可以做到以下幾點:

# find . -name "*.h*" | xargs grep SurfFeatureDetector | grep class 
# find . -name "*.h*" | xargs grep BruteForceMatcher | grep class 

這應該讓所有的* .h和* .HPP文件和grep爲SurfFeatureDetector和這些結果,grep的類。

+0

我包含頭文件 #include 但我仍然收到相同的錯誤。 – diggy

+0

好吧,仔細研究一下,看看它的定義,因爲它沒有找到類的定義。 – Brady

+0

我在/ usr/include /文件夾中運行了find命令,但沒有找到任何東西。順便說一句,我不僅錯過了SurfFeatureDetector,我也錯過了BruteForceMatcher,我也無法找到它。 – diggy

5

閱讀this discussion爲SURF和SIFT檢測器 - 它們已被移動爲非自由。

也作爲動態鏈接庫鏈接libopencv_nonfree.solibopencv_features2d.so

對於BruteForceMatcher它看起來仍然是一個開放的問題,但我很確定是在.so之一,我希望他們確實也改變頭。如果您發現有關BruteForceMatcher的東西,我將不勝感激。

+0

正如你所說你想要BruteForceMatcher的信息看看[this](http://stackoverflow.com/a/12508289/744859) –

3

包括以下

#"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 

似乎做的伎倆我。希望這可以幫助。

+0

謝謝你! – Komi

0

它真的很簡單le:

//matching descriptors 
cv::BFMatcher matcher(cv::NORM_L2, true); 
std::vector<cv::DMatch> matches; 
matcher.match(descriptor1, descriptor2, matches); 

隨着標誌設置爲true,您已經得到了交叉檢查。

0
#include <opencv2/legacy/legacy.hpp> 

添加此行,它將像以前一樣工作。