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
We are assuming that you have a 64-bit machine with Visual Studio 2015 installed. If you have Visual Studio 2012, replace vc14 with vc11 in the command. The path specified is where we would have our OpenCV binaries, and you should see two folders inside that path called lib and bin. If you are using Visual Studio 2018, you should compile OpenCV from scratch.

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!