简体   繁体   English

在mac os x sierra上使用opencv c ++编译hello world

[英]Compiling hello world with opencv c++ on mac os x sierra

First time I cannot compile hello world. 我第一次无法编译你好世界。 I've followed tons of tutorials how to install opencv. 我已经按照大量的教程如何安装opencv。 I just have following example: 我只有以下示例:

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

int main()
{

    Mat imageMe = imread("test.jpg", 0);
    return 0;
}

from opencv website. 来自opencv网站。 Looks straightforward, but it won't compile. 看起来很简单,但它不会编译。

g++ display_image.cpp `pkg-config --cflags --libs opencv` -l

Here's the error: 这是错误:

display_image.cpp:2:10: fatal error: 'opencv2/core/core.hpp' file not found
#include <opencv2/core/core.hpp>
         ^
1 error generated.

And running: 并运行:

pkg-config --cflags --libs opencv 

return this: 归还这个:

-I/usr/local/Cellar/opencv3/3.2.0/include/opencv -I/usr/local/Cellar/opencv3/3.2.0/include/opencv2 -L/usr/local/Cellar/opencv3/3.2.0/lib -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -l 

these files are present. 这些文件存在。

Running ls /usr/local/Cellar/opencv3/3.2.0/include/opencv2/core | grep core.hpp 运行ls /usr/local/Cellar/opencv3/3.2.0/include/opencv2/core | grep core.hpp ls /usr/local/Cellar/opencv3/3.2.0/include/opencv2/core | grep core.hpp Gives the result: ls /usr/local/Cellar/opencv3/3.2.0/include/opencv2/core | grep core.hpp给出结果:

core.hpp

What's the problem? 有什么问题?

edit: I've been using following tutorials to install opencv on my machine: 编辑:我一直在使用以下教程在我的机器上安装opencv:

http://www.pyimagesearch.com/2016/12/19/install-opencv-3-on-macos-with-homebrew-the-easy-way/ http://www.pyimagesearch.com/2016/12/19/install-opencv-3-on-macos-with-homebrew-the-easy-way/

https://www.learnopencv.com/install-opencv-3-on-yosemite-osx-10-10-x/ https://www.learnopencv.com/install-opencv-3-on-yosemite-osx-10-10-x/

https://blogs.wcode.org/2014/10/howto-install-build-and-use-opencv-macosx-10-10/ https://blogs.wcode.org/2014/10/howto-install-build-and-use-opencv-macosx-10-10/

http://seeb0h.github.io/howto/howto-install-homebrew-python-opencv-osx-el-capitan/ http://seeb0h.github.io/howto/howto-install-homebrew-python-opencv-osx-el-capitan/

edit: 编辑:

Even after all these answers I cannot compile a single app. 即使在所有这些答案之后,我也无法编译单个应用程序。 Even if it somehow can find .hpp file directly included in my file it fails finding includes inside. 即使它以某种方式可以找到直接包含在我的文件中的.hpp文件,但它找不到包含在里面。

Probably my configuration is wrong. 可能我的配置错了。 In /usr/local/Cellar/opencv3 I have 2 directories: 3.2.0 - probably installed binaries HEAD-9053839 - compiled from source 在/ usr / local / Cellar / opencv3我有2个目录:3.2.0 - 可能安装了二进制文件HEAD-9053839 - 从源代码编译

Now I also have opencv.pc file located in /usr/local/lib/pkgconfig: 现在我还有/ usr / local / lib / pkgconfig中的opencv.pc文件:

prefix=/usr/local/Cellar/opencv3/3.2.0       
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib

Name: opencv
Description: The opencv library
Version: 2.x.x
Cflags: -I${includedir}/opencv -I${includedir}/opencv2
Libs: -L${libdir} -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -l

As a prefix I've also tried HEAD-9053839. 作为前缀,我也尝试过HEAD-9053839。

I've created even simplier example: 我创造了更简单的例子:

#include <opencv2/opencv.hpp>
int main(){return 0; }

It's still telling me that 'opencv2/opencv.hpp' file not found. 它仍然告诉我找不到'opencv2 / opencv.hpp'文件。 Then I printed what pkg-config says: pkg-config --cflags --libs opencv 然后我打印了pkg-config所说的内容:pkg-config --cflags --libs opencv

-I/usr/local/Cellar/opencv3/3.2.0/include/opencv -I/usr/local/Cellar/opencv3/3.2.0/include/opencv2 -L/usr/local/Cellar/opencv3/3.2.0/lib -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -l 

And found out it'd rather should be directly include dir, not opencv/opencv2 subdirectories. 并且发现它宁可应该直接包含dir,而不是opencv / opencv2子目录。

So I've ran g++ like this: 所以我像这样运行g ++:

g++ -I/usr/local/Cellar/opencv3/3.2.0/include -L/usr/local/Cellar/opencv3/3.2.0/lib -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -l display_image.cpp

Then it told me: 然后它告诉我:

ld: library not found for -lopencv_contrib

After removing it it said: 删除它后说:

ld: library not found for -lopencv_legacy

Then I had to remove -l before display_image.cpp, but then I've got another error: 然后我必须在display_image.cpp之前删除-l,但后来又出现了另一个错误:

Undefined symbols for architecture x86_64:
  "cv::String::deallocate()", referenced from:
      cv::String::~String() in display_image-9d8f86.o
      cv::String::operator=(cv::String const&) in display_image-9d8f86.o

This is hell. 这是地狱。

I don't have a compilation environment to regenerate your error but I think the problem is about these lines in your code: 我没有编译环境来重新生成您的错误,但我认为问题在于您的代码中的这些行:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

since the opencv directory is being searched from 因为正在搜索opencv目录

/usr/local/Cellar/opencv3/3.2.0/include/opencv2

as your "pkg-config --cflags --libs opencv" implies. 正如你的“pkg-config --cflags --libs opencv”所暗示的那样。 So, I suppose, changing the include lines into 所以,我想,将包含行更改为

#include <core/core.hpp>
#include <highgui/highgui.hpp>

would help, or you can implicitly declare the directory you want to search like this: 会有所帮助,或者您可以隐式声明要搜索的目录,如下所示:

g++ -I/usr/local/Cellar/opencv3/3.2.0/include display_image.cpp ...

In OpenCV 3.x , the layout of the header files has changed and now you pretty much just use: OpenCV 3.x中 ,头文件的布局已经改变,现在你几乎只使用:

#include <opencv2/opencv.hpp>

So, in concrete terms, I am suggesting you replace your 2nd and 3rd lines with the above incantation. 所以,具体而言,我建议你用上面的咒语替换你的第二和第三行。

See the OpenCV Transition Guide . 请参阅OpenCV过渡指南

Soner's answer should solve the OP's problem. Soner的答案应该解决OP的问题。 I want to add some other solutions. 我想补充一些其他解决方案。

If you use CMake to install openCV from source, it will put OpenCV libraries in /usr/local/lib and OpenCV headers in /usr/local/include . 如果您使用CMake从源代码安装openCV,它会将OpenCV库放在/usr/local/lib/usr/local/include OpenCV头文件中。 Where the headers all go in the subfolder /usr/local/include/opencv . 标题全部放在子文件夹/usr/local/include/opencv Since they are default library and header directories for most IDE's, all the examples you find will work with that config. 由于它们是大多数IDE的默认库和头文件目录,因此您找到的所有示例都将使用该配置。

Now, brew does not install OpenCV to the above folders as you already knew. 现在, brew不会将OpenCV安装到您已经知道的上述文件夹中。 So you have three options: 所以你有三个选择:

  1. Change compiler's search paths for OpenCV libraries and headers: g++ -I/usr/local/Cellar/opencv3/3.2.0/include -L/usr/local/Cellar/opencv3/3.2.0/lib 更改编译器的OpenCV库和头文件的搜索路径: g++ -I/usr/local/Cellar/opencv3/3.2.0/include -L/usr/local/Cellar/opencv3/3.2.0/lib

  2. Soft link /usr/local/Cellar/opencv3/3.2.0/include/opencv to /usr/local/include and /usr/local/Cellar/opencv3/3.2.0/lib to /usr/local/lib 软链接/usr/local/Cellar/opencv3/3.2.0/include/opencv/usr/local/include/usr/local/Cellar/opencv3/3.2.0/lib/usr/local/lib

  3. Install OpenCV from source with CMake. 使用CMake从源安装OpenCV。

I did it! 我做的! It was a problem with pkg-config. 这是pkg-config的问题。 In /usr/local/Cellar/opencv3/3.2.0/lib/pkgconfig I've found opencv.pc, which looks like this: /usr/local/Cellar/opencv3/3.2.0/lib/pkgconfig我发现opencv.pc,如下所示:

# Package Information for pkg-config

prefix=/usr/local/Cellar/opencv3/3.2.0
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.2.0
Libs: -L${exec_prefix}/lib -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core
Libs.private: -framework OpenCL -L/usr/local/opt/jpeg/lib -ljpeg -L/usr/local/lib -lpng -ltiff -lImath -lIlmImf -lIex -lHalf -lIlmThread -L/usr/lib -lz -framework Cocoa -framework AVFoundation -framework CoreGraphics -framework CoreMedia -framework CoreVideo -framework QuartzCore -framework Accelerate
Cflags: -I${includedir_old} -I${includedir_new}

I've replaced old opencv.pc located in: /usr/local/lib/pkgconfig Now I'm not getting any errors. 我已经替换了位于以下的旧opencv.pc: /usr/local/lib/pkgconfig现在我没有收到任何错误。

I also had to use different include file: 我还必须使用不同的包含文件:

#include <opencv2/opencv.hpp>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM