In this section, we will see how to install OpenCV on Mac OS X. Precompiled binaries are not available for Mac OS X, so we need to compile OpenCV from scratch.
Before we proceed, we need to install CMake. If you don't already have CMake installed, you can download it from here: https://cmake.org/files/v3.12/cmake-3.12.0-rc1-Darwin-x86_64.dmg. It's a .dmg file, so once you download it, just run the installer.
Download the latest version of OpenCV from opencv.org. The current version is 4.0.0, and you can download it from here: https://github.com/opencv/opencv/archive/4.0.0.zip. Unzip the contents into a folder of your choice.
OpenCV 4.0.0 also has a new package called opencv_contrib, containing user contributions that are not yet considered stable, and some algorithms that are not freely available for commercial use in all the latest computer vision algorithms, which is worth keeping in mind. Installing this package is optional—OpenCV will work just fine if you don't install opencv_contrib.
Since we are installing OpenCV anyway, it's good to install this package so that you can experiment with it later on (as opposed to going through the whole installation process again). It is a great way to learn and play around with new algorithms. You can download it from the following link: https://github.com/opencv/opencv_contrib/archive/4.0.0.zip.
Unzip the contents of the zip file into a folder of your choice. For convenience, unzip it into the same folder as before, so that the opencv-4.0.0 and opencv_contrib-4.0.0 folders are in the same main folder.
We are now ready to build OpenCV. Open up your Terminal and navigate to the folder where you unzipped the contents of OpenCV 4.0.0. Run the following commands after substituting the right paths in the commands:
$ cd /full/path/to/opencv-4.0.0/
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/full/path/to/opencv-4.0.0/build -D INSTALL_C_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=/full/path/to/opencv_contrib-4.0.0/modules ../
It's time to install OpenCV 4.0.0. Go to the /full/path/to/opencv-4.0.0/build directory, and run the following commands on your Terminal:
$ make -j4
$ make install
In the preceding command, the -j4 flag indicates that it should be using four cores to install it. It's faster this way! Now, let's set the library path. Open up your ~/.profile file in your Terminal using the vi ~/.profile command, and add the following line:
export DYLD_LIBRARY_PATH=/full/path/to/opencv-4.0.0/build/lib:$DYLD_LIBRARY_PATH
We need to copy the pkgconfig file in opencv.pc to /usr/local/lib/pkgconfig and name it opencv4.pc. This way, if you already have an existing OpenCV 3.x.x installation, there will be no conflict. Let's go ahead and do that:
$ cp /full/path/to/opencv-4.0.0/build/lib/pkgconfig/opencv.pc /usr/local/lib/pkgconfig/opencv4.pc
We need to update our PKG_CONFIG_PATH variable as well. Open up your ~/.profile file and add the following line:
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/:$PKG_CONFIG_PATH
Reload your ~/.profile file using the following command:
$ source ~/.profile
We're finished! Let's see if it's working:
$ cd /full/path/to/opencv-4.0.0/samples/cpp
$ g++ -ggdb `pkg-config --cflags --libs opencv4` opencv_version.cpp -o /tmp/opencv_version && /tmp/opencv_version
If you see Welcome to OpenCV 4.0.0 printed on your Terminal, you are good to go. We will be using CMake to build our OpenCV projects throughout this book. We will cover it in more detail in Chapter 2, An Introduction to the Basics of OpenCV.