0

I'm trying to compile and run OpenCV through the command line. I can get the main Open CV libraries to work however I think my problem is with using the opencv_contrib libraries.

I have installed Open CV with home brew.

I can compile and run the below code fine from another SO question:

#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main( int argc, char** argv )
{
        Mat src = Mat(Size(320,240),CV_64F);;
        namedWindow("test");

        cout << "press any key to close" << endl;

        while(true){
                randn(src,0,1.0);
                imshow("test",src);
                if(waitKey() > 0) break;
        }
}

This is compiled like this:

g++ cv.cpp -I"/usr/local/Cellar/opencv/3.4.1_5/include" -L"/usr/local/Cellar/opencv/3.4.1_5/lib/" -lopencv_core -lopencv_highgui -o cv

Then ran ./main

enter image description here

However when I try and run anything with the opencv_contrib libraries I get this error when compiled like this:

g++ cv.cpp -I"/usr/local/Cellar/opencv/3.4.1_5/include" -L"/usr/local/Cellar/opencv/3.4.1_5/lib/" -I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core -o cv

error:

    Undefined symbols for architecture x86_64:
  "cv::xfeatures2d::SURF::create(double, int, int, bool, bool)", referenced from:
      _main in cv-f48298.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 believe this is likely due to not specifying the opencv_contrib libraries. I understand they come installed with homebrew. In my Cellar directory I have a directory for opencv and opencv_contrib. I'm not sure if the opencv_contrib directory needs to be located within the opencv directory.

But I believe everything is there as with this:

pkg-config --libs --cflags opencv

It outputs:

-I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core

But then if I add that to my command like:

g++ cv.cpp -I"/usr/local/Cellar/opencv/3.4.1_5/include" -L"/usr/local/Cellar/opencv/3.4.1_5/lib/" pkg-config --libs --cflags opencv -o cv

I get this:

clang: error: unsupported option '--libs'
clang: error: unsupported option '--cflags'
clang: error: no such file or directory: 'pkg-config'
clang: error: no such file or directory: 'opencv'

I'm able to compile the same code in Xcode and I only needed to add the search paths for the openCV directory and add the libs within there. I can run the code by opening the products folder in finder and running ./cv with images passed in. But I'm struggling with doing this all through the command line.

Any help would be great!

Elliot
  • 71
  • 9
  • when you installed opencv with homebrew, did you use the `--with-contrib` option? – Eran Jun 19 '18 at 14:23
  • Yes. I also can compile in Xcode fine by linking in other linker flags. Linking in `-I/usr/local/Cellar/opencv/3.4.1_5/include/opencv` , `-L/usr/local/Cellar/opencv/3.4.1_5/lib` , `-I/usr/local/Cellar/opencv/3.4.1_5/include` and adding all the 3.41 lib files in `/usr/local/Cellar/opencv/3.4.1_5/lib` – Elliot Jun 19 '18 at 14:31
  • it worked fine for me, using cmake to build with the command `find_package(OpenCV REQUIRED)` and later `target_link_libraries(myproject ${OpenCV_LIBS})` – Eran Jun 19 '18 at 15:36

1 Answers1

0

You want to add the output of the pkgconfig tool (you listed it above: -I/usr/local/include/opencv ...) to your compilation command line to compile correctly.

You could just manually copy this output text into your compiler invocation command:

g++ cv.cpp -I"/usr/local/Cellar/opencv/3.4.1_5/include" -L"/usr/local/Cellar/opencv/3.4.1_5/lib/" -I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core -o cv

This is usually automated by calling pkgconfig inline:

g++ cv.cpp -I"/usr/local/Cellar/opencv/3.4.1_5/include" -L"/usr/local/Cellar/opencv/3.4.1_5/lib/" `pkg-config --libs --cflags opencv` -o cv

Notice the backticks in this though - they are missing in your command line. With the backticks pkgconfig will be executed and the output (which you know what it looks like) will be inserted into the command line.

w-m
  • 10,772
  • 1
  • 42
  • 49
  • If I run: `g++ cv.cpp -I"/usr/local/Cellar/opencv/3.4.1_5/include" -L"/usr/local/Cellar/opencv/3.4.1_5/lib/" `pkg-config --libs --cflags opencv` -o cv`. I get: clang: error: unsupported option '--libs' clang: error: unsupported option '--cflags' clang: error: no such file or directory: '`pkg-config' clang: error: no such file or directory: 'opencv`' ` – Elliot Jun 19 '18 at 12:39
  • Or If I use `g++ cv.cpp -I"/usr/local/Cellar/opencv/3.4.1_5/include" -L"/usr/local/Cellar/opencv/3.4.1_5/lib/" -I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core -o cv` I get the error above related to `"cv::xfeatures2d::SURF` – Elliot Jun 19 '18 at 12:40
  • For the SURF features you need the OpenCV contrib package, which is installed with homebrew by using the `--with-contrib` flag, see e.g. https://www.pyimagesearch.com/2016/12/19/install-opencv-3-on-macos-with-homebrew-the-easy-way/ – w-m Jun 19 '18 at 14:16
  • I've already got the OpenCV contrib package. I have both directories in my Cellar directory as I stated above. I can also compile in Xcode. So I have the the SURF features. Are they going to be in a different location and therefore have to link differently? – Elliot Jun 19 '18 at 14:19
  • It seems you have several OpenCV versions installed. The Homebrew one at `/usr/local/Cellar/opencv` (which you say has xfeatures_2d). But I just noticed that the output of `pkgconfig` finds another OpenCV installation at `/usr/local/include`. Either fix your linking issue by manually adding `-lopencv_xfeatures2d` to your command line, or maybe uninstall the global OpenCV version so your pkgconfig finds the homebrew one. – w-m Jun 19 '18 at 14:35
  • Yes, I just saw that what I had linked in Xcode was far less than was outputted in. `pkg-config --libs --cflags opencv` I added all of them and now compiles with all libraries added. Your explanation makes sense, thanks. I'll look at deleting the other one. – Elliot Jun 19 '18 at 14:42