Time to learn how to build and install OpenCV 4 on Windows!




 

    This post presents how to build and install the OpenCV 4 library on a Windows system in a simplified and easy way.

Getting Started

To follow this tutorial make sure to download:

  1. The OpenCV 4 source code
  2. MinGW64
  3. CMake

 

Pre-configuration

    This step is to ensure MinGW64 and CMake are correctly installed. The misconfiguration of them was a recurring problem in the past posts I wrote about how to install OpenCV 3.

 

MinGW64

  1. Extract your MinGW64 to your disk in a place such as C:\mingw64;
  2. Now add it to System Path;
    • Edit your System Environment variables (you can search for Environment in Windows’ Start Menu);
    • Click on Path, and Edit;
    • Hit New, and add the address of your MinGW64 bin folder.

 

Figure 1

Figure 1 – MinGW64 set on the Windows’ environment variables

CMake

  1. Install it normally, just paying attention to the option to add CMake to the system path as shown in Figure 2.

 

Figure 2 – CMake Install options

 

OpenCV 4

  1. Extract the files;
  2. Create a folder where the OpenCV will be built, I suggest using C:\opencv4 to better follow this tutorial.

 

To the action!

  • Open CMake-GUI;
  • Set “Where is the source code:” to the folder you just extracted the newly downloaded OpenCV 4 files;
  • Set “Where to build the binaries” to the folder you created (I suggested c:\opencv4) ;
  • Click Configure;
    • Choose MinGW MakeFiles on the next window

 

Figure 3

Figure 3 – MinGW Makefiles

 

  • Finish by clicking on Generate when the Configure step finishes;

    To summarize what I just described, take a look at Figure 4. It shows what your CMake window should look like and which button to click.

 

Figure 4 - Step by step of CMake configuration for OpenCV 4

Figure 4 – Step by step of CMake configuration

 

Building and installing OpenCV 4

  • Now, open the CMD or PowerShell, and navigate to C:\opencv4
  • Inside C:\opencv4  type mingw32-make  and hit enter to start the build process
    • This step probably will take a while, wait until it’s finished.

 

Figure 5 - OpenCV 4 Build process

Figure 5 – OpenCV 4 Build process

 

  • Install by running the  mingw32-make install command on the same folder;
  • Finally, add C:\Opencv4\install\x64\mingw\bin to your system path.

 

Figure 6 - OpenCV 4 set on environment variables

Figure 6 – OpenCV 4 set on environment variables

 

Post-installation

Tough but easy, right? Now it is time to test your build/installation.

    • Type the command opencv_version  on your CMD/Powershell, it must print the current version as shown in Figure 7:

      Figure 7 – Output of the opencv_version command

 

  • For an even more detailed insight about your build, there’s also the opencv_version_win32 command.

 

    If the info is printed on the CMD/Powershell with no errors, your OpenCV 4 build/install procedure is now complete!

    Now that you have everything set up, is possible to use OpenCV to code. Just remember, you must configure your IDE properly by adding to the include paths  C:\Opencv4\install\include  and C:\Opencv4\install\x64\mingw\lib.

 

 

 

Have fun with your projects! 
If this post helped you, please consider buying me a coffee 🙂