编译时SIFT代码(C++)中未定义的引用

Undefined references in SIFT code (C++) while compiling

本文关键字:未定义 引用 C++ SIFT 代码 编译      更新时间:2023-10-16

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

以下是我试图在Ubuntu 12.04终端上使用g++sive1.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++的上述选项启用所有警告,启用调试模式,并将使输出可执行,称为sift

要在之后运行代码,请执行:

$ ./sift