Installation in MacOS#
The following steps have been tested for macOS (Mavericks) but should work with other versions as well.
Required Packages#
CMake 3.9 or higher
Git
Python 3.x and NumPy 1.5 or later
This tutorial will assume you have Python, NumPy and Git installed on your machine.
Note
macOS up to 12.2 (Monterey): Comes with Python 2.7 pre-installed.
macOS 12.3 and later: Python 2.7 has been removed, and no version of Python is included by default.
It is recommended to install the latest version of Python 3.x (at least Python 3.8) for compatibility with the latest OpenCV Python bindings.
Note
If you have Xcode and Xcode Command Line Tools installed, Git is already available on your machine.
Installing CMake#
Find the version for your system and download CMake from their release’s page
Install the
.dmgpackage and launch it from Applications. That will give you the UI app of CMakeFrom the CMake app window, choose menu Tools –> How to Install For Command Line Use. Then, follow the instructions from the pop-up there.
The install folder will be
/usr/local/bin/by default. Complete the installation by choosing Install command line links.Test that CMake is installed correctly by running:
cmake --version
Note
You can use Homebrew to install CMake with:
brew install cmake
Getting OpenCV Source Code#
You can use the latest stable OpenCV version or you can grab the latest snapshot from our Git repository.
Getting the Latest Stable OpenCV Version#
Go to our OpenCV releases page.
Download the source archive of the latest version (e.g., OpenCV 4.x) and unpack it.
Getting the Cutting-edge OpenCV from the Git Repository#
Launch Git client and clone OpenCV repository. If you need modules from OpenCV contrib repository then clone it as well.
For example:
cd ~/<your_working_directory>
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
Building OpenCV from Source Using CMake#
Create a temporary directory, which we denote as
build_opencv, where you want to put the generated Makefiles, project files as well the object files and output binaries and enter there.For example:
mkdir build_opencv cd build_opencv
Note
It is good practice to keep your source code directories clean. Create the build directory outside of the source tree.
Configuring. Run
cmake [<some optional parameters>] <path to the OpenCV source directory>For example:
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_EXAMPLES=ON ../opencv
Alternatively, you can use the CMake GUI (
cmake-gui):set the OpenCV source code path to, e.g.
/Users/your_username/opencvset the binary build path to your CMake build directory, e.g.
/Users/your_username/build_opencvset optional parameters
run: “Configure”
run: “Generate”
Description of some parameters
build type:
-DCMAKE_BUILD_TYPE=Release(orDebug).include Extra Modules: If you cloned the
opencv_contribrepository and want to include its modules, set:-DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modulesset
-DBUILD_DOCS=ONfor building documents (doxygen is required)set
-DBUILD_EXAMPLES=ONto build all examples
[optional] Building python. Set the following python parameters:
-DPYTHON3_EXECUTABLE=$(which python3)-DPYTHON3_INCLUDE_DIR=$(python3 -c "from sysconfig import get_paths as gp; print(gp()['include'])")-DPYTHON3_NUMPY_INCLUDE_DIRS=$(python3 -c "import numpy; print(numpy.get_include())")
Build. From build directory execute make, it is recommended to do this in several threads
For example:
make -j$(sysctl -n hw.ncpu) # runs the build using all available CPU cores
After building, you can install OpenCV system-wide using:
sudo make install
To use OpenCV in your CMake-based projects through
find_package(OpenCV), specify theOpenCV_DIRvariable pointing to the build or install directory.For example:
cmake -DOpenCV_DIR=~/build_opencv ..
Verifying the OpenCV Installation#
After building (and optionally installing) OpenCV, you can verify the installation by checking the version using Python:
python3 -c "import cv2; print(cv2.__version__)"
This command should output the version of OpenCV you have installed.
Note
You can also use a package manager like Homebrew or pip to install releases of OpenCV only (Not the cutting edge).
Installing via Homebrew:
For example:
brew install opencv
Installing via pip:
For example:
pip install opencv-python
Note
To access the extra modules from
opencv_contrib, install theopencv-contrib-pythonpackage usingpip install opencv-contrib-python.