c++ - 编译时 SIFT 代码 (C++) 中的 undefined reference

标签 c++ opencv image-processing sift

<分区>

我正在尝试检测关键点并提取一组图像的描述符并将它们存储在文本文件中以进行匹配(这将由另一段代码处理)。这是我目前所有的代码。

以下是我尝试在 Ubuntu 12.04 终端上使用 g++ sift1.cpp 编译的代码。我安装了 OpenCV 2.4.9。

#include <iostream>
#include <stdio.h>
#include <dirent.h>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/features2d/features2d.hpp>

using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
    if(argc != 2)
    {
        cout << "Usage: ./output <database path>" << endl;
        return -1;
    }
    Mat img;
    vector<KeyPoint> keypoints;
    //  SiftFeatureDetector detector;
    //  vector<KeyPoint> keypoints;
    //      detector.detect(img, keypoints);
    Mat descriptor;
    SIFT sift;
    Mat frame_gray;
    //  vector<Rect> faces;
    ofstream outfile_kp, outfile_des;
    outfile_kp.open("keypoints_file.txt");
    outfile_des.open("descriptors_file.txt");
    DIR *pdir = NULL;
    pdir = opendir(argv[1]);
    struct dirent *pent = NULL;
    while(pent == readdir(pdir))
    {
        if((strcmp(pent -> d_name, ".") == 0) || (strcmp(pent -> d_name, "..") == 0))
        {
            continue;
        }
        img = imread(pent -> d_name, CV_LOAD_IMAGE_GRAYSCALE);
        cvtColor( img, frame_gray, CV_BGR2GRAY );
        equalizeHist( frame_gray, frame_gray );
        sift.operator()(frame_gray,frame_gray,keypoints,descriptor,false);
        //      std::ostream_iterator<KeyPoint> output_iterator_kp(outfile_kp, "\n");
        //      std::copy(keypoints.begin(), keypoints.end(), output_iterator_kp);
        //      outfile_des << descriptor << endl;
        //      std::ostream_iterator<KeyPoint> output_iterator_des(output_des, "\n");
        //      std::copy(keypoints.begin(), keypoints.end(), output_iterator_des);
        //      outfile_kp << keypoints << endl;
        //      outfile_des << descriptor << endl;
    }
    outfile_kp.close();
    outfile_des.close();
    /*
       Mat img_keypoints;      drawkeypoints(img,keypoints,img_keypoints,Scalar::all(-1),DrawMatchesFlags::DEFAULT);
       imshow("keypoints",img_keypoints);
     */
    waitKey(0);
    return 0;
}

以下是我得到的错误:

/tmp/ccqW6zsl.o: In function `main':
sift1.cpp:(.text+0xc1): undefined reference to `cv::SIFT::SIFT(int, int, double, double, double)'
sift1.cpp:(.text+0x20e): undefined reference to `cv::imread(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
sift1.cpp:(.text+0x26b): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
sift1.cpp:(.text+0x283): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
sift1.cpp:(.text+0x2ab): undefined reference to `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
sift1.cpp:(.text+0x2c3): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
sift1.cpp:(.text+0x2db): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
sift1.cpp:(.text+0x2f3): undefined reference to `cv::equalizeHist(cv::_InputArray const&, cv::_OutputArray const&)'
sift1.cpp:(.text+0x30b): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
sift1.cpp:(.text+0x323): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
sift1.cpp:(.text+0x33b): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
sift1.cpp:(.text+0x379): undefined reference to `cv::SIFT::operator()(cv::_InputArray const&, cv::_InputArray const&, std::vector<cv::KeyPoint, std::allocator<cv::KeyPoint> >&, cv::_OutputArray const&, bool) const'
sift1.cpp:(.text+0x3c3): undefined reference to `cv::waitKey(int)'
/tmp/ccqW6zsl.o: In function `cv::Mat::~Mat()':
sift1.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x2b): undefined reference to `cv::fastFree(void*)'
/tmp/ccqW6zsl.o: In function `cv::Mat::operator=(cv::Mat const&)':
sift1.cpp:(.text._ZN2cv3MataSERKS0_[cv::Mat::operator=(cv::Mat const&)]+0xf2): undefined reference to `cv::Mat::copySize(cv::Mat const&)'
/tmp/ccqW6zsl.o: In function `cv::Mat::release()':
sift1.cpp:(.text._ZN2cv3Mat7releaseEv[cv::Mat::release()]+0x3b): undefined reference to `cv::Mat::deallocate()'
/tmp/ccqW6zsl.o: In function `cv::Feature2D::~Feature2D()':
sift1.cpp:(.text._ZN2cv9Feature2DD2Ev[cv::Feature2D::~Feature2D()]+0x46): undefined reference to `cv::DescriptorExtractor::~DescriptorExtractor()'
sift1.cpp:(.text._ZN2cv9Feature2DD2Ev[cv::Feature2D::~Feature2D()]+0x5b): undefined reference to `cv::FeatureDetector::~FeatureDetector()'
sift1.cpp:(.text._ZN2cv9Feature2DD2Ev[cv::Feature2D::~Feature2D()]+0x72): undefined reference to `cv::Algorithm::~Algorithm()'
sift1.cpp:(.text._ZN2cv9Feature2DD2Ev[cv::Feature2D::~Feature2D()]+0xa2): undefined reference to `cv::FeatureDetector::~FeatureDetector()'
sift1.cpp:(.text._ZN2cv9Feature2DD2Ev[cv::Feature2D::~Feature2D()]+0xbd): undefined reference to `cv::Algorithm::~Algorithm()'
/tmp/ccqW6zsl.o: In function `cv::SIFT::~SIFT()':
sift1.cpp:(.text._ZN2cv4SIFTD1Ev[cv::SIFT::~SIFT()]+0x1e): undefined reference to `vtable for cv::SIFT'
sift1.cpp:(.text._ZN2cv4SIFTD1Ev[cv::SIFT::~SIFT()]+0x30): undefined reference to `vtable for cv::SIFT'
sift1.cpp:(.text._ZN2cv4SIFTD1Ev[cv::SIFT::~SIFT()]+0x37): undefined reference to `vtable for cv::SIFT'
sift1.cpp:(.text._ZN2cv4SIFTD1Ev[cv::SIFT::~SIFT()]+0x42): undefined reference to `VTT for cv::SIFT'
sift1.cpp:(.text._ZN2cv4SIFTD1Ev[cv::SIFT::~SIFT()]+0x6b): undefined reference to `cv::Algorithm::~Algorithm()'
sift1.cpp:(.text._ZN2cv4SIFTD1Ev[cv::SIFT::~SIFT()]+0x9d): undefined reference to `cv::Algorithm::~Algorithm()'
collect2: ld returned 1 exit status

我无法解决这些问题。任何见解将不胜感激。提前致谢。

最佳答案

您遇到的是编译代码时的链接器错误。这是因为编译代码时,编译器找不到 OpenCV 库。因此,您需要在编译代码时链接 OpenCV 库。

简单地说,这样做:

$ g++ -Wall -g -o sift sift.cpp `pkg-config --cflags --libs opencv` 

g++ 的上述选项启用所有警告,启用 Debug模式并将输出可执行文件称为 sift

要在之后运行您的代码,请执行以下操作:

$ ./sift

关于c++ - 编译时 SIFT 代码 (C++) 中的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29466317/

相关文章:

android - Android 上 OpenCV 的优化(neon 指令,tegra 3 上的 GLSL)

c# - 如何用C#代码实现OpenCVDotNet

javascript - HTML5 Canvas 增强图像

android - 如何从不同尺寸的图像生成统一尺寸的圆形位图

javascript - 确定Javascript中黑色和白色像素之间的距离

c++ - 使用 protected 方法而不是为继承树中的每个派生类创建新的虚函数是一种好习惯吗?例如。在描述中

c++ - 找出内存被消耗的地方

c++ - 如何查找 C++ 库 (.lib) 中的类?

c++ - 使用 SDL 2.0 和 C++11 的 CMake

c++ - Code::Blocks 中 OpenCV 的代码完成