在Mac终端中运行OpenCV 3 [英] Running OpenCV 3 in Mac Terminal
问题描述
过去一周,我一直在尝试使用终端运行一些简单的OpenCV程序。我已尝试过各种论坛的许多教程和建议,但收效甚微。尝试将OpenCV头文件链接到我的OpenCV主程序时出现问题。对于一个简单的c ++程序,我只需执行 g ++ main.cpp header.hpp
来生成可执行程序。如何链接必要的OpenCV头文件,例如< opencv2 / highgui / highgui.hpp>
& < opencv2 /核心/ core.hpp>
?
例如,当试图从
请记住,你可能已经安装了opencv / usr / local
文件夹不是 / opt / local
取决于你如何编译/安装OpenCV。
此外,您可能安装了pkg-config,当您需要链接更多库时,它可以派上用场。
例如,您可以运行:
pkg-config - -libs --cflags opencv
在我的情况下输出:
-I / opt / local / include / opencv -I / opt / local / include -L / opt / local / lib -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d - lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab
但是在你的情况下,它应该输出你特定的OpenCV路径。
这将简化编译到这个:
g ++ main.cpp`pkg-config --libs --cflags opencv` -o main
您链接的指南使用 cmake
为您生成Makefile。
这是另一个不错的选择。此外,基于相同的指南,您应该安装XCode,您可以使用它来创建命令行工具并指向标题搜索路径和库搜索路径。
< a href =https://i.stack.imgur.com/PyAJX.png =nofollow noreferrer>
For the past week, I have been trying to run some simple OpenCV programs using the terminal. I have tried many tutorials and recommendations from various forums with little success. The problem arises when trying to link the OpenCV header files to my OpenCV main program. For a simple c++ program I would simply execute g++ main.cpp header.hpp
to generate the program executeable. How do I go about linking the necessary OpenCV header files such as <opencv2/highgui/highgui.hpp>
& <opencv2/core/core.hpp>
?
For example, when attempted to execute the sample program from http://docs.opencv.org/2.4/doc/tutorials/introduction/display_image/display_image.html the following occurs:
Desktop Robert$ g++ loadIMG.cpp Undefined symbols for architecture x86_64: "cv::namedWindow(cv::String const&, int)", referenced from: _main in loadIMG-54c517.o "cv::Mat::deallocate()", referenced from: cv::Mat::release() in loadIMG-54c517.o "cv::Mat::copySize(cv::Mat const&)", referenced from: cv::Mat::operator=(cv::Mat const&) in loadIMG-54c517.o "cv::String::deallocate()", referenced from: cv::String::~String() in loadIMG-54c517.o "cv::String::allocate(unsigned long)", referenced from: cv::String::String(char const*) in loadIMG-54c517.o "cv::imread(cv::String const&, int)", referenced from: _main in loadIMG-54c517.o "cv::imshow(cv::String const&, cv::_InputArray const&)", referenced from: _main in loadIMG-54c517.o "cv::waitKey(int)", referenced from: _main in loadIMG-54c517.o "cv::fastFree(void*)", referenced from: cv::Mat::~Mat() in loadIMG-54c517.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
Please note: OpenCV has already been built using the following tutorial: http://blogs.wcode.org/2014/10/howto-install-build-and-use-opencv-macosx-10-10/
Any help or direction would be appreciated. Thank you.
You haven't specified:
- the include path (header search path) using
-I"/path/to/your/include"
- the path to the libraries using -L"/path/to/libraries"
- which libraries to link against, in this case core and highgui: -lopencv_core -lopencv_highgui
I have opencv headers in /opt/local/include
and libraries in /opt/local/lib
,
so to compile a basic program like this:
#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;
}
}
I compiled like so:
g++ main.cpp -I"/opt/local/include/" -L"/opt/local/lib/" -lopencv_core -lopencv_highgui -o main
Then ran ./main
:
Bare in mind you might have opencv installed in the /usr/local
folder not /opt/local
depending how you compiled/installed OpenCV.
Also, you might have pkg-config installed which can come in handy when you need to link against more libraries.
For example, you can run:
pkg-config --libs --cflags opencv
which in my case outputs:
-I/opt/local/include/opencv -I/opt/local/include -L/opt/local/lib -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab
but in your case, it should output your particular OpenCV paths.
This would simplify compiling to this:
g++ main.cpp `pkg-config --libs --cflags opencv` -o main
The guide you linked to uses cmake
which generates Makefiles for you.
That's another nice options. Also, based on the same guide, you should have XCode installed which you can use to create a Command Line Tool and point the Header Search Paths and Library Search Paths.
这篇关于在Mac终端中运行OpenCV 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!