使用opencv_contrib在Mac OS终端中运行OpenCV

时间:2018-06-19 12:01:28

标签: c++ opencv

我正在尝试通过命令行编译并运行OpenCV。我可以使用主要的Open CV库,但是我认为我的问题是使用opencv_contrib库。

我已经在家庭酿造机上安装了Open CV。

我可以从另一个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;
        }
}

这是这样编译的:

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

然后运行./main

enter image description here

但是,当我尝试使用opencv_contrib库运行任何内容时,按如下方式编译时会出现此错误:

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

错误:

    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)

我认为这很可能是由于未指定opencv_contrib库。我了解他们安装了自制软件。在我的地窖目录中,我有一个opencv和opencv_contrib的目录。我不确定opencv_contrib目录是否需要位于opencv目录中。

但是我相信一切都像这样:

pkg-config --libs --cflags opencv

它输出:

-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

但是如果我将其添加到命令中,例如:

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

我明白了:

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'

我能够在Xcode中编译相同的代码,我只需要添加openCV目录的搜索路径并在其中添加库。我可以通过在finder中打开products文件夹并运行./cv并传递传入的图像来运行代码。但是我很难通过命令行来完成所有这些工作。

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:0)

您要将pkgconfig工具(您在上面列出的内容:-I/usr/local/include/opencv ...输出添加到编译命令行以正确编译。

您可以手动将此输出文本复制到编译器调用命令中:

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

这通常是通过内联调用pkgconfig来自动实现的:

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

但是请注意其中的反引号-您的命令行中缺少反引号。带有反引号的pkgconfig将被执行,输出(您知道它的样子)将被插入命令行。