xcode OpenCv 2.4.3:找不到 imread 和 SurfFeatureDetector::detect

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14940126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:47:54  来源:igfitidea点击:

OpenCv 2.4.3: can't find imread and SurfFeatureDetector::detect

c++xcodeopencvosx-snow-leopard

提问by Reb.Cabin

on Mac OSX 10.8, XCode 4.6, in C++

Mac OSX 10.8,XCode 4.6,C++

I'm following the tutorial in opencv_tutorials.pdf, located in /opt/local/share/OpenCV/docon my system. On page 311, we get this sample (slightly edited):

我正在学习 中的教程opencv_tutorials.pdf,位于/opt/local/share/OpenCV/doc我的系统中。在第 311 页上,我们得到了这个示例(略有编辑):

#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv; 
int main( int argc, char** argv ) {
  Mat img_1 = imread( argv[1], CV_LOAD_IMAGE_GRAYSCALE );
  int minHessian = 400;
  SurfFeatureDetector detector( minHessian );
  std::vector<KeyPoint> keypoints_1;
  detector.detect( img_1, keypoints_1 );
}

I had to modify the code above to #include "opencv2/nonFree/features2d.hpp", a file that I found by running

我不得不将上面的代码修改为#include "opencv2/nonFree/features2d.hpp",我通过运行找到的文件

find /opt/local/include -name "*.hpp" -exec grep SurfFeatureDetector "{}" ';' -print

The code compiles, meaning that the symbol detector.detectis found in the hppfile and passes type-checking by the C++ compiler. Next is to try to find the libraries that contain the symbols.

代码编译,这意味着detector.detecthpp文件中找到符号并通过 C++ 编译器的类型检查。接下来是尝试查找包含符号的库。

Now, I have these libraries in my distribution, in /opt/local/lib:

现在,我的发行版中有这些库,位于/opt/local/lib

libopencv_calib3d.2.4.3.dylib
libopencv_contrib.2.4.3.dylib
libopencv_core.2.4.3.dylib
libopencv_features2d.2.4.3.dylib
libopencv_flann.2.4.3.dylib
libopencv_gpu.2.4.3.dylib
libopencv_highgui.2.4.3.dylib
libopencv_imgproc.2.4.3.dylib
libopencv_legacy.2.4.3.dylib
libopencv_ml.2.4.3.dylib
libopencv_nonfree.2.4.3.dylib
libopencv_objdetect.2.4.3.dylib
libopencv_photo.2.4.3.dylib
libopencv_stitching.2.4.3.dylib
libopencv_ts.2.4.3.dylib
libopencv_video.2.4.3.dylib
libopencv_videostab.2.4.3.dylib

I found it uninformative to nmsearch these for the required symbols because name mangling is difficult to decompile in my head. I just brute-force included all the libraries, but still get linker errors:

我发现在nm这些文件中搜索所需的符号并没有什么信息,因为名称修改很难在我的脑海中反编译。我只是蛮力包含了所有库,但仍然出现链接器错误:

Undefined symbols for architecture x86_64:
  "cv::imread(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
      _main in main.o
  "cv::FeatureDetector::detect(cv::Mat const&, std::__1::vector<cv::KeyPoint, std::__1::allocator<cv::KeyPoint> >&, cv::Mat const&) const", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64

I've done some spot-checking of the OpenCv reference documents and the wiki pages, starting from here

我已经对 OpenCv 参考文档和 wiki 页面进行了一些抽查,从这里开始

http://opencv.willowgarage.com/documentation/cpp/features2d__feature_detection_and_descriptor_extraction.html

but without success.

但没有成功。

I'd be grateful for

我会很感激

  1. specific help in finding these OpenCv APIs
  2. general strategies for this kind of query: "what library contains the symbol I need to link against". This kind of problem makes me hate C++, since 5 minutes of coding always seems to lead to hours of hit-and-miss library searching, possibly because I just don't know the professional secret for doing this.
  1. 查找这些 OpenCv API 的具体帮助
  2. 这种查询的一般策略:“哪个库包含我需要链接的符号”。这种问题让我讨厌 C++,因为 5 分钟的编码似乎总是会导致数小时的库搜索,这可能是因为我只是不知道这样做的专业秘诀。

回答by fduff

On Linux and using OpenCV 3.0.0I had to link with the opencv_imgcodecsshared lib.

在 Linux 上并使用OpenCV 3.0.0我必须与opencv_imgcodecs共享库链接。

回答by Radford Parker

My toolchain utilizes CMake to keep track of these sorts of things. For most new libraries I use, there are usually google searchable accompanying Find*.cmake files that are written by others. If you wanted more information about my specific set-up, feel free to ask. I have seen this specific problem (Undefined symbols for architecture x86_64) a few times for things in the nonfree libraries. I'm not sure why you are getting that error for the imread, though. The first place I would look is the matcher_simple.cpp example in the samples/cpp folder. If you can compile and run this (using this) and the other examples, your set-up should be ok. If you can't, can you please post the errors of that example? I'm not familiar with XCode but someone elsesuggested going to Build Settings>Build Options> Compiler for C/C++/Objective-C and change it from Apple LLVM Compiler to LLVM GCC 4.2. And there are a few other guesses heretoo.

我的工具链使用 CMake 来跟踪这些事情。对于我使用的大多数新库,通常有其他人编写的 Find*.cmake 文件随附 google 可搜索文件。如果您想了解有关我的特定设置的更多信息,请随时询问。对于非自由库中的内容,我已经多次看到这个特定问题(体系结构 x86_64 的未定义符号)。不过,我不确定为什么您会收到 imread 的错误。我首先要看的是 samples/cpp 文件夹中的 matcher_simple.cpp 示例。如果您可以编译并运行它(使用this)和其他示例,那么您的设置应该没问题。如果你不能,你能不能把那个例子的错误贴出来?我不熟悉 XCode 但其他人建议去 Build Settings>Build Options> Compiler for C/C++/Objective-C 并将其从 Apple LLVM Compiler 更改为 LLVM GCC 4.2。还有一些其他的猜测在这里了。

回答by Tom Hanley

Try including -lopencv_features2dand -lopencv_nonfreein your list of Other Linker Flags in the Build Settings. I was getting the same problems and it worked for me

尝试在 Build Settings 中的 Other Linker Flags 列表中包含-lopencv_features2d-lopencv_nonfree。我遇到了同样的问题,它对我有用