嘛,就当是记录一下好了,只需要知道自己要安装的 OpenCV 的版本号就行~
顺便处理了一下 OpenCV 4 之后生成 pkgconfig 文件「opencv4.pc」的小坑,
OpenCV 3 和 4 都没问题,2 的话没有测试过;然后 Python binding 是 Python 3,因为 Python 2 本身也快 deprecated 了。最后就是需要有 cmake, curl, unzip 和 pip。当然 freetype
和 harfbuzz
包也是需要的,用对应的包管理软件安装一下即可~
于是下面的代码需要根据自己需求改的部分就是一个 OpenCV 的版本号,然后是跟生成 pkgconfig 文件有关的那两行,OpenCV 3 应该不用的,OpenCV 4 则需要加上。
export OPENCV_VER=4.4.0 export OPENCV_SRC="https://github.com/opencv/opencv/archive/${OPENCV_VER}.zip" export OPENCV_CONTRIB_SRC="https://github.com/opencv/opencv_contrib/archive/${OPENCV_VER}.zip" curl -L -o "opencv-${OPENCV_VER}.zip" "$OPENCV_SRC" curl -L -o "opencv_contrib-${OPENCV_VER}.zip" "$OPENCV_CONTRIB_SRC" unzip "opencv-${OPENCV_VER}.zip" 1>&2 > /dev/null unzip "opencv_contrib-${OPENCV_VER}.zip" 1>&2 > /dev/null export OPENCV="`pwd`/opencv-${OPENCV_VER}" export OPENCV_CONTRIB="`pwd`/opencv_contrib-${OPENCV_VER}/modules" pip3 install numpy mkdir -p "${OPENCV}/build" cd "${OPENCV}/build" cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D OPENCV_EXTRA_MODULES_PATH="${OPENCV_CONTRIB}" \ -D PYTHON3_LIBRARY=`python3 -c 'import subprocess ; import sys ; s = subprocess.check_output("python3-config --configdir", shell=True).decode("utf-8").strip() ; (M, m) = sys.version_info[:2] ; print("{}/libpython{}.{}.dylib".format(s, M, m))'` \ -D PYTHON3_INCLUDE_DIR=`python3 -c 'import distutils.sysconfig as s; print(s.get_python_inc())'` \ -D PYTHON3_EXECUTABLE=`which python3` \ -D BUILD_opencv_python2=OFF \ -D BUILD_opencv_python3=ON \ -D INSTALL_PYTHON_EXAMPLES=OFF \ -D INSTALL_C_EXAMPLES=OFF \ -D OPENCV_ENABLE_NONFREE=ON \ -D OPENCV_GENERATE_PKGCONFIG=ON \ -D OPENCV_PC_FILE_NAME=opencv4.pc \ -D BUILD_EXAMPLES=OFF ..
等生成好 Unix makefile 之后就可以开始编译了~macOS 上则是
make -j`sysctl -a | grep 'hw.ncpu' | awk '{print \\$2}'`
*nix 则是
make -j`nproc`
最后安装即可~
sudo make install