2015-04-06 161 views
1

我試圖檢測關鍵點並提取一組圖像的描述符,並將它們存儲在文本文件中進行匹配(將由另一段代碼處理)。這是我目前所有的代碼。編譯時在SIFT代碼(C++)中未定義的引用

以下是我試圖在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 

我無法來解決這些問題。任何有識之士將不勝感激。提前致謝。

+0

看到上面的重複鏈接。你遇到的是一個鏈接器錯誤,因爲編譯時,編譯器找不到OpenCV庫。按照重複帖子中的說明解決問題。 – rayryeng 2015-04-06 06:24:48

+0

非常感謝您的先生!對重複的問題抱歉;我是Stack Overflow和OpenCV的新手......非常感謝! – user3271487 2015-04-06 06:27:54

+0

呵呵:)不用擔心。從OpenCV開始,您遇到的是最常見的問題。我過去曾問過同樣的問題。祝你好運! – rayryeng 2015-04-06 06:29:10

回答

1

編譯代碼時遇到的問題是鏈接器錯誤。這是因爲編譯代碼時,編譯器找不到OpenCV庫。因此,編譯代碼時需要鏈接到OpenCV庫。

簡單地說,這樣做:

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

g++以上選項使所有的警告,啓用調試模式,並會叫sift輸出可執行文件。

後運行你的代碼,這樣做:

$ ./sift 
相關問題