Overview

In this module, we will discuss how to Install OpenCV for C++ on macOS.

The instructions might be complicated, but give time to understand and install it correctly. Ask questions in the Forum; we will be available to answer all the questions.

Although these instructions are to install OpenCV 4.5.0, you can use them to install any version of OpenCV.

Step 1: Install XCode

Install XCode from the App Store. If XCode available on the App Store is not compatible with your OS:

  • A. Find XCode version compatible with your OS from this table.
  • B. Go to this webpage.
  • C. Log in to your apple developer account or create a new account.
  • D. Search for Xcode and download the version compatible with your OS.
  • E. Install XCode.

After installation, open XCode and accept Xcode-build license when prompted.

Step 2: Install dependencies

If you have not installed Homebrew already, run the following command on the terminal to install it:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" 

Next, install the dependencies using Homebrew:

brew install cmake 
brew install qt5 
qtfolder=$(ls /usr/local/Cellar/qt/) 
QT5PATH=/usr/local/Cellar/qt/"$qtfolder"

Step 3: Download and Install OpenCV 4.5.0 

We are going to create an installation directory where OpenCV is downloaded and installed.Start a new terminal session and change directory to the OpenCV install path.

# Create directory for installation

mkdir installation 

mkdir installation/OpenCV

For convenience, let us store the current working directory in OpenCV_Home_DIR variable. The variable will be convenient for later stages.

# Save current working directory

OpenCV_Home_DIR=$(pwd) 

Now, we clone OpenCV and OpenCV_contrib packages from their GitHub repositories.

First, we clone the OpenCV_contrib GitHub repository and checkout the version of choice.

git clone https://github.com/opencv/opencv_contrib.git 

cd opencv_contrib git checkout tags/4.5.0 

cd ..

Similarly, we clone the OpenCV repository and checkout to the version selected.

git clone https://github.com/opencv/opencv.git 
cd opencv 
git checkout tags/4.5.0 

Now, we start building OpenCV using CMake. First, we configure the build.

Create a build directory.

mkdir build && cd build Configure OpenCV

cmake \ -D CMAKE_BUILD_TYPE=RELEASE \

-D CMAKE_INSTALL_PREFIX=$OpenCV_Home_DIR/installation/OpenCV \ 

-D INSTALL_C_EXAMPLES=ON \ 

-D WITH_TBB=ON \ 

-D CMAKE_PREFIX_PATH=$QT5PATH \ 

-D CMAKE_MODULE_PATH="$QT5PATH"/lib/cmake \ 

-D WITH_QT=ON \ 

-D WITH_OPENGL=ON \ 

-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \ 

-D BUILD_EXAMPLES=ON \ 

..

Finally install OpenCV

make -j$(sysctl -n hw.physicalcpu) 
sudo make install 
cd $OpenCV_Home_DIR

You can delete the build folder inside opencv if you want to free up space. The installation folder is $OpenCV_Home_DIR/installation/OpenCV/.

You will find the CMake files in "$OpenCV_Home_DIR/installation/OpenCV/lib/cmake/opencv4".

Leave a Reply