Building on macOS
Prerequisites
Section titled “Prerequisites”-
Install Xcode from the Mac App Store (required for the compiler toolchain).
-
Install Xcode Command Line Tools:
Terminal window xcode-select --install -
Install Homebrew (if not already installed):
Terminal window /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" -
Install CMake and dependencies via Homebrew:
Terminal window brew install cmake pkg-configbrew install jpeg libpng libtiff openexrbrew install eigen tbb -
Install Python and NumPy (for Python bindings):
Terminal window brew install python3pip3 install numpy
Build Steps (Intel and Apple Silicon)
Section titled “Build Steps (Intel and Apple Silicon)”git clone https://github.com/opencv/opencv.gitgit clone https://github.com/opencv/opencv_contrib.gitgit clone https://github.com/opencv/opencv_extra.git
mkdir build_opencv && cd build_opencv
cmake \ -DCMAKE_BUILD_TYPE=Debug \ -DBUILD_TESTS=ON \ -DBUILD_PERF_TESTS=ON \ -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules \ -DOPENCV_TEST_DATA_PATH=../opencv_extra/testdata \ ../opencv
make -j$(sysctl -n hw.ncpu)git clone https://github.com/opencv/opencv.gitgit clone https://github.com/opencv/opencv_contrib.gitgit clone https://github.com/opencv/opencv_extra.git
mkdir build_opencv && cd build_opencv
cmake \ -DCMAKE_BUILD_TYPE=Debug \ -DCMAKE_SYSTEM_PROCESSOR=arm64 \ -DCMAKE_OSX_ARCHITECTURES=arm64 \ -DWITH_OPENJPEG=OFF \ -DWITH_IPP=OFF \ -DBUILD_TESTS=ON \ -DBUILD_PERF_TESTS=ON \ -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules \ -DOPENCV_TEST_DATA_PATH=../opencv_extra/testdata \ ../opencv
make -j$(sysctl -n hw.ncpu)Xcode IDE Integration
Section titled “Xcode IDE Integration”# Generate Xcode project filescmake -G Xcode \ -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules \ ../opencv
# Open in Xcodeopen OpenCV.xcodeprojIn Xcode, Debug/Release is selected from the scheme toolbar. Set the build scheme to ALL_BUILD to compile everything, or select individual test targets.
VS Code on macOS
Section titled “VS Code on macOS”VS Code with the CMake Tools extension works well. After building, set OpenCV_DIR in your project’s CMakeLists.txt to the build directory:
set(OpenCV_DIR "/path/to/build_opencv")find_package(OpenCV REQUIRED)Python Bindings on macOS
Section titled “Python Bindings on macOS”# After build, set PYTHONPATH to use cv2 from build direxport PYTHONPATH=/path/to/build_opencv/python_loader:/path/to/build_opencv/lib/python3/export DYLD_LIBRARY_PATH=/path/to/build_opencv/lib:$DYLD_LIBRARY_PATH
python3 -c "import cv2; print(cv2.__version__)"