Let's see how to get OpenCV up and running on various operating systems.
Installing OpenCV
Windows
To keep things easy, let's install OpenCV using pre-built libraries. Go to opencv.org and download the latest version for Windows. The current version is 4.0.0, and you can get the download link from the OpenCV homepage. You should make sure you have admin rights before you proceed.
The downloaded file will be an executable file, so just double-click on it to start the installation. The installer expands the content into a folder. You will be able to choose the installation path, and check the installation by inspecting the files.
Once you are done with the previous step, we need to set the OpenCV environment variables and add them to the system path to complete the installation. We will set up an environment variable that will hold the build directory of the OpenCV library. We will be using this in our projects.
Open up the Terminal and type the following:
C:> setx -m OPENCV_DIR D:OpenCVBuildx64vc14
Let's go ahead and add a path to the bin folder to our system path. The reason we need to do this is because we will be using the OpenCV library in the form of dynamic link libraries (DLLs). Essentially, all the OpenCV algorithms are stored here, and our operating system will only load them during runtime.
In order to do that, our operating system needs to know where they are located. The PATH system variable contains a list of all the folders where it can find DLLs. So, naturally, we need to add the path of the OpenCV library to this list.
Why do we need to do all this? Well, the other option is to copy the required DLLs in the same folder as the application's executable file (.exe file). This is an unnecessary overhead, especially when we are working with many different projects.
We need to edit the PATH variable to add this folder. You can use software such as Path Editor to do this, which you can download from here: https://patheditor2.codeplex.com. Once you install it, start it up and add the following new entry (you can right-click on the path to insert a new item):
%OPENCV_DIR%bin
Go ahead and save it to the registry. We are done!
Mac OS X
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.
Linux
Let's see how to install OpenCV on Ubuntu. We need to install some dependencies before we begin. Let's install them using the package manager by running the following command in your Terminal:
$ sudo apt-get -y install libopencv-dev build-essential cmake libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev libqt4-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils
Now that you have installed the dependencies, let's download, build, and install OpenCV:
$ wget "https://github.com/opencv/opencv/archive/4.0.0.tar.gz" -O opencv.tar.gz $ wget "https://github.com/opencv/opencv_contrib/archive/4.0.0.tar.gz" -O opencv_contrib.tar.gz $ tar -zxvf opencv.tar.gz $ tar -zxvf opencv_contrib.tar.gz $ cd 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 ../ $ make -j4 $ sudo make install
Let's copy the pkgconfig file in opencv.pc to /usr/local/lib/pkgconfig, and name it opencv4.pc:
$ cp /full/path/to/opencv-4.0.0/build/lib/pkgconfig/opencv.pc /usr/local/lib/pkgconfig/opencv4.pc
We're finished! We will now be able to use it to compile our OpenCV programs from the command line. Also, if you already have an existing OpenCV 3.x.x installation, there will be no conflict.
Let's check the installation is working properly:
$ 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 should be good to go. In the following chapters, we will learn how to use CMake to build our OpenCV projects.