This tutorial provides step-by-step instructions to compile OpenCV 4 with Qt 5 for Python 3 and C++ on macOS High Sierra 10.13. For the latest macOS version, I think you can install OpenCV from Homebrew directly. I wrote this tutorial on Dec. 2020 and the High Sierra is a previous macOS system and installing OpenCV directly from Homebrew will fail so I have to compile it manually.
Installation Dependencies
1 2 3 4 5 6 7 8 |
brew install cmake pkg-config brew install jpeg libpng libtiff openexr brew install eigen tbb brew install qt5 brew install python@3.7 pip3 install numpy |
Clone OpenCV
1 2 3 4 5 |
git clone -b 4.5.0 --depth=1 https://github.com/opencv/opencv.git opencv git clone -b 4.5.0 --depth=1 https://github.com/opencv/opencv_contrib.git opencv_contrib cd opencv mkdir build cd build |
Compile OpenCV
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
export SDKROOT="/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk" cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D OPENCV_GENERATE_PKGCONFIG=ON \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D INSTALL_C_EXAMPLES=ON \ -D OPENCV_ENABLE_NONFREE=ON \ -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \ -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=/usr/local/opt/python@3.7/bin/python3 \ -D BUILD_opencv_python2=OFF \ -D BUILD_opencv_python3=ON \ -D WITH_QT=ON \ -D Qt5_DIR=$(brew --prefix qt5)/lib/cmake/Qt5 \ -D BUILD_EXAMPLES=ON .. |
The terminal will display many lines and you need check the following points.
Now, you can start compiling.
1 |
make |
Once you’ve reached 100% without any errors, execute.
1 |
make install |
After installing, you can execute the following command to check pkg-config. The screen should print the installed version (4.5.0) of OpenCV.
1 |
pkg-config --modversion opencv4 |