C++ – OpenCV compile error with nonfree libraries (for Surf Descriptor) in Eclipse on Ubuntu

ceclipselinuxmakefileopencv

I have been working on this tutorial on another computer and now, after getting back home, I cannot seem to be able to run the code anymore.

I keep getting errors similar to the following one:

g++-4.8 -I/usr/local/include/opencv -I/usr/lib/i386-linux-gnu/ -I/usr/local/include/opencv2 -O2 -g -Wall -c -fmessage-length=0 -MMD -MP -MF"image-processor/references/homography.d" -MT"image-processor/references/homography.d" -o "image-processor/references/homography.o" "../image-processor/references/homography.cpp"
In file included from /usr/include/opencv2/nonfree/nonfree.hpp:46:0,
                 from ../image-processor/references/homography.cpp:10:
/usr/include/opencv2/nonfree/features2d.hpp:73:21: error: ‘vector’ has not been declared
                     vector<KeyPoint>& keypoints) const;
                     ^
/usr/include/opencv2/nonfree/features2d.hpp:73:27: error: expected ‘,’ or ‘...’ before ‘<’ token
                     vector<KeyPoint>& keypoints) const;
                           ^
/usr/include/opencv2/nonfree/features2d.hpp:77:21: error: ‘vector’ has not been declared
                     vector<KeyPoint>& keypoints,
                     ^
/usr/include/opencv2/nonfree/features2d.hpp:77:27: error: expected ‘,’ or ‘...’ before ‘<’ token
                     vector<KeyPoint>& keypoints,
                           ^
/usr/include/opencv2/nonfree/features2d.hpp:76:10: error: ‘void cv::SIFT::operator()(cv::InputArray, cv::InputArray, int) const’ cannot be overloaded
     void operator()(InputArray img, InputArray mask,
          ^
/usr/include/opencv2/nonfree/features2d.hpp:72:10: error: with ‘void cv::SIFT::operator()(cv::InputArray, cv::InputArray, int) const’
     void operator()(InputArray img, InputArray mask,
          ^
/usr/include/opencv2/nonfree/features2d.hpp:81:5: error: ‘AlgorithmInfo’ does not name a type
     AlgorithmInfo* info() const;
     ^
/usr/include/opencv2/nonfree/features2d.hpp:83:49: error: ‘vector’ has not been declared
     void buildGaussianPyramid( const Mat& base, vector<Mat>& pyr, int nOctaves ) const;
                                                 ^
/usr/include/opencv2/nonfree/features2d.hpp:83:55: error: expected ‘,’ or ‘...’ before ‘<’ token
     void buildGaussianPyramid( const Mat& base, vector<Mat>& pyr, int nOctaves ) const;
                                                       ^
/usr/include/opencv2/nonfree/features2d.hpp:84:33: error: ‘vector’ does not name a type
     void buildDoGPyramid( const vector<Mat>& pyr, vector<Mat>& dogpyr ) const;
                                 ^
/usr/include/opencv2/nonfree/features2d.hpp:84:39: error: expected ‘,’ or ‘...’ before ‘<’ token
     void buildDoGPyramid( const vector<Mat>& pyr, vector<Mat>& dogpyr ) const;
                                       ^
/usr/include/opencv2/nonfree/features2d.hpp:85:39: error: ‘vector’ does not name a type
     void findScaleSpaceExtrema( const vector<Mat>& gauss_pyr, const vector<Mat>& dog_pyr,
                                       ^
/usr/include/opencv2/nonfree/features2d.hpp:85:45: error: expected ‘,’ or ‘...’ before ‘<’ token
     void findScaleSpaceExtrema( const vector<Mat>& gauss_pyr, const vector<Mat>& dog_pyr,
                                             ^
/usr/include/opencv2/nonfree/features2d.hpp:89:40: error: ‘vector’ has not been declared
     void detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
                                        ^
/usr/include/opencv2/nonfree/features2d.hpp:89:46: error: expected ‘,’ or ‘...’ before ‘<’ token
     void detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
                                              ^
/usr/include/opencv2/nonfree/features2d.hpp:90:41: error: ‘vector’ has not been declared
     void computeImpl( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors ) const;
                                         ^
/usr/include/opencv2/nonfree/features2d.hpp:90:47: error: expected ‘,’ or ‘...’ before ‘<’ token
     void computeImpl( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors ) const;
                                               ^
/usr/include/opencv2/nonfree/features2d.hpp:125:28: error: ‘vector’ has not been declared
                     CV_OUT vector<KeyPoint>& keypoints) const;
                            ^
/usr/include/opencv2/nonfree/features2d.hpp:125:34: error: expected ‘,’ or ‘...’ before ‘<’ token
                     CV_OUT vector<KeyPoint>& keypoints) const;
                                  ^
In file included from /usr/include/opencv2/nonfree/nonfree.hpp:46:0,
                 from ../image-processor/references/homography.cpp:10:
/usr/include/opencv2/nonfree/features2d.hpp:128:28: error: ‘vector’ has not been declared
                     CV_OUT vector<KeyPoint>& keypoints,
                            ^
/usr/include/opencv2/nonfree/features2d.hpp:128:34: error: expected ‘,’ or ‘...’ before ‘<’ token
                     CV_OUT vector<KeyPoint>& keypoints,
                                  ^
/usr/include/opencv2/nonfree/features2d.hpp:127:10: error: ‘void cv::SURF::operator()(cv::InputArray, cv::InputArray, int) const’ cannot be overloaded
     void operator()(InputArray img, InputArray mask,
          ^
In file included from /usr/include/opencv2/nonfree/nonfree.hpp:46:0,
                 from ../image-processor/references/homography.cpp:10:
/usr/include/opencv2/nonfree/features2d.hpp:124:10: error: with ‘void cv::SURF::operator()(cv::InputArray, cv::InputArray, int) const’
     void operator()(InputArray img, InputArray mask,
          ^
In file included from /usr/include/opencv2/nonfree/nonfree.hpp:46:0,
                 from ../image-processor/references/homography.cpp:10:
/usr/include/opencv2/nonfree/features2d.hpp:132:5: error: ‘AlgorithmInfo’ does not name a type
     AlgorithmInfo* info() const;
     ^
/usr/include/opencv2/nonfree/features2d.hpp:142:40: error: ‘vector’ has not been declared
     void detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
                                        ^
/usr/include/opencv2/nonfree/features2d.hpp:142:46: error: expected ‘,’ or ‘...’ before ‘<’ token
     void detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
                                              ^
/usr/include/opencv2/nonfree/features2d.hpp:143:41: error: ‘vector’ has not been declared
     void computeImpl( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors ) const;
                                         ^
/usr/include/opencv2/nonfree/features2d.hpp:143:47: error: expected ‘,’ or ‘...’ before ‘<’ token
     void computeImpl( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors ) const;
                                               ^
../image-processor/references/homography.cpp: In function ‘int main(int, char**)’:
../image-processor/references/homography.cpp:83:41: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for( int i = 0; i < good_matches.size(); i++ )
                                         ^
../image-processor/references/homography.cpp:108:24: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
   char* image_window = "Window";
                        ^
make: *** [image-processor/references/homography.o] Error 1

**** Build Finished ****

I have researched quite a lot and did the following things:

  • Recompiled OpenCV adding the nonfree and contrib modules

  • Used g++-4.8 instead of g++ as compiler

  • Checked and added new includes/links for basically all the libraries in OpenCV

  • Cried a little

Here is the makefile created and used by Eclipse:

#

Automatically-generated file. Do not edit!

#

-include ../makefile.init

RM := rm

All of the sources participating in the build are defined here

-include sources.mk
-include image-processor/src/subdir.mk
-include image-processor/references/subdir.mk
-include subdir.mk
-include objects.mk

ifneq ($(MAKECMDGOALS),clean) ifneq ($(strip $(C++_DEPS)),)
-include $(C++_DEPS) endif ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS) endif ifneq ($(strip $(CC_DEPS)),)
-include $(CC_DEPS) endif ifneq ($(strip $(CPP_DEPS)),)
-include $(CPP_DEPS) endif ifneq ($(strip $(CXX_DEPS)),)
-include $(CXX_DEPS) endif ifneq ($(strip $(C_UPPER_DEPS)),)
-include $(C_UPPER_DEPS) endif endif

-include ../makefile.defs

Add inputs and outputs from these tool invocations to the build variables

All Target all: shared-robovision

Tool invocations shared-robovision: $(OBJS) $(USER_OBJS) @echo 'Building target: $@' @echo 'Invoking: GCC C++ Linker' g++

-L/usr/lib/i386-linux-gnu/ -L/usr/local/include/opencv -L/usr/local/include/opencv2 -o "shared-robovision" $(OBJS) $(USER_OBJS) $(LIBS) @echo 'Finished building target: $@' @echo ' '

Other Targets clean: -$(RM) $(OBJS)$(C++_DEPS)$(C_DEPS)$(CC_DEPS)$(CPP_DEPS)$(EXECUTABLES)$(CXX_DEPS)$(C_UPPER_DEPS)

shared-robovision -@echo ' '

.PHONY: all clean dependents .SECONDARY:

-include ../makefile.targets

Here are the libraries I included:

USER_OBJS :=

LIBS := -lopencv_core -lopencv_xfeatures2d -lopencv_nonfree
-lopencv_flann -lopencv_legacy -lopencv_contrib -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_imgcodecs -lopencv_imgproc -lopencv_video -lopencv_ml -lopencv_ts -lopencv_videostab -lopencv_highgui -lopencv_imgproc

EDIT:

My headers:

#include <stdio.h>
#include <iostream>
#include "opencv2/opencv.hpp"
#include "opencv2/opencv_modules.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include <opencv2/nonfree/nonfree.hpp>
#include <opencv2/nonfree/features2d.hpp>

using namespace cv;
using namespace std;

Best Answer

To run the sample, you need the following libs

-lopencv_core 
-lopencv_highgui 
-lopencv_features2d 
-lopencv_nonfree
-lopencv_flann 
-lopencv_calib3d

Now you need to tell the compiler where are the header files, therefore

-I/.../opencv-2.4.10/install/include

Now you need to tell the linker where are the lib files, therefore

-L/.../opencv-2.4.10/release/lib

Invoke the g++ compiler

g++ main.cpp -o test -I/.../opencv-2.4.10/install/include  -L/.../opencv-2.4.10/release/lib -lopencv_core -lopencv_highgui -lopencv_features2d -lopencv_nonfree -lopencv_flann -lopencv_calib3d

I'm using ubuntu and opencv 2.4.10. The sample is running perfectly.

Related Topic