Mac下安装OpenCV(配置Xcode)
在Mac上安装OPenCV,使用的开发工具是Xcode7.3,安装的版本是2.4.12。(这是我第一次使用Markdown编写文档,感觉还不错。2016.03.31)
1.安装Cmake
使用安装工具是Homebrew,但是有个错误,使用命令brew install cmake
安装cmake在使用的时候会提示找不到cmake
命令。所以使用brew cask install cmake
来安装Cmake。猜测是因为brew cask
能自动链接到程序,而brew
只是安装,不负责环境变量的设置。
2.下载和编译OPenCV
2.1 下载OpenCV
下载地址
1>官网网站:http://opencv.org
2>sourceforge:http://sourceforge.net/projects/opencvlibrary/
下载完成之后把源代码拷贝到让自己高兴的任何地方。
2.2 编译OPenCV
起初使用的安装方法也是brew install openCV
安装,但是在运行测试代码的时候报错,libpng相关的错误,未找到解决方法,最终使用以下方法编译:
cd <the path of opencv source>
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
make
sudo make install
到此OpenCV的安装已经完成,安装路径为/usr/local
,该路径可以修改为让你高兴的路径。它安装的文件分布为:
1>头文件:
/usr/local/include
目录包含两个文件夹:opencv
和opencv2
。2>lib文件:
/usr/local/lib
目录下包含了opencv
相关的动态库。
如果想卸载OpenCV,则可以回到build
目录下,执行sudo make uninstall
,然后删除上面提到的文件即可。
参考内容:
Building OpenCV from Source Using CMake, Using the Command Line
3.使用Xcode进行OpenCV开发
1.Open Xcode, choose New->New Project->Command Line Tool
2.Name it and select C++
for type
3.Click on your project from the left menu. Click the build settings
tab from the top. Filter all. Scroll to Search Paths
. Under header search paths
, for debug and release, set the path to /usr/local/include
.Under library search paths
, set the path to $(PROJECT_DIR)
. Finally, check if C++ standard library
is libstdc++
or not, if not, change it to this!
4.Click on your project from the left menu. File->New->New Group
, Name the group OpenCV Frameworks.
5.Select the folder (group) you just labeled, OpenCV Frameworks
in the left menu. Go to File->add Files
, Type 'command+shift+G'
, which will allow you to manually go to a folder. Go to -> /usr/local/lib
6.Select both of these files, libopencv_core.dylib
, libopencv_highgui.dylib
, and click Add
. (you may need to add other library files from this folder to run other code.)
7.You must include this line of code in the beginning of your main.cpp file:
#include <opencv2/opencv.hpp>
修改main.cpp代码进行测试,如下:
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv) {
Mat image;
image = imread("/Users/hd/Pictures/hd.jpg", 1);
namedWindow("xx ", WINDOW_AUTOSIZE);
imshow("xx ", image);
waitKey(0);
return 0;
}