下载CMake,https://cmake.org/download/ 下载OpenCV源码,解压 ==== GUI编译方法: 安装,启动CMake Browse Source 指到OpenCV源码目录 Browse Build 指向一个新的目录,例如/Users/lee/Downloads/opencv-3.4.1/gui_release Configure 选择Unix Makefiles 等待执行完毕 Generate cd gui_release make -j4 sudo make install ==== ==== 命令行编译方法: 安装,启动CMake,打开 Tools - How to install For Command Line Use,按照说明操作 cd /Users/lee/Downloads/opencv-3.4.1 mkdir release cd release cmake -G "Unix Makefiles" .. make sudo make install ==== ==== 在终端中使用: mkdir TestOpenCV cd TestOpenCV vi CMakeLists.txt #版本 cmake_minimum_required(VERSION 2.8) #项目名称 project( test ) #设置OpenCV所在目录,为后面的find_package提供位置 #OpenCV_DIR为OpenCV源码编译后得到的文件夹(如上面的release目录) set( OpenCV_DIR /Users/lee/Downloads/opencv-3.4.1/release ) #会在OpenCV所在目录查找配置文件OpenCVConfig.cmake find_package( OpenCV REQUIRED ) #向工程添加源文件 add_executable( test main.cpp ) #添加链接库 target_link_libraries( test ${OpenCV_LIBS} ) vi main.cpp #includeusing namespace cv; int main(int argc, char** argv) { Mat image; image = imread("/Users/lee/Downloads/20180128214955.jpg", 1); namedWindow("Display Image", WINDOW_AUTOSIZE); imshow("Display Image", image); waitKey(0); return 0; } cmake . make ./test 会显示对应的图片 ==== ==== 在Xcode中使用: 创建C++项目 项目属性 - Build Settings - Search Paths Header Search Path = /usr/local/include/ Library Search Path = /usr/local/lib 在项目下创建lib目录,Add Files To,Command+Shift+G,输入/Usr/local/lib,将所有不带箭头的文件添加进去 #include using namespace cv; int main(int argc, char** argv) { Mat image; image = imread("/Users/lee/Downloads/20180128214955.jpg", 1); namedWindow("Display Image", WINDOW_AUTOSIZE); imshow("Display Image", image); waitKey(0); return 0; } 编译运行 ====