1、下载opencv
https://opencv.org/releases/
推荐下载2.4.13或者 3.2.0
2、安装
cd /path/of/your/downloaded/opencv
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=/path/to/install/opencv -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF ..
make -j8
make install
3、编译成功后,配置环境变量
c++需要的环境配置:
vim ~/.zshrc (我用的是zsh)
添加
export OpenCV_DIR=/path/to/install/opencv/shared
export DYLD_LIBRARY_PATH=/path/to/install/opencv/shared/lib
配置python的环境:
添加:(控制台可用)
export PYTHONPATH=/path/to/install/opencv/lib/python2.7/site-packages:$PYTHONPATH
(项目中可用)
如果需要编译的opencv 的python接口,加入到特定的python环境中,需要将编译完的动态链接库,设置一下软连接。不然找不到用户安装的opencv python环境。
这里是链接到mac系统自带的python2.7环境下:
sudo ln -s /path/to/install/opencv/lib/python2.7/site-packages/cv2.so /Library/Python/2.7/site-packages/cv2.so
4、测试环境安装成功
c++:
编写main.cpp:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main() {
Mat srcImage = imread("/Users/gpf/Desktop/1.png");
imshow("[img]", srcImage);
waitKey(0);
return 0;
}
CmakeList:
cmake_minimum_required(VERSION 3.14)
project(clion_code)
find_package(OpenCV)
include_directories( ${OpenCV_INCLUDE_DIRS} )
set(CMAKE_CXX_STANDARD 11)
add_executable(clion_code main.cpp)
target_link_libraries( clion_code ${OpenCV_LIBS} )
python:
新建python file mian.py: (进行了动态库的软链接)
import cv2
print( cv2.__version__)
或者直接在命令行:
进入python命令行窗口,
imoort cv2
不会报错即配置环境变量成功。(配置了.zshrc 文件)
网友评论