OpenCV - 未定义的引用:SurfFeatureDetector和BruteForceMatcher

时间:2012-04-01 22:42:20

标签: c++ opencv linker undefined-reference surf

我正在用C ++编写一个程序,它使用2个图像来检测SURF特征,用bruteforcematcher计算匹配并绘制它。

这是代码

#include <cstdio>
#include <string>
#include <vector>
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include "opencv2/features2d/features2d.hpp"


using namespace cv;
using namespace std;

int main(int argc, char **argv){
        if (argc <3) {
            cout << "Usage: " << argv[0] << " imageLocation1 imageLocation2" << endl;

            return -1;
        }

        Mat source1 = imread(argv[1],CV_LOAD_IMAGE_GRAYSCALE);
        Mat source2 = imread(argv[2],CV_LOAD_IMAGE_GRAYSCALE);
        if(source1.empty() || source2.empty()){
        printf("Can't load all the images!");
        return -1;
        }   

//Initialise the Wrapping Class for Surf()
    SurfFeatureDetector detector(400);

//detect : first param: Image, second param: vector (output)

    vector<KeyPoint> keypoints1,keypoints2;

    detector.detect(source1,keypoints1);
    detector.detect(source2,keypoints2);

//Initialise wrapping class for descriptors computing using SURF() class.
    SurfDescriptorExtractor extractor;

//Compute: Input:image, keypoints Output:descriptors
    Mat descriptors1,descriptors2;

    extractor.compute(source1,keypoints1,descriptors1);
    extractor.compute(source2,keypoints2,descriptors2);

//Initialise BruteForceMatcher: For each descriptor in the first set, this matcher finds the closest descriptor in the second set by trying each on (=brute)
    BruteForceMatcher< L2<float> > matcher;
    vector< DMatch > matches;

//match: execute the matcher!
    matcher.match(descriptors1,descriptors2, matches);

//Draw the matches with drawMatches
    Mat target;
    drawMatches(source1,keypoints1,source2,keypoints2,matches,target); 

    imshow("Matches", target);

    waitKey(0);

    return 0;
}

构建不是问题,但在链接时,我得到了这个非常讨厌的错误:

CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o: In function `cv::BruteForceMatcher<cv::L2<float> >::~BruteForceMatcher()':
lennart_martens_opgave13.cpp:(.text._ZN2cv17BruteForceMatcherINS_2L2IfEEED2Ev[_ZN2cv17BruteForceMatcherINS_2L2IfEEED5Ev]+0xb): undefined reference to `cv::DescriptorMatcher::~DescriptorMatcher()'
CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o: In function `cv::BruteForceMatcher<cv::L2<float> >::~BruteForceMatcher()':
lennart_martens_opgave13.cpp:(.text._ZN2cv17BruteForceMatcherINS_2L2IfEEED0Ev[_ZN2cv17BruteForceMatcherINS_2L2IfEEED5Ev]+0x12): undefined reference to `cv::DescriptorMatcher::~DescriptorMatcher()'
CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o: In function `main':
lennart_martens_opgave13.cpp:(.text.startup+0x172): undefined reference to `cv::SurfFeatureDetector::SurfFeatureDetector(double, int, int, bool)'
lennart_martens_opgave13.cpp:(.text.startup+0x24f): undefined reference to `cv::FeatureDetector::detect(cv::Mat const&, std::vector<cv::KeyPoint, std::allocator<cv::KeyPoint> >&, cv::Mat const&) const'
lennart_martens_opgave13.cpp:(.text.startup+0x30a): undefined reference to `cv::FeatureDetector::detect(cv::Mat const&, std::vector<cv::KeyPoint, std::allocator<cv::KeyPoint> >&, cv::Mat const&) const'
lennart_martens_opgave13.cpp:(.text.startup+0x346): undefined reference to `cv::SurfDescriptorExtractor::SurfDescriptorExtractor(int, int, bool, bool)'
lennart_martens_opgave13.cpp:(.text.startup+0x495): undefined reference to `cv::DescriptorExtractor::compute(cv::Mat const&, std::vector<cv::KeyPoint, std::allocator<cv::KeyPoint> >&, cv::Mat&) const'
lennart_martens_opgave13.cpp:(.text.startup+0x4bb): undefined reference to `cv::DescriptorExtractor::compute(cv::Mat const&, std::vector<cv::KeyPoint, std::allocator<cv::KeyPoint> >&, cv::Mat&) const'
lennart_martens_opgave13.cpp:(.text.startup+0x5ac): undefined reference to `cv::DescriptorMatcher::match(cv::Mat const&, cv::Mat const&, std::vector<cv::DMatch, std::allocator<cv::DMatch> >&, cv::Mat const&) const'
lennart_martens_opgave13.cpp:(.text.startup+0x6de): undefined reference to `cv::drawMatches(cv::Mat const&, std::vector<cv::KeyPoint, std::allocator<cv::KeyPoint> > const&, cv::Mat const&, std::vector<cv::KeyPoint, std::allocator<cv::KeyPoint> > const&, std::vector<cv::DMatch, std::allocator<cv::DMatch> > const&, cv::Mat&, cv::Scalar_<double> const&, cv::Scalar_<double> const&, std::vector<char, std::allocator<char> > const&, int)'
lennart_martens_opgave13.cpp:(.text.startup+0x781): undefined reference to `cv::DescriptorMatcher::~DescriptorMatcher()'
lennart_martens_opgave13.cpp:(.text.startup+0x7ad): undefined reference to `vtable for cv::SurfDescriptorExtractor'
lennart_martens_opgave13.cpp:(.text.startup+0x7b5): undefined reference to `cv::DescriptorExtractor::~DescriptorExtractor()'
lennart_martens_opgave13.cpp:(.text.startup+0x7d8): undefined reference to `vtable for cv::SurfFeatureDetector'
lennart_martens_opgave13.cpp:(.text.startup+0x7e0): undefined reference to `cv::FeatureDetector::~FeatureDetector()'
lennart_martens_opgave13.cpp:(.text.startup+0x8c8): undefined reference to `vtable for cv::SurfFeatureDetector'
lennart_martens_opgave13.cpp:(.text.startup+0x8d0): undefined reference to `cv::FeatureDetector::~FeatureDetector()'
lennart_martens_opgave13.cpp:(.text.startup+0x942): undefined reference to `vtable for cv::SurfDescriptorExtractor'
lennart_martens_opgave13.cpp:(.text.startup+0x94a): undefined reference to `cv::DescriptorExtractor::~DescriptorExtractor()'
lennart_martens_opgave13.cpp:(.text.startup+0x9a2): undefined reference to `cv::DescriptorMatcher::~DescriptorMatcher()'
CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o:(.rodata._ZTVN2cv17BruteForceMatcherINS_2L2IfEEEE[vtable for cv::BruteForceMatcher<cv::L2<float> >]+0x10): undefined reference to `cv::DescriptorMatcher::add(std::vector<cv::Mat, std::allocator<cv::Mat> > const&)'
CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o:(.rodata._ZTVN2cv17BruteForceMatcherINS_2L2IfEEEE[vtable for cv::BruteForceMatcher<cv::L2<float> >]+0x14): undefined reference to `cv::DescriptorMatcher::clear()'
CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o:(.rodata._ZTVN2cv17BruteForceMatcherINS_2L2IfEEEE[vtable for cv::BruteForceMatcher<cv::L2<float> >]+0x18): undefined reference to `cv::DescriptorMatcher::empty() const'
CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o:(.rodata._ZTVN2cv17BruteForceMatcherINS_2L2IfEEEE[vtable for cv::BruteForceMatcher<cv::L2<float> >]+0x20): undefined reference to `cv::DescriptorMatcher::train()'
CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o:(.rodata._ZTVN2cv17BruteForceMatcherINS_2L2IfEEEE[vtable for cv::BruteForceMatcher<cv::L2<float> >]+0x24): undefined reference to `cv::DescriptorMatcher::read(cv::FileNode const&)'
CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o:(.rodata._ZTVN2cv17BruteForceMatcherINS_2L2IfEEEE[vtable for cv::BruteForceMatcher<cv::L2<float> >]+0x28): undefined reference to `cv::DescriptorMatcher::write(cv::FileStorage&) const'
CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o:(.rodata._ZTVN2cv17BruteForceMatcherINS_2L2IfEEEE[vtable for cv::BruteForceMatcher<cv::L2<float> >]+0x30): undefined reference to `cv::BruteForceMatcher<cv::L2<float> >::knnMatchImpl(cv::Mat const&, std::vector<std::vector<cv::DMatch, std::allocator<cv::DMatch> >, std::allocator<std::vector<cv::DMatch, std::allocator<cv::DMatch> > > >&, int, std::vector<cv::Mat, std::allocator<cv::Mat> > const&, bool)'
CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o:(.rodata._ZTVN2cv17BruteForceMatcherINS_2L2IfEEEE[vtable for cv::BruteForceMatcher<cv::L2<float> >]+0x34): undefined reference to `cv::BruteForceMatcher<cv::L2<float> >::radiusMatchImpl(cv::Mat const&, std::vector<std::vector<cv::DMatch, std::allocator<cv::DMatch> >, std::allocator<std::vector<cv::DMatch, std::allocator<cv::DMatch> > > >&, float, std::vector<cv::Mat, std::allocator<cv::Mat> > const&, bool)'
CMakeFiles/opg13.dir/src/lennart_martens_opgave13.o:(.rodata._ZTIN2cv17BruteForceMatcherINS_2L2IfEEEE[typeinfo for cv::BruteForceMatcher<cv::L2<float> >]+0x8): undefined reference to `typeinfo for cv::DescriptorMatcher'
collect2: ld gaf exit-status 1 terug
make[2]: *** [bin/opg13] Fout 1
make[1]: *** [CMakeFiles/opg13.dir/all] Fout 2
make: *** [all] Fout 2

我真的不知道问题是什么。我没有在互联网上找到任何东西。希望有人可以帮忙!

编辑:这是我的CMakeLists.txt:

cmake_minimum_required(VERSION 2.4)


PROJECT(LABO5)

# paths 
INCLUDE_DIRECTORIES(src)
INCLUDE_DIRECTORIES(/usr/local/include)
LINK_DIRECTORIES(/usr/local/lib)
LINK_DIRECTORIES(/usr/lib)
SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)
SET(CMAKE_CXX_FLAGS "-o3 -w")
SET(CMAKE_CXX_LINK_FLAGS "-pg")
SET(OpenCV_LIBRARIES opencv_core opencv_highgui opencv_imgproc )


ADD_EXECUTABLE(opg13 src/lennart_martens_opgave13.cpp)
TARGET_LINK_LIBRARIES(opg13 ${OpenCV_LIBRARIES})
SET(CMAKE_BUILD_TYPE Release)

7 个答案:

答案 0 :(得分:41)

如果您使用的是opencv 2.4,则SURF和SIFT接口将更改为nonfree文件夹。您可以通过包含此行

来使用它
#include <opencv2/nonfree/features2d.hpp>

然后您可以像以前一样使用SurfFeatureDetector。

答案 1 :(得分:8)

对于SURF,@Mingyi Wu已经回答了。对于 BruteForceMatcher ,请

#include <opencv2/legacy/legacy.hpp>

答案 2 :(得分:1)

如果您从svn使用opencv2.4或trunk,则更改SURF和SIFT接口。 http://code.opencv.org/projects/opencv/wiki/ChangeLog

答案 3 :(得分:0)

  

我真的不知道问题是什么。

问题很可能是错误的链接线。不幸的是,您没有说明您的链接行,因此无法提供进一步的帮助。阅读this可能会有所帮助。

答案 4 :(得分:0)

安装ROS后我遇到了类似的问题。问题是我链接到了错误的库。

我通过将以下行添加到我的CMakeLists.txt来解决链接错误:link_directories(/opt/ros/groovy/lib)

答案 5 :(得分:0)

对于ocv 2.4.9:#include <opencv2/nonfree/features2d.hpp>是SURF的位置。在项目设置中选择配置属性,然后选择链接器,然后输入,然后添加 opencv_nonfree249d.lib < / strong>到其他依赖关系。有了这些,ocv文档中的以下示例工作正常:-) http://docs.opencv.org/doc/tutorials/features2d/feature_detection/feature_detection.html#feature-detection

答案 6 :(得分:-1)

将OpenCV从2.3.1升级到2.4.5之后我遇到了这个问题,我已经解决了连接opencv_nonfree的问题,并为我的项目添加了必要的标题:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/imgproc/imgproc.hpp"