Problem compiling nonfree opencv modules


#1

Hi!

For a personal project I’m trying to identify if a small image is part of a bigger image.

After reading this forum and other websites I learned about the SIFT feature detection and matching algorithm. I also learned that the algorithm is proprietary (and therefore not included in the opencv library by default) and that, in order to use it, I should compile opencv with the opencv_contrib modules… but despite all my efforts I haven’t been able to use it with openframeworks.

My problem is that after I compile the library and run the code I get the following error:

Undefined symbols for architecture x86_64:
“cv::Feature2D::write(cv::FileStorage&) const”, referenced from:
vtable for cv::xfeatures2d::SIFT in ofApp.o
construction vtable for cv::Feature2D-in-cv::xfeatures2d::SIFT in ofApp.o
“cv::Feature2D::read(cv::FileNode const&)”, referenced from:
vtable for cv::xfeatures2d::SIFT in ofApp.o
construction vtable for cv::Feature2D-in-cv::xfeatures2d::SIFT in ofApp.o
“cv::Feature2D::getDefaultName() const”, referenced from:
vtable for cv::xfeatures2d::SIFT in ofApp.o
construction vtable for cv::Feature2D-in-cv::xfeatures2d::SIFT in ofApp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I’m using Xcode 10.1 and Mojave.

I’m quite new to OF and the first time I attempt to compile a library, so I’m probably doing something (or, well, several things) wrong.

Is there a way to fix that issue? Or a different approach I could use (for instance, could I find the library compiled somewhere?)


Here’s the process I’ve followed:

  1. Downloaded the 3.4.3 version of the opencv source
  2. Downloaded the 3.4 branch of opencv_contrib
  3. Used CMAKE to configure and generate the binaries using the following parameters:- Specified the “Unix Makefiles” generator and “Use default native compilers”
    • Unchecked BUILD_SHARED_LIBS and BUILD_TESTS (per Shiffman’s tutorial)
    • Pointed the OPENCV_EXTRA_MODULES_PATH to the opencv_contrib modules directory
    • Unchecked WITH_1394 and WITH_FFMPEG (per this guide)
    • Checked OPENCV_ENABLE_NONFREE
    • Set the CMAKE_OSX_SYSROOT to point to “/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk”
    • Added x86_64 to CMAKE_OSX_ARCHITECTURES (per this guide)

After setting that configuration I click on “Configure” again and then on “Generate”. When that has finished I run “make” and then “sudo make install” in the build directory.

After those commands are finished without errors my /usr/local/include/opencv2 directory contains the compiled modules with xfeatures2d.hpp and xfeatures2d/nonfree.hpp too.

Next thing I do is go to the addons folder of OF, open the ofxOpenCv directory and replace the /openframeworks/addons/ofxOpenCv/libs/opencv/include/opencv2 directory with the one I generated.

I also update the /openframeworks/addons/ofxOpenCv/libs/opencv/lib/emscripten directory with the libs I generated in the building process.

Then I open projectGenerator and create a new project with ofxCv and ofxOpenCv.

In order to use SIFT I add the following lines to ofApp.h:

#include “ofxOpenCv.h”
#include “ofxCv.h”

#include “opencv2/xfeatures2d/nonfree.hpp”

using namespace cv;
using namespace ofxCv;

and then, just to test everything, I add:

cv::xfeatures2d::SiftFeatureDetector detector;

to ofApp::setup()


#2

Ok, I just learned about apothecary, so I’m going to try to build the library using it.


#3

Hi all,

I just went through more or less the same process and I thought it is running but I celebrated to early yesterday before end of the day …

/usr/local/include/opencv2/core/mat.inl.hpp:599: error: undefined reference to 'cv::Mat::updateContinuityFlag() (for opencv3.4.4)

@javierarce I’m on Ubuntu, so the emscripten folder and actually the whole ofxOpenCv lib folder is not used (at least in QtCreator). Under linux opencv is installed on system level and configured via PKG. You can check the addons.make to see what is used for your OS.

My steps

  • I tried opencv4 and opencv344.
  • Build both using this guide via cmake-gui and enabled PKG-config in the settings.
  • The new versions are installed in /usr/local and seem to be set up properly via pkg-config.
  • In ofxOpenCv/addons.make I switched to the corresponding pkg opencv package (called opencv3x or opencv4x)
  • After updating the QtCreator project the includes are all found, even the xfeatures2d but I get different undefined references nevertheless (undefined reference cv::Mat::updateContinuityFlag()).

This is my opencv 3.4.4 pkg-config file:

> # Package Information for pkg-config
> 
> prefix=/usr/local
> exec_prefix=${prefix}
> libdir=${exec_prefix}/lib
> includedir_old=${prefix}/include/opencv
> includedir_new=${prefix}/include
> 
> Name: OpenCV
> Description: Open Source Computer Vision Library
> Version: 3.4.4
> Libs: -L${exec_prefix}/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_photo -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ml -lopencv_ximgproc -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_flann -lopencv_xphoto -lopencv_imgproc -lopencv_core
> Libs.private: -ldl -lm -lpthread -lrt
> Cflags: -I${includedir_old} -I${includedir_new}

Cheers,
Felix


#4

I was able to solve it by uninstalling the old opencv installation. There must have been a conflict. I removed all libopencv via sudo apt remove