1

I am trying to detect the keypoints and extract the descriptors of a set of images and store them in a text file for matching (which will be handled by another piece of code). This is all the code I have at the moment.

The following is the code that I'm trying to compile by using g++ sift1.cpp on the Ubuntu 12.04 terminal. I have OpenCV 2.4.9 installed.

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

And the following are the errors I get:

/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

I am unable to solve these issues. Any insight would be greatly appreciated. Thanks in advance.

  • See the above duplicate link. What you're experiencing is a linker error because when compiling, the compiler can't find the OpenCV libraries. Follow the instructions in the duplicate post to fix your problem. – rayryeng Apr 06 '15 at 06:24
  • Thank you so much sir! Sorry about the duplicate question; I'm new to Stack Overflow and OpenCV... Thank you very much! – user3271487 Apr 06 '15 at 06:27
  • Heh :) No worries. What you're experiencing is amongst the most frequently asked questions when starting with OpenCV. I've asked the same question in the past. Good luck! – rayryeng Apr 06 '15 at 06:29
  • I will write an answer to make it self-contained though. I wouldn't mind if you accepted my answer if it helped :) Good luck! – rayryeng Apr 06 '15 at 06:33
  • Thanks for the accept! Did you end up getting it to compile? – rayryeng Apr 06 '15 at 06:36
  • 1
    Yes, I did... I am a beginner-level coder... I can understand the coding part but the linking part etc. was proving to be tricky... Thanks a lot for your help, sir! – user3271487 Apr 06 '15 at 06:43

1 Answers1

1

What you are experiencing are linker errors when compiling your code. This is because when compiling your code, the compiler can't find the OpenCV libraries. Therefore, you need to link with the OpenCV libraries when compiling your code.

Simply put, do this:

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

The above options for g++ enable all warnings, enable debug mode and will make the output executable called sift.

To run your code after, do:

$ ./sift
rayryeng
  • 102,964
  • 22
  • 184
  • 193