I found a more compact solution, very close to this guide. It's more cumbersome, but leaves you with a very clean installation.
Introduction
This entry documents the installation of OpenCV from source for both C++ and Python. We will show the procedure at the hand of OpenCV 3.4.1, but all versions will work in the same or highly similar way. Since, at the time of writing, Visual Studio 2013 is the default IDE we cannot support OpenCV versions 4, as the C++ 11 standard is not supported by the VC 2013 compiler. Furthermore, we ran into similar problems for versions >= 3.4.2 We will, however, also include a section at the bottom which extends this guide to the installation of OpenCV 4 with Visual Studio 2017.
We will run most action from the command line. Our preferred tool for this is Window's native PowerShell.
Software Requirements
Visual Studio 2013
As explained above, Visual Studio 2013 with its C++ compiler, is the default IDE at the time of writing. You could also compile with a newer version of Visual Studio. However, it is not guaranteed that you can use the compiled code from applications compiled with a different Visual Studio version (and thus different compiler).
Git
We will use git to fetch the source code. You could also run this guide without git but downloading the source code from OpenCV's Github ... could but rather simply use git.
CMake
Download and install the most current version of CMake. We run this tutorial with version 3.11.0. Check the box Add CMake to the system path for current user.
Python (Anaconda Distribution)
We will also compile the Python bindings. Please install Anaconda as shown here. Although we will set-up and environment for our Python installation, we need to make sure we have the correct Python version (3.6) installed in the root environment. Therefore, install Anaconda and follow the instructions on the bottom of the installation guide and set the python installation to 3.6.
Now, open an Anaconda command prompt and create a new environment with name OpenCV and install Python 3.6 and all packages coming with Anaconda (for example numpy). You can do so, by pasting conda create -y -f -n OpenCV python=3.6 anaconda
into the command prompt.
Remarks on Environment:
We encountered problems when the python version in the root environment did not match the version in the OpenCV environment and therefore we needed to make sure we install the correct Python version in both environments. We do think it happens as CMake finds the root environment and does not ask us for its location. We found the workaround shown above. However, if you need a different Python version in your root, we think you can run this guide and use CMake for configuration BUT before generating the build, you need to open the CMakeCache.txt file in the build folder and change the Python library and interpreter paths ... (more)